SpringCloud---(10)Spring Cloud Config(配置中心)

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。


访问配置文件的格式:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties


命中默认的配置application.yml(优先级高):
http://localhost:8080/master/xxx-default.yml

命中设置的配置foobar-dev.yml
http://localhost:8080/master/foobar-dev.yml

查看默认的配置
http://localhost:8080/xxx/dedault/master

查看设置的配置
http://localhost:8080/foobar/dev/master

bootstrap.*
里面的配置   ---->连接Config server,加载远程配置    --->加载application.*里面的配置

 

10.1 Config连接git的服务端

  1. 在讲到配置时,不得不说 SpringCloud 提供了一套解决分布式的配置管理方案,
    它既包含了服务端
    ConfigServer也包含了客户端ConfigClient
  2. SpringCloud 将配置文件当作源代码一样存储到 git 或者 svn 服务器上,
    虽然说这样没有什么管理界面配置啥的,既然能用
    svn 上传上去,那也能做成管理界面,
  3. 只是花的工作量多少而已了,而既然都说了是配置,那就是只要会稍微学些git或者svn的提交文件方式,
    基本上任何都极易掌握;
  4. 当我们把配置文件放在 git 上的时候,我们如果要做到更新的话,
    我们需要借助于
    git 网页上的 push 操作来触发更新操作;
  5. 该项目实现 ConfigServer 是如何和 Git 服务器相关联存储配置。

10.1.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>
</project>

10.1.2 application.yml

server:
  port: 8080  #指定服务端口
spring:
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo

10.1.3 ConfigServerApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   SpringCloudConfig的服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("ConfigServer启动...");
    }

}

10.2 Config连接git的客户端

 

  1. 有配置服务端,那么势必就会有与之对应的客户端,SpringCloud 文档中集成也非常简单;
  2. 但是这里有点需要注意,就是 bootstrap 配置文件,
    官方建议我们在
    bootstrap中放置不更改的属性,我们同样也需要在这里做一些简单不易于改变的配置;
  3. 这里还顺便列举下配置路径的规则:
    配置服务的路径规则:


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

10.2.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud config的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- springboot的web依赖包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.2.2 bootstrap.yml

spring:
  cloud:
    config:
     #配置服务客户端ClientServer的连接入口
      uri: http://localhost:8080
      profile: dev
      label: master #当configserver后端存储为git时默认为master
  application:
   #取foobar-dev.yml这个文件的application名字时,即为foobar
    name: foobar

10.2.3 application.yml

server:
  port: 8081
#测试一:配置服务客户端Client应用入口(正常测试 ConfigClient )
#prifile: profile-fev(local)

# 测试二:配置服务客户端Client应用入口(链接 ClientServer 测试)
#spring:
#  cloud:
#    config:
#      uri: http://localhost:8082
#      profile: dev
#      label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
#
#  application:
#    name: foobar  #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称

# 测试三:配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
#profile: profile-local-dev

10.2.4 ConfigClientApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @title   配置客户端ConfigClient接入配置服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
        System.out.println("ConfigClient启动...");
    }

}

10.2.5 ConfigClientController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

10.2.6 测试

Spring Cloud Config连接git的服务端,端口(8080
Spring Cloud Config连接git的客户端,端口(8081
    测试:
http://localhost:8081/profile(更多测试看microservice-config-serverREADME.md
   

 

10.3 服务端对配置文件进行对称加解密

 

配置服务端ClientServer对配置文件内容进行对称加解密

1、存储在Git上面的内容为明文,在生产环境的话,也不利于传输,特别一些重要的信息容易被泄露;
2、所以需要对文件的内容进行加密、解密,有利于内容在网络中的安全传输;

10.3.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>

</project>

10.3.2 application.yml

server:
  port: 8010  #指定服务端口

spring:
  application:
    name: microservice-config-server-encrypt
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo
          username:
          password:
encrypt:
  #加密时需要使用
  key: foo

10.3.3 ConfigServerEncryptApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   ConfigServer服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
public class ConfigServerEncryptApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerEncryptApplication.class, args);
        System.out.println("ConfigServerEncrypt启动...");
    }

}

 

10.4 客户端对配置文件进行对称加解密

 

配置客户端ConfigClient链接经过对称加解密的配置微服务

1Git服务端的文件内容进行了加密处理,那么是不是配置客户端拿到内容之后需要解密呢?
2、答案显然不是的,因为这样解密的话,先不说实现起来的难易程度,单从表面上来讲,
若是加解密频繁换的话,那客户端是不是每次都得升级解密算法呢?

3、而 SpringCloud 配置客户端不需要做什么加解密的配置,加解密的配置在服务端做就好了;

10.4.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

10.4.2 bootstrap.yml

server:
  port: 8011  #指定服务端口
  #配置服务客户端Client应用入口,链接ConfigServer入口
spring:
  cloud:
    config:
      #链接configServer服务的入口
      uri: http://localhost:8180
      #选择生产配置文件
      profile: prd
      #后端存储是git时,默认使用的是master
      label: master
encrypt:
  #取foobar-dev.yml文件的application名字,即为foobar
  key: foo

10.4.3 application.yml

server:
  port: 8012  #指定服务端口

10.4.4 测试

Spring Cloud Config加解密对称的服务端,端口(8010
Spring Cloud Config加解密对称的客户端,端口(8012
    测试:
http://localhost:8012/profile
   

10.5 服务端对配置文件进行非对称RSA加解密

 

配置服务端ClientServer对配置文件内容进行非对称RSA加解密

10.5.1 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.0</modelVersion>

    <artifactId>microservice-config-server-encrypt-rsa</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>

</project>

10.5.2 application.yml

server:
  port: 8020  #指定服务端口

spring:
  application:
    name: microservice-config-server-encrypt-rsa
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo
          username:
          password:
encrypt:
  key-store:
    location: classpath:/server-rsa.jks
    password: 123456
    alias: mytest
    secret: a123456 #私钥密码

10.5.3 ConfigServerEncryptRsaApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   ConfigServer服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
public class ConfigServerEncryptRsaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerEncryptRsaApplication.class, args);
        System.out.println("ConfigServerEncryptRsa启动...");
    }

}

 

10.6 客户端对配置文件进行非对称RSA加解密

 

配置客户端ConfigClient链接经过对称加解密的配置微服务

配置客户端不需要做任何加解密的配置,加解密的配置在服务端做就好了

配置中加密和解密:
例如:
foobar
加密
curl -X POST http://localhost:8080/encrypt -d foobar
解密
curl -X POST http://localhost:8080/decrypt -d 4126ff42a0e70b88be94f0f782
fb7296a985f7cf3b3f9eb0ce01f268d5408e77

 

10.6.1 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.0</modelVersion>

    <artifactId>microservice-config-client-encrypt-rsa</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

10.6.2 bootstrap.yml

server:
  port: 8021  #指定服务端口
  #配置服务客户端Client应用入口,链接ConfigServer入口
spring:
  cloud:
    config:
      #链接configServer服务的入口
      uri: http://localhost:8280
      #选择生产配置文件
      profile: stg1rsa #
      #后端存储是git时,默认使用的是master
      label: master
encrypt:
  #取foobar-dev.yml文件的application名字,即为foobar
  key: foo

10.6.3 application.yml

server:
  port: 8022  #指定服务端口

10.6.4 ConfigClientEncryptRsaApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @title   配置客户端ConfigClient链接经过对称加解密的微服务配置
 *      专门为测试经过对称加解密的配置微服务
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
public class ConfigClientEncryptRsaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientEncryptRsaApplication.class, args);
        System.out.println("ConfigClientEncryptRsa启动...");
    }

}

10.6.5 ConfigClientEncryptController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
public class ConfigClientEncryptController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

10.6.6 测试

Spring Cloud Config加解密非对称RSA的服务端,端口(8020
Spring Cloud Config加解密非对称RSA的客户端,端口(8022
    测试:
http://localhost:8022/profile

 

10.7 服务端设置安全认证

 

配置服务端ConfigServer设置安全认证

虽然之前对内容进行了加密,但是为了更安全的安全隔离服务与服务之间也需要设置简单的安全认证。

 

10.7.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.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-security</artifactId>
        </dependency>
    </dependencies>

</project>

10.7.2 application.yml

server:
  port: 8380  #指定服务端口
spring:
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo
#配置登录密码
security:
  basic:
    enabled: true
  user:
    name: kevin
    password: 123456

 

10.7.3 ConfigServerAuthcApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   配置服务端ConfigServer设置安全认证
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
public class ConfigServerAuthcApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerAuthcApplication.class, args);
        System.out.println("ConfigServerAuthc启动...");
    }

}

 

10.8 客户端设置安全认证

 

配置服务客户端ConfigClient连接经过认证的配置服务端

该项目设置如链接上服务端的认证

 

10.8.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud config的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- springboot的web依赖包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

</project>

10.8.2 bootstrap.yml

spring:
  cloud:
    config:
     #配置服务客户端ClientServer的连接入口
      uri: http://localhost:8380
      #username、password 属性字段的优先级高于 uri 的优先级
      username: kevin
      password: 123456
      profile: dev
      label: master #当configserver后端存储为git时默认为master
  application:
   #取foobar-dev.yml这个文件的application名字时,即为foobar
    name: foobar

 

10.8.3 application.yml

server:
  port: 8381
#测试一:配置服务客户端Client应用入口(正常测试 ConfigClient )
#prifile: profile-fev(local)

# 测试二:配置服务客户端Client应用入口(链接 ClientServer 测试)
#spring:
#  cloud:
#    config:
#      uri: http://localhost:8082
#      profile: dev
#      label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
#
#  application:
#    name: foobar  #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称

# 测试三:配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
#profile: profile-local-dev

 

10.8.4 ConfigClientAuthcApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @title   配置客户端ConfigClient接入配置服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
public class ConfigClientAuthcApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientAuthcApplication.class, args);
        System.out.println("ConfigClientAuthc启动...");
    }

}

 

10.8.5 ConfigClientAuthcController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
public class ConfigClientAuthcController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

10.8.6 测试

 

Spring Cloud Config使用安全认证的服务端,端口(8030
Spring Cloud Config使用安全认证的客户端,端口(8031
    测试:
http://localhost:8031/profile

 

10.9 Config与Eureka配合使用的服务端

 

SpringCloudConfigEureka配合使用的服务端

 

10.9.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.9.2 application.yml

server:
  port: 8040  #指定服务端口
spring:
  application:
    name: microservice-config-server-eureka
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo
          username:
          password:
eureka:
  client:
    #注册中心地址
    service-url:
      defaultZone: http://kevin:123456@localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    #instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

 

10.9.3 ConfigServerEurekaApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   EurekaServer结合Eureka
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
@EnableDiscoveryClient
public class ConfigServerEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerEurekaApplication.class, args);
        System.out.println("ConfigServerEureka启动...");
    }

}

 

10.10 Config与Eureka配合使用的客户端

 

SpringCloudConfigEureka配合使用的客户端

 

10.10.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud config的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- springboot的web依赖包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.10.2 bootstrap.yml

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: MICROSERVICE-CONFIG-SERVER-EUREKA
  application:
   #取foobar-dev.yml这个文件的application名字时,即为foobar
    name: foobar
eureka:
  client:
    #注册中心地址
    service-url:
      defaultZone: http://kevin:123456@localhost:8761/eureka/
  instance:
    prefer-ip-address: true

 

10.10.3 application.yml

server:
  port: 8041
#测试一:配置服务客户端Client应用入口(正常测试 ConfigClient )
#prifile: profile-fev(local)

# 测试二:配置服务客户端Client应用入口(链接 ClientServer 测试)
#spring:
#  cloud:
#    config:
#      uri: http://localhost:8082
#      profile: dev
#      label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
#
#  application:
#    name: foobar  #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称

# 测试三:配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
#profile: profile-local-dev

 

10.10.4 ConfigClientEurekaApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 *
 * @title   配置客户端ConfigClient接入配置服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableDiscoveryClient
public class ConfigClientEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientEurekaApplication.class, args);
        System.out.println("ConfigClient启动...");
    }

}

 

10.10.5 ConfigClientController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

10.10.6 测试

Spring Cloud ConfigEureka配合使用的服务端,端口(8040
Spring Cloud ConfigEureka配合使用的客户端,端口(8041
    测试:
http://localhost:8041/profile

 

10.11 Config手动刷新配置的客户端

 

单点手动动态刷新ConfigClient配置

1、当ConfigServer启动后,假如我们新增配置内容的话,是不是要重新启动一下ConfigServer呢?
2、答案肯定是不需要重新启动的,因为 SpringCloud 给我们提供了一个刷新的触发机制,
这样便可以在不重新的情况下重新加载最新配置文件内容;

通过
curl手动刷新

curl -X POST http://localhost:8082/refresh

 

10.11.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
         <!-- 客户端配置模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 监控和管理生产环境的模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.11.2 bootstrap.yml

spring:
  cloud:
    config:
     #配置服务客户端ClientServer的连接入口
      uri: http://localhost:8080
      profile: dev  #文件后缀名的前个单词
      label: master #当configserver后端存储为git时默认为master
  application:
   #取foobar-dev.yml这个文件的application名字时,即为foobar
    name: foobar

 

10.11.3 application.yml

server:
  port: 8082

# 配置服务客户端Client应用入口(正常测试 ConfigClient )
# profile: profile-dev

#配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
# profile: profile-local-dev

 

10.11.4 ConfigClientRefreshApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @title   配置客户端ConfigClient接入配置服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
public class ConfigClientRefreshApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientRefreshApplication.class, args);
        System.out.println("ConfigClientRefresh启动...");
    }

}

 

10.11.5 ConfigClientController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

10.11.6 测试

1.启动10.1中的Config服务端

2.再启动该项目

Spring Cloud Config之手动刷新,端口(8082),一次只能刷新一个客户端
    测试:
curl -X POST http://localhost:8082/refresh
        http://localhost:8082/profile

 

10.12 Config半自动刷新配置的客户端

 

通过bus/refresh半自动刷新ConfigClient配置

1、之前讲到了手动刷新配置,但是如果微服务多的话,那么是不是需要对每台服务进行手动刷新呢?
2、答案肯定是不需要的,我们也可以采用 rabbitmq 消息中间件产品来增强刷新机制;

10.12.1 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.0</modelVersion>

    <artifactId>microservice-config-client-refresh-bus</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 客户端配置模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 监控和管理生产环境的模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Rabbitmq模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.12.2 bootstrap.yml

spring:
  cloud:
    config:
     #配置服务客户端ClientServer的连接入口
      uri: http://localhost:8080
      profile: dev
      label: master #当configserver后端存储为git时默认为master
    bus:
      trace:
        enabled: true
  application:
   #取foobar-dev.yml这个文件的application名字时,即为foobar
    name: foobar
  #连接MQ
  rabbitmq:
    host: localhost   # 登录 Rabbitmq 后台管理页面地址为:http://localhost:15672
    port: 5672
    username: guest   # 默认账户
    password: guest   # 默认密码

 

10.12.3 application.yml

server:
  port: 8084

# 配置服务客户端Client应用入口(正常测试 ConfigClient )
# profile: profile-dev

#配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
# profile: profile-local-dev

 

10.12.4 ConfigClientRefreshBusApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @title   配置客户端ConfigClient接入配置服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
public class ConfigClientRefreshBusApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientRefreshBusApplication.class, args);
        System.out.println("ConfigClientRefreshBus启动...");
    }

}

 

10.12.5 ConfigClientController.java

package com.kevin.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin
 * @version 1.0
 * @title   配置客户端Controller
 * @description
 * @createDate 2018/12/4
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

10.13 Config半自动刷新配置的服务端

 

10.13.1 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.0</modelVersion>

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

    <parent>
        <groupId>com.kevin.cloud</groupId>
        <artifactId>config-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- springcloud的依赖包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!-- Rabbitmq模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

    </dependencies>

</project>

 

10.13.2 application.yml

server:
  port: 8080  #指定服务端口

spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
        #git的项目地址
          uri: https://gitee.com/LoneTraveler/kevin-config-repo
          username:
          password:
  #连接MQ
  rabbitmq:
    host: localhost   # 登录 Rabbitmq 后台管理页面地址为:http://localhost:15672
    port: 5672
    username: guest   # 默认账户
    password: guest   # 默认密码
encrypt:
  key: foo

 

10.13.3 ConfigServerApplication.java

package com.kevin.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 *
 * @title   SpringCloudConfig的服务端
 * @description
 * @author caonanqing
 * @createDate 2018/11/7
 * @version 1.0
 */
@SpringBootApplication  //设为springboot
@EnableConfigServer     //实现服务发现,注册
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("ConfigServer启动...");
    }

}

 

10.13.4 测试

 

步骤:
Erlang
安装MQ
在命令行输入该命令可以进去监控页面
rabbitmq-plugins enable rabbitmq_management
进入监控页面:http://localhost:15672
账号和密码都是:guest
curl
curl -X POST http://localhost:8083/bus/refresh
执行半自动刷新,可以起两个端口作为批量测试(批量刷新)
    测试:
localhost:8083/profile
   
测试:localhost:8084/profile

如果想要自动刷新,需要到码云上添加Web HooksURL,密码随意设置
   
http://192.168.0.104:8083/bus/refresh   (外网IP)
   
测试:localhost:8083/profile
   
注:在自动刷新的时候,将refresh的执行放到某一个Client不是很合理,
    我们可以放到
server中,让server来负责其刷新

 

Spring Cloud Config之半自动刷新,端口(8083),可以多个端口,测试批量刷新
    测试:
curl -X POST http://localhost:8083/bus/refresh(随意选择一个客户端对所有的客户端进行半自动刷新)
   
http://localhost:8083/profile
    http://localhost:8084/profile

如果想要自动刷新,需要到码云上添加Web HooksURL,密码随意设置  

 

10.14 所有的测试连接

Spring Cloud Config连接git的服务端,端口(8080)
Spring Cloud Config连接git的客户端,端口(8081)
    测试:http://localhost:8081/profile(更多测试看microservice-config-server的README.md)
    
Spring Cloud Config加解密对称的服务端,端口(8010)
Spring Cloud Config加解密对称的客户端,端口(8012)
    测试:http://localhost:8012/profile
    
Spring Cloud Config加解密非对称RSA的服务端,端口(8020)
Spring Cloud Config加解密非对称RSA的客户端,端口(8022)
    测试:http://localhost:8022/profile
    
Spring Cloud Config使用安全认证的服务端,端口(8030)
Spring Cloud Config使用安全认证的客户端,端口(8031)
    测试:http://localhost:8031/profile
    
Spring Cloud Config与Eureka配合使用的服务端,端口(8040)
Spring Cloud Config与Eureka配合使用的客户端,端口(8041)
    测试:http://localhost:8041/profile
    
Spring Cloud Config之手动刷新,端口(8082),一次只能刷新一个客户端
    测试:curl -X POST http://localhost:8082/refresh
        http://localhost:8082/profile
        
Spring Cloud Config之半自动刷新,端口(8083),可以多个端口,测试批量刷新
    测试:curl -X POST http://localhost:8083/bus/refresh(随意选择一个客户端对所有的客户端进行半自动刷新)
    http://localhost:8083/profile
    http://localhost:8084/profile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值