日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

只读副本和Spring Data第4部分:配置只读存储库

發布時間:2023/12/3 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 只读副本和Spring Data第4部分:配置只读存储库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前,我們在同一應用程序中設置了兩個EntityManager。 一種用于讀取,另一種用于寫入。 現在是時候創建我們的讀取存儲庫了。

只讀存儲庫將使用輔助只讀EntityManager。

為了使其成為只讀存儲庫,至關重要的是不要執行任何保存和持久操作。

package com.gkatzioura.springdatareadreplica.repository; import java.util.List; import org.springframework.data.repository.Repository; import com.gkatzioura.springdatareadreplica.config.ReadOnlyRepository; import com.gkatzioura.springdatareadreplica.entity.Employee; /** * This is a read only repository */ public interface ReadEmployeeRepository extends Repository { List findAll(); }

我們的下一個任務是使用讀取數據庫實體管理器創建此存儲庫。
這意味著除只讀存儲庫外,所有存儲庫均應使用默認實體管理器創建。

我將首先創建一個注釋。 此注釋將聲明我的存儲庫為只讀。 另外,我將使用此批注進行掃描操作,以便使用適當的EntityManager。

package com.gkatzioura.springdatareadreplica.config; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention (RetentionPolicy.RUNTIME) @Target ({ElementType.TYPE}) @Documented public @interface ReadOnlyRepository { }

現在,我知道Spring Boot消除了對注釋的需求,并以自動化的方式創建了存儲庫,但是我們的案例很特殊。

通過進行一些調整,我們的只讀存儲庫將如下所示

package com.gkatzioura.springdatareadreplica.repository; import java.util.List; import org.springframework.data.repository.Repository; import com.gkatzioura.springdatareadreplica.config.ReadOnlyRepository; import com.gkatzioura.springdatareadreplica.entity.Employee; /** * This is a read only repository */ @ReadOnlyRepository public interface ReadEmployeeRepository extends Repository { List findAll(); }

現在是時候使用我們的存儲庫掃描了。 除使用@ReadOnlyRepository批注進行注釋的存儲庫外,所有存儲庫均將注入主EntityManager。

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 @EnableJpaRepositories ( basePackages = "com.gkatzioura" , excludeFilters = @ComponentScan .Filter(ReadOnlyRepository. class ), entityManagerFactoryRef = "entityManagerFactory" ) 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.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; @Configuration @EnableJpaRepositories ( basePackages = "com.gkatzioura" , includeFilters= @ComponentScan .Filter(ReadOnlyRepository. class ), entityManagerFactoryRef = "readEntityManagerFactory" ) 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(); } }

輔助實體管理器將僅注入僅具有@ReadOnlyRepository批注的存儲庫。

為了說明這一點,讓我們對控制器進行一些更改。

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; import com.gkatzioura.springdatareadreplica.repository.ReadEmployeeRepository; @RestController public class EmployeeContoller { private final EmployeeRepository employeeRepository; private final ReadEmployeeRepository readEmployeeRepository; public EmployeeContoller(EmployeeRepository employeeRepository, ReadEmployeeRepository readEmployeeRepository) { this .employeeRepository = employeeRepository; this .readEmployeeRepository = readEmployeeRepository; } @GetMapping ( "/employee" ) public List getEmployees() { return employeeRepository.findAll(); } @GetMapping ( "/employee/read" ) public List getEmployeesRead() { return readEmployeeRepository.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-the-read-repository.html

總結

以上是生活随笔為你收集整理的只读副本和Spring Data第4部分:配置只读存储库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。