国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

界面網(wǎng)站的風格關鍵字c語言

界面網(wǎng)站的風格,關鍵字c語言,個人網(wǎng)站備案需要哪些材料,網(wǎng)站建設公司薪酬在現(xiàn)代微服務架構(gòu)中,通常需要與多個數(shù)據(jù)庫交互的服務。這可能是由于各種原因,例如遺留系統(tǒng)集成、不同類型的數(shù)據(jù)存儲需求,或者僅僅是為了優(yōu)化性能。Spring Boot 具有靈活的配置和強大的數(shù)據(jù)訪問庫,可以輕松配置多個數(shù)據(jù)庫。在本綜…

在現(xiàn)代微服務架構(gòu)中,通常需要與多個數(shù)據(jù)庫交互的服務。這可能是由于各種原因,例如遺留系統(tǒng)集成、不同類型的數(shù)據(jù)存儲需求,或者僅僅是為了優(yōu)化性能。Spring Boot 具有靈活的配置和強大的數(shù)據(jù)訪問庫,可以輕松配置多個數(shù)據(jù)庫。在本綜合指南中,我們將探討如何在 Spring Boot 微服務中設置和管理多個數(shù)據(jù)庫連接。

1. 簡介

微服務通常需要與各種數(shù)據(jù)庫交互。每個微服務可能需要不同類型的數(shù)據(jù)庫,例如用于事務數(shù)據(jù)的 SQL 數(shù)據(jù)庫和用于非結(jié)構(gòu)化數(shù)據(jù)的 NoSQL 數(shù)據(jù)庫。Spring Boot 為配置和管理多個數(shù)據(jù)源提供了出色的支持,使其成為現(xiàn)代微服務架構(gòu)的理想選擇。

2.為什么要使用多個數(shù)據(jù)庫?

您可能需要在微服務中使用多個數(shù)據(jù)庫的原因如下:

  • 遺留系統(tǒng)集成:與遺留系統(tǒng)的現(xiàn)有數(shù)據(jù)庫集成。
  • 優(yōu)化性能:使用針對特定類型的數(shù)據(jù)(例如關系型與非關系型)優(yōu)化的不同數(shù)據(jù)庫。
  • 數(shù)據(jù)隔離:出于安全、合規(guī)或組織原因分離數(shù)據(jù)。
  • 可擴展性:在不同的數(shù)據(jù)庫之間分配數(shù)據(jù)負載以提高性能。

3.設置 Spring Boot 項目

首先,創(chuàng)建一個新的 Spring Boot 項目。您可以使用 Spring Initializr 或您喜歡的 IDE 來設置項目。

Maven 依賴項

在您的 中pom.xml包含 Spring Data JPA 和您將使用的數(shù)據(jù)庫的依賴項(例如,內(nèi)存中的 H2、PostgreSQL、MySQL 等)。

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope></dependency>
</dependencies>

4.配置多個數(shù)據(jù)源

application.ymlapplication.properties文件中,配置每個數(shù)據(jù)庫的連接屬性。

application.yml

spring:datasource:primary:url: jdbc:h2:mem:primarydbdriver-class-name: org.h2.Driverusername: sapassword: passwordsecondary:url: jdbc:postgresql://localhost:5432/secondarydbdriver-class-name: org.postgresql.Driverusername: postgrespassword: password
jpa:primary:database-platform: org.hibernate.dialect.H2Dialecthibernate:ddl-auto: updatesecondary:database-platform: org.hibernate.dialect.PostgreSQLDialecthibernate:ddl-auto: update

5.創(chuàng)建數(shù)據(jù)源配置類

接下來,為每個數(shù)據(jù)源創(chuàng)建單獨的配置類。

主數(shù)據(jù)源配置

package com.example.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(basePackages = "com.example.primary.repository",entityManagerFactoryRef = "primaryEntityManagerFactory",transactionManagerRef = "primaryTransactionManager"
)
public class PrimaryDataSourceConfig {@Bean(name = "primaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "primaryEntityManagerFactory")public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(@Qualifier("primaryDataSource") DataSource dataSource) {LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();em.setDataSource(dataSource);em.setPackagesToScan(new String[] { "com.example.primary.entity" });HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();em.setJpaVendorAdapter(vendorAdapter);return em;}@Bean(name = "primaryTransactionManager")public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);}
}

輔助數(shù)據(jù)源配置

package com.example.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableJpaRepositories(basePackages = "com.example.secondary.repository",entityManagerFactoryRef = "secondaryEntityManagerFactory",transactionManagerRef = "secondaryTransactionManager"
)
public class SecondaryDataSourceConfig {@Bean(name = "secondaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "secondaryEntityManagerFactory")public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(@Qualifier("secondaryDataSource") DataSource dataSource) {LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();em.setDataSource(dataSource);em.setPackagesToScan(new String[] { "com.example.secondary.entity" });HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();em.setJpaVendorAdapter(vendorAdapter);return em;}@Bean(name = "secondaryTransactionManager")public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);}
}

6. 定義實體管理器

為每個數(shù)據(jù)庫定義實體類。確保將它們放在配置類中指定的相應包中。

主數(shù)據(jù)庫實體

package com.example.primary.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PrimaryEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;// getters and setters
}

輔助數(shù)據(jù)庫實體

package com.example.secondary.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class SecondaryEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String description;// getters and setters
}

7. 創(chuàng)建存儲庫

為每個數(shù)據(jù)庫創(chuàng)建存儲庫接口,確保它們按照配置放置在正確的包中。

主存儲庫

package com.example.primary.repository;
import com.example.secondary.entity.SecondaryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SecondaryRepository extends JpaRepository<SecondaryEntity, Long> {
}

二級存儲庫

package com.example.secondary.repository;
import com.example.secondary.entity.SecondaryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SecondaryRepository extends JpaRepository<SecondaryEntity, Long> {
}

8.測試配置

最后,創(chuàng)建一個簡單的 REST 控制器來測試設置。此控制器將使用兩個存儲庫來執(zhí)行 CRUD 操作。

package com.example.controller;
import com.example.primary.entity.PrimaryEntity;
import com.example.primary.repository.PrimaryRepository;
import com.example.secondary.entity.SecondaryEntity;
import com.example.secondary.repository.SecondaryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {@Autowiredprivate PrimaryRepository primaryRepository;@Autowiredprivate SecondaryRepository secondaryRepository;@GetMapping("/test")public String test() {PrimaryEntity primaryEntity = new PrimaryEntity();primaryEntity.setName("Primary Entity");primaryRepository.save(primaryEntity);SecondaryEntity secondaryEntity = new SecondaryEntity();secondaryEntity.setDescription("Secondary Entity");secondaryRepository.save(secondaryEntity);return "Entities saved!";}
}

http://aloenet.com.cn/news/41835.html

相關文章:

  • 成都哪里有做網(wǎng)站建設的青島網(wǎng)站建設公司電話
  • 在小說網(wǎng)站做責編如何免費發(fā)布廣告
  • 網(wǎng)站制作公司智能 樂云踐新做網(wǎng)站怎么優(yōu)化
  • 網(wǎng)站模板購買房地產(chǎn)估價師考試
  • 小程序開發(fā)平臺哪里做得好seo網(wǎng)站優(yōu)化培訓怎么做
  • 可以免費發(fā)帖的網(wǎng)站品牌廣告投放
  • asp如何做網(wǎng)站河北網(wǎng)站seo策劃
  • 網(wǎng)站瀏覽思路上海網(wǎng)絡推廣公司網(wǎng)站
  • 哪個網(wǎng)站推廣做的好引流獲客app下載
  • 關于藥品網(wǎng)站建設策劃書seo軟件哪個好
  • 做網(wǎng)站公司在深圳杭州疫情最新情況
  • 網(wǎng)站建設在線視頻上海百度推廣官網(wǎng)
  • java源代碼網(wǎng)站seo在線外鏈
  • 企業(yè)網(wǎng)站建設系統(tǒng)惠東seo公司
  • 邯鄲哪有做網(wǎng)站的公司邵陽網(wǎng)站seo
  • 網(wǎng)站開發(fā)原型濰坊今日頭條新聞
  • 濰坊市建設局網(wǎng)站上海關鍵詞自動排名
  • 網(wǎng)站源碼上傳完后怎么做足球排名最新排名世界
  • 怎么給企業(yè)做網(wǎng)站網(wǎng)絡營銷企業(yè)是什么
  • 網(wǎng)站策劃書十大外貿(mào)電商平臺
  • 邯鄲網(wǎng)站建設縱橫廣告軟文怎么寫
  • 中華南大街網(wǎng)站建設佛山百度網(wǎng)站排名優(yōu)化
  • 深圳網(wǎng)站建設公司哪家專業(yè)今日國內(nèi)新聞10則
  • 如何制作網(wǎng)站平臺北京aso優(yōu)化
  • 銷項稅和進項導入是在國稅網(wǎng)站做嗎制作網(wǎng)站費用
  • 自己的電腦做服務器建立網(wǎng)站的方法百度競價搜索
  • 做能支付的網(wǎng)站貴嗎國內(nèi)免費域名
  • 小說網(wǎng)站建設的支柱app推廣活動策劃方案
  • 做門的網(wǎng)站1688的網(wǎng)站特色
  • python 做的網(wǎng)站有哪些如何在網(wǎng)絡上推廣產(chǎn)品