只读副本和Spring Data第3部分:配置两个实体管理器

我们之前的设置可以正常工作。 我们现在要做的是进一步发展,并配置两个单独的实体管理器,而不会影响我们之前实现的功能。

第一步是将默认的实体管理器配置设置为主要配置。
这是第一步

 package com.gkatzioura.springdatareadreplica.config;  import javax.sql.DataSource;  import org.springframework.beans.factory.annotation.Qualifier;  import org.springframework.beans.factory.annotation.Value;  import org.springframework.boot.jdbc.DataSourceBuilder;  import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.ComponentScan;  import org.springframework.context.annotation.Configuration;  import org.springframework.context.annotation.Primary;  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;  @Configuration  public class PrimaryEntityManagerConfiguration { 
     @Value ( "${spring.datasource.username}" ) 
     private String username; 
     @Value ( "${spring.datasource.password}" ) 
     private String password; 
     @Value ( "${spring.datasource.url}" ) 
     private String url; 
     @Bean 
     @Primary 
     public DataSource dataSource() throws Exception { 
         return DataSourceBuilder.create() 
                                 .url(url) 
                                 .username(username) 
                                 .password(password) 
                                 .driverClassName( "org.postgresql.Driver" ) 
                                 .build(); 
     } 
     @Bean 
     @Primary 
     public LocalContainerEntityManagerFactoryBean entityManagerFactory( 
             EntityManagerFactoryBuilder builder, 
             @Qualifier ( "dataSource" ) DataSource dataSource) { 
         return builder.dataSource(dataSource) 
                       .packages( "com.gkatzioura.springdatareadreplica" ) 
                       .persistenceUnit( "main" ) 
                       .build(); 
     }  } 

如果您使用此配置运行您的应用程序,它将像之前的应用程序一样运行。
现在是时候配置只读实体管理器了。

 package com.gkatzioura.springdatareadreplica.config;  import javax.sql.DataSource;  import org.springframework.beans.factory.annotation.Qualifier;  import org.springframework.beans.factory.annotation.Value;  import org.springframework.boot.jdbc.DataSourceBuilder;  import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Primary;  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;  @Configuration  public class ReadOnlyEntityManagerConfiguration { 
     @Value ( "${spring.datasource.username}" ) 
     private String username; 
     @Value ( "${spring.datasource.password}" ) 
     private String password; 
     @Value ( "${spring.datasource.readUrl}" ) 
     private String readUrl; 
     @Bean 
     public DataSource readDataSource() throws Exception { 
         return DataSourceBuilder.create() 
                                 .url(readUrl) 
                                 .username(username) 
                                 .password(password) 
                                 .driverClassName( "org.postgresql.Driver" ) 
                                 .build(); 
     } 
     @Bean 
     public LocalContainerEntityManagerFactoryBean readEntityManagerFactory( 
             EntityManagerFactoryBuilder builder, 
             @Qualifier ( "readDataSource" ) DataSource dataSource) { 
         return builder.dataSource(dataSource) 
                       .packages( "com.gkatzioura.springdatareadreplica" ) 
                       .persistenceUnit( "read" ) 
                       .build(); 
     }  } 

另外,我将向控制器添加方法以保存模型。

 package com.gkatzioura.springdatareadreplica.controller;  import java.util.List;  import org.springframework.http.HttpStatus;  import org.springframework.web.bind.annotation.GetMapping;  import org.springframework.web.bind.annotation.PostMapping;  import org.springframework.web.bind.annotation.RequestBody;  import org.springframework.web.bind.annotation.ResponseStatus;  import org.springframework.web.bind.annotation.RestController;  import com.gkatzioura.springdatareadreplica.entity.Employee;  import com.gkatzioura.springdatareadreplica.repository.EmployeeRepository;  @RestController  public class EmployeeContoller { 
     private final EmployeeRepository employeeRepository; 
     public EmployeeContoller(EmployeeRepository employeeRepository) { 
         this .employeeRepository = employeeRepository; 
     } 
     @GetMapping ( "/employee" ) 
     public List<Employee> getEmployees() { 
         return employeeRepository.findAll(); 
     } 
     @PostMapping ( "/employee" ) 
     @ResponseStatus (HttpStatus.CREATED) @ResponseStatus (HttpStatus.CREATED) 
     public void addEmployee( @RequestBody Employee employee) { 
         employeeRepository.save(employee); 
     }  } 

如果您确实尝试使用控制器添加员工,然后查询读取的数据库,您将看到根本没有添加任何条目。

因此,我们已经启动并运行了主要的实体管理器,而我们也有辅助的实体管理器。 第二个尚未使用。 下一个博客着重于使用辅助只读实体管理器。

翻译自: https://www.javacodegeeks.com/2019/10/read-replicas-and-spring-data-configuring-two-entity-managers.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值