Spring Cloud Config(分布式配置中心)使用git作为远程配置中心连接数据库配置

为什么要用SpringCloud分布式配置中心?

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
在SpringCloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
在spring cloud config组件中,分两个角色,config server (服务端) 和 config client (客户端)。

config server (服务端) 端 pom.xml 配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>cn.etgroupId>
    <artifactId>SpringCouldartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
    parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Dalston.SR5version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
        dependency>
    dependencies>
project>

application.properties 配置

spring.application.name=config-server
server.port=8089
spring.cloud.config.server.git.uri=https://github.com/UserName/SpringCloud
spring.cloud.config.server.git.searchPaths=/jdbc.properties
spring.cloud.config.server.git.username=UserName
spring.cloud.config.server.git.password=Password

server.port:设置端口 spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径
spring.cloud.config.label:配置仓库的分支(可略)
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码

Main.java(入口) 启动服务器


@EnableConfigServer
@SpringBootApplication
public class AppLication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(AppLication.class).web(true).run(args);
    }
}

config client (客户端) 端 pom.xml 配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>cn.etgroupId>
    <artifactId>SRPINGCLOUD-CONFIGCLIENTartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
    parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Dalston.SR4version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.44version>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.5version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
    dependencies>
project>

bootstarp.properties 配置

spring.cloud.config.uri=http://localhost:8089/
spring.application.name=jdbc
spring.cloud.config.profile=pro

spring.application.name=jdbc #数据库连接配置
spring.cloud.config.uri=http://localhost:8089/ #指明配置服务中心的网址
spring.cloud.config.label#指明远程仓库的分支
spring.cloud.config.profile
- dev开发环境配置文件
- test测试环境
- pro正式环境

application.properties 配置

spring.jpa.show-sql=true
server.port=80
server.context-path=/sb #设置上下文路径(可略)
spring.devtools.restart.enabled=true

获取数据源 TestController.java

@Configuration
public class TestController {
    @Value("${url}")
    private String url;
    @Value("$driverClass")
    private String driverClass;
    @Value("userid")
    private String userid;
    @Value("password")
    private String password;
    @Bean
    //获取数据源
    public DataSource getDataSource(){
        //数据库连接池
        DruidDataSource dds = new DruidDataSource();
        dds.setUrl(url);
        dds.setDriverClassName(driverClass);
        dds.setUsername(userid);
        dds.setPassword(password);
        return dds;
    }
}

Main.java(入口)启动客户端

@SpringBootApplication
public class AppLication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(AppLication.class).web(true);
    }
}

这样一个简单的SpringCloud分布式配置中心就完成了。

你可能感兴趣的:(spring)