spring-分布式配置中心

Posted by Chen Quan on July 25, 2019

spring-分布式配置中心

一、搭建配置服务中心(config server)

首先通过IDEA spring initializer(或直接通过https://start.spring.io/)创建一个spring boot项目(demo项目命名:configserver),创建过程中选择:config server依赖,生成项目后的POM XML文件如下:(如果缺少依赖请自行手动添加)

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>configserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configserver</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
	<!--分布式配置中心-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

git方式

1. 编写分布式配置文件

配置文件命名规则应尽可能使用:{spring.application.name}-{profile}.{properties yml},例如person服务的dev模式配置文件可命名为:person-dev.yml
spring:
  jpa:
    database: postgresql
    properties:
      hibernate:
        temp.use_jdbc_metadata_defaults: false
  datasource:
    url: jdbc:postgresql://192.168.188.141:5432/postgres
    platform: postgres
    username: postgres
    password: 13882
    driver-class-name: org.postgresql.Driver

2.配置启动类

在spring boot启动类(ConfigserverApplication)上添加@EnableConfigServer注解,代码如下:

@EnableConfigServer
@SpringBootApplication
public class ConfigserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigserverApplication.class, args);
    }

}

3.编写application.{yml|properties}

spring:
  application:
    name: configserver
  profiles:
    active: git #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
  cloud:
    config:
      server:
        #如下是GIT配置
        git:
          uri: https://gitee.com/didispace/config-repo-demo   # 配置git仓库的地址(最后不需要带/,否则会出现:No custom http config found for URL: XXX)
          search-paths: repo     # git仓库地址下的相对搜索地址(可用使用通配符),可以配置多个,用,分割。可以{application}实现按应用查配置
          username:   # git仓库的账号(公开仓库无需账号信息)
          password:   # git仓库的密码(公开仓库无需账号信息)
          default-label: master         #git默认分支

 通过上述简单的两步即完成搭建基于GIT的配置服务中心,启动运行项目,GIT仓库中的配置文件会被自动转换成当前项目的WEB API,若需访问查看远程配置数据可以参照以下的规则:

/{application}/{profile}[/{label}]
[/{label}]/{application}-{profile}{.yml|.properties|.json}
规则简单说明:{application}=配置消费方应用名称(即:config client的项目名,通俗讲:就是谁用这个配置就是谁的名字),{profile}=配置环境(如:dev开发环境,test测试环境,prod生产环境),{label}=仓库分支名(git或svn方式指定,native本地方式无需指定),.yml .properties .json表示指定的响应返回格式,{}表示必需,[]表示可选, 表示或的关系

svn方式

1. 添加额外依赖

需要在pom.xml文件中引入如下依赖:

 <!--spring cloud配置中心依赖[默认GIT]-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--使用SVN作为配置中心依赖-->
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
        </dependency>

2. 配置文件(application.yml)

server:
  port: 8866

spring:
  application:
    name: configserver
  profiles:
    active: subversion #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
  cloud:
    config:
      server:
        #如下是SVN配置
        svn:
          uri: http://svnhost:port/svn/app-config #SVN仓库地址
          username: svnuser #SVN账号(如果没有权限可为空)
          password: svnpassword #SVN密码(如果没有权限可为空)
          default-label: trunk #默认SVN分支

其余同Git一样。

本地文件方式

1. 编写分布式配置文件

先在config server项目文件服务器创建指定存放配置的目录,直接创建在项目的resources/configs目录下

2. 配置文件(application.yml)

server:
  port: 8866

spring:
  application:
    name: configserver
  profiles:
    active: native #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
  cloud:
    config:
      server:
        #如下是本地文件配置
        native:
          search-locations: classpath:/configs #配置文件存放的目录