第六篇:配置中心(Spring Cloud Config)

时间:2020-03-07
本文章向大家介绍第六篇:配置中心(Spring Cloud Config),主要包括第六篇:配置中心(Spring Cloud Config)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.为什么要用配置中心

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

Spring Cloud Config支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中,可以通过git修改。

在Spring Cloud Config组件中,有两个是重要的服务,一是config server,二是config client。

2.搭建Spring Cloud Config配置中心

新创建一个Maven父工程,父工程pom文件如下

<?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>

    <groupId>com.niuben</groupId>
    <artifactId>spring-cloud-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <!--父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <!--属性-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

    <!--版本管理-->
    <dependencyManagement>
        <dependencies>
            <!--springcloud-->
            <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>

    <!--Maven打包工具-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

构建config-server

新创建model,config-server工程

加载pom文件

<?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">
    <parent>
        <artifactId>spring-cloud-config</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-config依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包工具-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写配置文件application.properties

spring.application.name=config-server
server.port=8888

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/niugit/test01/
# 配置仓库路径
spring.cloud.config.server.git.searchPaths=test
# 配置仓库的分支
spring.cloud.config.label=master
# 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
# 访问git仓库的用户名
#spring.cloud.config.server.git.username=
# 访问git仓库的用户密码
#spring.cloud.config.server.git.password=
  • 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仓库的用户密码

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。

在启动类加@EnableConfigServer注解,开启配置服务器

@SpringBootApplication
@EnableConfigServer // 开启配置中心 服务
public class ConfigServerApplication {

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

}

在远程仓库https://gitee.com/niugit/test01/中有一个test文件,文件下有config-client-dev.properties配置文件,其中有两个属性

foo=now is 100
democonfigclient.message=hello spring cloud config!

启动程序:访问http://localhost:8888/config-client/dev,会在页面出现

{"name":"config-client","profiles":["dev"],"label":null,"version":"441761abcdd8e01c6f063cc873857728db333bce","state":null,"propertySources":[{"name":"https://gitee.com/niugit/test01//test/config-client-dev.properties","source":{"foo":"now is 100","democonfigclient.message":"hello spring cloud config!"}}]}

表明配置服务中心可以从远程程序获取配置信息了

注:http请求地址和资源文件映射如下

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

构建config-client

创建新model,config-client工程

<?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">
    <parent>
        <artifactId>spring-cloud-config</artifactId>
        <groupId>com.niuben</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-starter-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

    <!--Maven打包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写bootstrap.properties配置文件

spring.application.name=config-client
# 远程仓库的分支
spring.cloud.config.label=master
# dev开发环境配置文件,test测试环境,pro正式环境
spring.cloud.config.profile=dev
# 配置服务中心的网址
spring.cloud.config.uri= http://localhost:8888/
server.port=8881

创建controller层,TestClient类

@RestController
public class TestClient {

    @Value("${foo}")
    String foo;

    @RequestMapping(value = "/test")
    public String hi() {
        return foo;
    }
}

启动启动类,访问:http://localhost:8881/test,出现

now is 100

表明config-client从config-server获取了foo的属性,而config-server是从git仓库读取的

源码:https://gitee.com/niugit/spring-cloud-config

原文地址:https://www.cnblogs.com/niudaben/p/12436857.html