Spring Cloud Config微研

转自:https://www.pianshen.com/article/74702246/

spring cloud config server 官方文档在

http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html

也有中文版的。

1. Quick Start

1.1.建立一个config sever项目

pom.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
     
  6.  
    <groupId>com.cloud.shj</groupId>
  7.  
    <artifactId>config-server</artifactId>
  8.  
    <version>0.0.1-SNAPSHOT</version>
  9.  
     
  10.  
    <packaging>jar</packaging>
  11.  
     
  12.  
    <name>SpringCloud-Config</name>
  13.  
    <description>Demo project for Spring Boot</description>
  14.  
     
  15.  
    <parent>
  16.  
    <groupId>org.springframework.boot</groupId>
  17.  
    <artifactId>spring-boot-starter-parent</artifactId>
  18.  
    <version>2.0.5.RELEASE</version>
  19.  
    <relativePath/> <!-- lookup parent from repository -->
  20.  
    </parent>
  21.  
     
  22.  
    <properties>
  23.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  24.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  25.  
    <java.version>1.8</java.version>
  26.  
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  27.  
    </properties>
  28.  
     
  29.  
    <dependencyManagement>
  30.  
    <dependencies>
  31.  
    <dependency>
  32.  
    <groupId>org.springframework.cloud</groupId>
  33.  
    <artifactId>spring-cloud-dependencies</artifactId>
  34.  
    <version>${spring-cloud.version}</version>
  35.  
    <type>pom</type>
  36.  
    <scope>import</scope>
  37.  
    </dependency>
  38.  
    </dependencies>
  39.  
    </dependencyManagement>
  40.  
     
  41.  
    <dependencies>
  42.  
    <dependency>
  43.  
    <groupId>org.springframework.cloud</groupId>
  44.  
    <artifactId>spring-cloud-config-server</artifactId>
  45.  
    </dependency>
  46.  
    </dependencies>
  47.  
     
  48.  
     
  49.  
     
  50.  
    <repositories>
  51.  
    <repository>
  52.  
    <id>spring-releases</id>
  53.  
    <url>https://repo.spring.io/libs-release</url>
  54.  
    </repository>
  55.  
    </repositories>
  56.  
     
  57.  
     
  58.  
     
  59.  
    </project>

pom.xml

  1.  
    server:
  2.  
    port: 7010 #端口
  3.  
    spring:
  4.  
    application:
  5.  
    name: server-config
  6.  
    cloud:
  7.  
    config:
  8.  
    server:
  9.  
    git:
  10.  
    uri: https://github.com/shaweiwei/myspringcloudconfig/ #Spring Cloud Config服务器从git存储库中提取远程客户端的配置(必须提供)
  11.  
    search-paths: myconfigpath #搜索文件目录
  12.  
    label: master

 主启动类

  1.  
    package com.example.springcloudconfig;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.config.server.EnableConfigServer;
  6.  
     
  7.  
    @SpringBootApplication
  8.  
    @EnableConfigServer
  9.  
    public class SpringCloudConfigApplication {
  10.  
     
  11.  
    public static void main(String[] args) {
  12.  
     
  13.  
    SpringApplication.run(SpringCloudConfigApplication.class, args);
  14.  
    }
  15.  
    }

注意:

1.config server的git服务器,如果是公共的,用户名和密码就不用设置,如果是私有的,需要设置用户名和密码。

访问地址https://github.com/shaweiwei/myspringcloudconfig/可以直进入下面的页面。

打开文件夹“myconfigpath",可以看到很多properties文件

1.2.尝试一个客户端

  1.  
    C:Userslunmei>curl localhost:7010/config-client-dev.yml
  2.  
    democonfigclient:
  3.  
    message: hello spring io
  4.  
    myww: myww version 2

http服务具有以下服务的资源

  1.  
    /{application}/{profile}[/{label}]
  2.  
    /{application}-{profile}.yml
  3.  
    /{label}/{application}-{profile}.yml
  4.  
    /{application}-{profile}.properties
  5.  
    /{label}/{application}-{profile}.properties

什么意思?比如,对于上面的文件config-client-dev.properties,打开后内容是

application:config-client
profile:dev
label:master

  1.  
    C:Userslunmei>curl localhost:7010/config-client-dev.yml
  2.  
    democonfigclient:
  3.  
    message: hello spring io
  4.  
    myww: myww version 2
  5.  
    C:Userslunmei>curl localhost:7010/config-client-dev.properties
  6.  
    democonfigclient.message: hello spring io
  7.  
    myww: myww version 2
  8.  
    C:Userslunmei>curl localhost:7010/master/config-client-dev.properties
  9.  
    democonfigclient.message: hello spring io
  10.  
    myww: myww version 2
  11.  
    C:Userslunmei>curl localhost:7010/master/config-client-dev.yml
  12.  
    democonfigclient:
  13.  
    message: hello spring io
  14.  
    myww: myww version 2

类似的

  1.  
    C:Userslunmei>curl localhost:7010/service-config/dev/master
  2.  
    {"name":"service-config","profiles":["dev"],"label":"master","version":"38bd83c975164ec6f1ce10343fe346c0022db3fa","state":null,"propertySources":[{"name":"https://github.com/shaweiwei/myspringcloudconfig//myconfigpath/service-config-dev.properties","source":{"ip":"192.168.30.51","port":"7777"}}]}

1.3.config client端写法

pom.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  
    <modelVersion>4.0.0</modelVersion>
  5.  
     
  6.  
    <groupId>com.example</groupId>
  7.  
    <artifactId>config-client</artifactId>
  8.  
    <version>0.0.1-SNAPSHOT</version>
  9.  
    <packaging>jar</packaging>
  10.  
     
  11.  
    <name>config-client</name>
  12.  
    <description>Demo project for Spring Boot</description>
  13.  
     
  14.  
    <parent>
  15.  
    <groupId>org.springframework.boot</groupId>
  16.  
    <artifactId>spring-boot-starter-parent</artifactId>
  17.  
    <version>2.0.5.RELEASE</version>
  18.  
    <relativePath/> <!-- lookup parent from repository -->
  19.  
    </parent>
  20.  
     
  21.  
    <properties>
  22.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24.  
    <java.version>1.8</java.version>
  25.  
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  26.  
    </properties>
  27.  
     
  28.  
    <dependencies>
  29.  
    <dependency>
  30.  
    <groupId>org.springframework.cloud</groupId>
  31.  
    <artifactId>spring-cloud-starter-config</artifactId>
  32.  
    </dependency>
  33.  
     
  34.  
    <dependency>
  35.  
    <groupId>org.springframework.boot</groupId>
  36.  
    <artifactId>spring-boot-starter-web</artifactId>
  37.  
    </dependency>
  38.  
     
  39.  
    <dependency>
  40.  
    <groupId>org.springframework.boot</groupId>
  41.  
    <artifactId>spring-boot-starter-test</artifactId>
  42.  
    <scope>test</scope>
  43.  
    </dependency>
  44.  
    </dependencies>
  45.  
     
  46.  
    <dependencyManagement>
  47.  
    <dependencies>
  48.  
    <dependency>
  49.  
    <groupId>org.springframework.cloud</groupId>
  50.  
    <artifactId>spring-cloud-dependencies</artifactId>
  51.  
    <version>${spring-cloud.version}</version>
  52.  
    <type>pom</type>
  53.  
    <scope>import</scope>
  54.  
    </dependency>
  55.  
    </dependencies>
  56.  
    </dependencyManagement>
  57.  
     
  58.  
    <build>
  59.  
    <plugins>
  60.  
    <plugin>
  61.  
    <groupId>org.springframework.boot</groupId>
  62.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  63.  
    </plugin>
  64.  
    </plugins>
  65.  
    </build>
  66.  
     
  67.  
     
  68.  
    </project>

bootstrap.properties

  1.  
    # 和git里的文件名对应
  2.  
    spring.application.name=config-client
  3.  
    server.port=7021
  4.  
     
  5.  
    # 指明配置服务中心的网址
  6.  
    spring.cloud.config.uri= http://localhost:7010/
  7.  
    # 远程仓库的分支
  8.  
    spring.cloud.config.label=master
  9.  
    # dev 开发环境配置文件 | test 测试环境 | pro 正式环境
  10.  
    # 和git里的文件名对应
  11.  
    spring.cloud.config.profile=dev

启动类

  1.  
    package com.example.configclient;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Value;
  4.  
    import org.springframework.boot.SpringApplication;
  5.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
    import org.springframework.web.bind.annotation.RequestMapping;
  7.  
    import org.springframework.web.bind.annotation.RestController;
  8.  
     
  9.  
    @SpringBootApplication
  10.  
    @RestController
  11.  
    public class ConfigClientApplication {
  12.  
    @Value("${myww}") // git配置文件里的key
  13.  
    String foo;
  14.  
     
  15.  
    @RequestMapping("/config")
  16.  
    public String config(){
  17.  
    return foo;
  18.  
    }
  19.  
    @RequestMapping("/a")
  20.  
    public String a(){
  21.  
    return "hello";
  22.  
    }
  23.  
     
  24.  
     
  25.  
    public static void main(String[] args) {
  26.  
    SpringApplication.run(ConfigClientApplication.class, args);
  27.  
    }
  28.  
    }

1.4.config server继承EnvironmentRepository

config client 不变,config server写法变了。使用自定义的内容。

注意官方文档中有一段说明:

环境库

您要在哪里存储配置服务器的配置数据?管理此行为的策略是EnvironmentRepository,服务于Environment对象。这个Environment是Spring Environment(包括propertySources作为主要功能)的域的浅层副本。Environment资源由三个变量参数化:

    {application}, which maps to spring.application.name on the client side.
    {profile}, which maps to spring.profiles.active on the client (comma-separated list).
    {label}, which is a server side feature labelling a "versioned" set of config files.

 
 

存储库实现通常表现得像一个Spring Boot应用程序从“spring.config.name”等于{application}参数加载配置文件,“spring.profiles.active”等于{profiles}参数。配置文件的优先级规则也与常规启动应用程序中的相同:活动配置文件优先于默认配置,如果存在多个配置文件,则最后一个配置文件(例如向Map添加条目))。

示例:客户端应用程序具有此引导配置:
bootstrap.yml

spring:
  application:
    name: foo
  profiles:
    active: dev,mysql

pom.xml

  1.  
    server:
  2.  
    port: 7010
  3.  
    spring:
  4.  
    application:
  5.  
    name: server-config
  6.  
    profiles:
  7.  
    active: mybatis #生效版本profile
  8.  
    #cloud:
  9.  
    #config:
  10.  
    #server:
  11.  
    #git:
  12.  
    #uri: https://github.com/shaweiwei/myspringcloudconfig/
  13.  
    #search-paths: myconfigpath
  14.  
    #label: master

增加配置文件

  1.  
    package com.example.springcloudconfig.config;
  2.  
     
  3.  
    import org.springframework.cloud.config.environment.Environment;
  4.  
    import org.springframework.cloud.config.environment.PropertySource;
  5.  
    import org.springframework.cloud.config.server.environment.EnvironmentRepository;
  6.  
    import org.springframework.context.annotation.Configuration;
  7.  
    import org.springframework.context.annotation.Profile;
  8.  
     
  9.  
    import java.util.HashMap;
  10.  
    import java.util.Map;
  11.  
     
  12.  
    @Configuration
  13.  
    @Profile("mybatis")
  14.  
    public class MybatisEnvironmentRepository implements EnvironmentRepository {
  15.  
     
  16.  
    /**
  17.  
    *
  18.  
    * @param application
  19.  
    * @param profile
  20.  
    * @param label
  21.  
    * @return
  22.  
    */
  23.  
    @Override
  24.  
    public Environment findOne(String application, String profile, String label) {
  25.  
     
  26.  
    //map的内容可以从数据库或其他地方获取。
  27.  
    Map<String,String> map = new HashMap<String,String>();
  28.  
    map.put("key1","key");
  29.  
    map.put("key2","key");
  30.  
    map.put("key3","key");
  31.  
    if(application.equals("config-client")){//当spring.application.name=config-client时候
  32.  
    map.put("myww","look at here");
  33.  
    }
  34.  
     
  35.  
    Environment environment = new Environment(application,new String[]{profile},label,"1.0",null);
  36.  
    environment.add(new PropertySource("application.properties",map));
  37.  
    return environment;//new Environment(application,profile);
  38.  
    }
  39.  
    }

运行后发现仍然生效

  1.  
    C:Userslunmei>curl http://localhost:7010/master/config-client-dev.yml
  2.  
    application: config-client
  3.  
    key1: key
  4.  
    key2: key
  5.  
    key3: key
  6.  
    myww: look at here
  7.  
     
  8.  
    C:Userslunmei>curl http://localhost:7010/master/other-dev.yml
  9.  
    application: config-client
  10.  
    key1: key
  11.  
    key2: key
  12.  
    key3: key

而运行了客户端后,也能看到效果

  1.  
    C:Userslunmei>curl http://localhost:7021/config
  2.  
    look at here
原文地址:https://www.cnblogs.com/sharpest/p/13712758.html