在SpringBoot微服务中设置和管理多个数据库的代码示例

 更新时间:2024年12月26日 09:12:30   作者:小蜗牛慢慢爬行  
在现代微服务架构中,通常需要与多个数据库交互的服务,这可能是由于各种原因,例如遗留系统集成、不同类型的数据存储需求,或者仅仅是为了优化性能,在本综合指南中,我们将探讨如何在 Spring Boot 微服务中设置和管理多个数据库连接,需要的朋友可以参考下

引言

在现代微服务架构中,通常需要与多个数据库交互的服务。这可能是由于各种原因,例如遗留系统集成、不同类型的数据存储需求,或者仅仅是为了优化性能。Spring Boot 具有灵活的配置和强大的数据访问库,可以轻松配置多个数据库。在本综合指南中,我们将探讨如何在 Spring Boot 微服务中设置和管理多个数据库连接。

1. 简介

微服务通常需要与各种数据库交互。每个微服务可能需要不同类型的数据库,例如用于事务数据的 SQL 数据库和用于非结构化数据的 NoSQL 数据库。Spring Boot 为配置和管理多个数据源提供了出色的支持,使其成为现代微服务架构的理想选择。

2.为什么要使用多个数据库?

您可能需要在微服务中使用多个数据库的原因如下:

  • 遗留系统集成:与遗留系统的现有数据库集成。
  • 优化性能:使用针对特定类型的数据(例如关系型与非关系型)优化的不同数据库。
  • 数据隔离:出于安全、合规或组织原因分离数据。
  • 可扩展性:在不同的数据库之间分配数据负载以提高性能。

3.设置 Spring Boot 项目

首先,创建一个新的 Spring Boot 项目。您可以使用 Spring Initializr 或您喜欢的 IDE 来设置项目。

Maven 依赖项

在您的 中pom.xml包含 Spring Data JPA 和您将使用的数据库的依赖项(例如,内存中的 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.配置多个数据源

application.ymlapplication.properties文件中,配置每个数据库的连接属性。

application.yml

spring:
  datasource:
    primary:
      url: jdbc:h2:mem:primarydb
      driver-class-name: org.h2.Driver
      username: sa
      password: password
    secondary:
      url: jdbc:postgresql://localhost:5432/secondarydb
      driver-class-name: org.postgresql.Driver
      username: postgres
      password: password
jpa:
    primary:
      database-platform: org.hibernate.dialect.H2Dialect
      hibernate:
        ddl-auto: update
    secondary:
      database-platform: org.hibernate.dialect.PostgreSQLDialect
      hibernate:
        ddl-auto: update

5.创建数据源配置类

接下来,为每个数据源创建单独的配置类。

主数据源配置

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);
    }
}

辅助数据源配置

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. 定义实体管理器

为每个数据库定义实体类。确保将它们放在配置类中指定的相应包中。

主数据库实体

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
}

辅助数据库实体

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. 创建存储库

为每个数据库创建存储库接口,确保它们按照配置放置在正确的包中。

主存储库

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.测试配置

最后,创建一个简单的 REST 控制器来测试设置。此控制器将使用两个存储库来执行 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 {
    @Autowired
    private PrimaryRepository primaryRepository;
    @Autowired
    private 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!";
    }
}

以上就是在SpringBoot微服务中设置和管理多个数据库的代码示例的详细内容,更多关于SpringBoot微服务设置和管理数据库的资料请关注脚本之家其它相关文章!

相关文章

  • nacos配置读取实现过程

    nacos配置读取实现过程

    本文介绍了在Spring Boot应用中如何指定和使用配置文件,创建配置类读取Nacos配置,并通过接口返回给前端,同时使用@RefreshScope实现配置实时刷新
    2026-01-01
  • JAVA使用TreeMap对字符串进行排序

    JAVA使用TreeMap对字符串进行排序

    这篇文章主要介绍了JAVA使用TreeMap对字符串进行排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Java 面向对象通过new揭开对象实例化

    Java 面向对象通过new揭开对象实例化

    各位铁汁们大家好呀,我们上次博客讲了,通过 Student student1 = new Student();就可以实例化一个对象,这个对象就有Student类中的所以成员变量。可是 对象student1 和 类Student到底是怎样建立联系的,在内存中到底发生了什么
    2022-04-04
  • 老生常谈java中的fail-fast机制

    老生常谈java中的fail-fast机制

    下面小编就为大家带来一篇老生常谈java中的fail-fast机制。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Java中this和super关键字的使用详解

    Java中this和super关键字的使用详解

    super 代表父类的存储空间标识(可以理解为父亲的引用)。 this代表当前对象的引用(谁调用就代表谁)。本文将通过简单的示例介绍二者的使用与区别,需要的可以了解一下
    2022-10-10
  • 原来Java接口多实现还可以这样玩

    原来Java接口多实现还可以这样玩

    JAVA中类不直接支持多继承,因为会出现调用的不确定性,所以JAVA将多继承机制进行改良,在JAVA中变成了多实现,这篇文章主要给大家介绍了关于Java接口多实现的相关资料,需要的朋友可以参考下
    2021-09-09
  • SpringBoot中TypeExcludeFilter的作用及使用方式

    SpringBoot中TypeExcludeFilter的作用及使用方式

    在SpringBoot应用程序中,TypeExcludeFilter通过过滤特定类型的组件,使它们不被自动扫描和注册为bean,这在排除不必要的组件或特定实现类时非常有用,通过创建自定义过滤器并注册到spring.factories文件中,我们可以在应用启动时生效
    2025-01-01
  • 详解Spring AOP 实现“切面式”valid校验

    详解Spring AOP 实现“切面式”valid校验

    本篇文章主要介绍了详解Spring AOP 实现“切面式”valid校验,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Javase随机数类、日期类、包装类的基础知识示例详解

    Javase随机数类、日期类、包装类的基础知识示例详解

    我们在开发时,除了操作一些固定的数字之外,有时候还要操作一些不确定的随机数,这篇文章主要介绍了Javase随机数类、日期类、包装类的基础知识,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-09-09
  • 解决eclipse启动tomcat时不能加载web项目的问题

    解决eclipse启动tomcat时不能加载web项目的问题

    这篇文章主要介绍了解决eclipse启动tomcat时不能加载web项目的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06

最新评论