2016년 11월 27일 일요일

Configuring Master/Slave Replication with Connector/J

You can see the sql query log by using log4jdbc.

- Structure
















- application.yml

spring:
  application:
    name: jpa-project
  datasource:
    url: jdbc:log4jdbc:mysql:replication://localhost,192.168.1.5:3306/test?useSSL=false
    driver-class-name: net.sf.log4jdbc.DriverSpy
    username: user
    password: password
logging:
  level:
    jdbc:
      sqltiming: info
      audit: fatal
      resultset: fatal
      sqlonly: fatal
      connection: fatal

- JpaApplication.java

package org.blog.test;
package org.blog.test;

import lombok.extern.slf4j.Slf4j;
import org.blog.test.user.entity.User;
import org.blog.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class JpaApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }

    @Autowired
    private UserService userService;

    @Override
    public void run(String... args) throws Exception {
        User user = userService.getUser("name1");
        log.info("master : {}", user);
        User user1 = userService.getUserFromSlave("name1");
        log.info("slave : {}", user1);
    }
}

- user.java

package org.blog.test.user.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;


@Table
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column
    private String name;

    public User(String name) {
        this.name = name;
    }
}


- UserRepository.java

package org.blog.test.user.repository;

import org.blog.test.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;


public interface UserRepository extends JpaRepository {
    User findByName(String name);
}

- UserService.java

package org.blog.test.user.service;
package org.blog.test.user.service;

import org.blog.test.user.entity.User;


public interface UserService {

    User addUser(String name);

    User getUser(String name);

    User getUserFromSlave(String name);
}

- UserServiceImpl.java
package org.blog.test.user.service.impl;

import org.blog.test.user.entity.User;
import org.blog.test.user.repository.UserRepository;
import org.blog.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

@Override
public User addUser(String name) {
return userRepository.save(new User(name));
}

@Override
public User getUser(String name) {
return userRepository.findByName(name);
}

@Override
@Transactional(readOnly = true)
public User getUserFromSlave(String name) {
return userRepository.findByName(name);
}
}

- build.gradle

apply plugin: 'java'
apply plugin: 'spring-boot'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
    compile group: 'com.googlecode.log4jdbc', name: 'log4jdbc', version: '1.2'
}

- result

2016-11-27 22:18:01.846  INFO 10884 --- [           main] jdbc.sqltiming                           : select user0_.id as id1_0_, user0_.name as name2_0_ from user user0_ where user0_.name='name1' 
 {executed in 21 msec}
2016-11-27 22:18:01.857  INFO 10884 --- [           main] org.blog.test.JpaApplication             : master : null
2016-11-27 22:18:01.872  INFO 10884 --- [           main] jdbc.sqltiming                           : select user0_.id as id1_0_, user0_.name as name2_0_ from user user0_ where user0_.name='name1' 
 {executed in 8 msec}
2016-11-27 22:18:01.899  INFO 10884 --- [           main] org.blog.test.JpaApplication             : slave : User(id=1, name=name1)
2016-11-27 22:18:01.901  INFO 10884 --- [           main] org.blog.test.JpaApplication             : Started JpaApplication in 10.216 seconds (JVM running for 10.937)

댓글 없음 :

댓글 쓰기