Spring Cloud Config

第一章 Spring Cloud Config介绍

Config server可以从本地读取配置文件,也可以从远程git仓库读取配置文件。

其中client端用到bootstrap.yml文件,bootstrap相对于application具有优先的执行顺序。

 

第二章 Config server从本地读取配置文件

 

2.1. Server

 

2.1.1. 依赖引入

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-config-server</artifactId>

</dependency>

2.1.2. 启动器

@SpringBootApplication

@EnableConfigServer

public class ConfigServer_StartApplication

2.1.3. Yml配置文件

 

Application.yml:

server:
  port: 8500

#config server从本地读取配置文件  
spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/clientConfig
  profiles: 
    active: native
  application:
name: config-server

Client端要读取的文件

/clientConfig/config-client-dev.yml:

server:
  port: 8501

testmsg: test message 1

2.2. Client

 

2.2.1. 依赖引入

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

2.2.2. 启动器

@SpringBootApplication
@RestController
public class ConfigClient_StartApplication {
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ConfigClient_StartApplication.class, args);
    }
    
    @Value("${testmsg}")
    String testmsg;
    
    @RequestMapping("/getTestMessage")
    public String getTestMessage() {
        return testmsg;
    }

}

2.2.3. Yml配置文件

 

bootstrap.yml:

spring:
  application:
    name: config-client  #需要与server端要读取的配置文件名匹配config-client-dev.yml({spring.application.name}-{spring.profiles.active}.yml)
  cloud:
    config:
      uri: http://localhost:8500
      fail-fast: true #如果没有读取成功,执行快速失败
  profiles:
    active: dev       #需要与server端要读取的配置文件名匹配config-client-dev.yml({spring.application.name}-{spring.profiles.active}.yml)

2.3. 测试截图

http://localhost:8501/getTestMessage

 

第三章 Config server从远程git仓库读取配置文件

 

在上面例子的基础上修改server端,client端不变。

 

3.1. Server

3.1.1. 将文件上传到github中:

 

https://github.com/chenkai2008/PojectForMe

 

 

config-client-dev.yml:

server:
  port: 8501

testmsg: test message from github

3.1.2. 修改yml配置文件

 

#config server 从远程git仓库中读取配置文件   
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenkai2008/PojectForMe
          searchPaths: SpringCloudConfig #仓库的文件夹地址
          username: chenkai2008
          password: '*********'
        label: master  #git仓库分支名
  application:
    name: config-server  

 

3.1.3. 测试截图

http://localhost:8501/getTestMessage

 

第四章 结合Eureka使用spring cloud config

 

4.1. Server

 

4.1.1. 依赖引入

 

 

 

<!-- spring cloud config 服务器端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- eureka 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.1.2. 启动器

@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient 

public class ConfigServer_StartApplication {

4.1.3. Yml配置

 

application.yml

server:
  port: 8500

#config server从本地读取配置文件  
#spring:
#  cloud:
#    config:
#      server:
#        native:
#          search-locations: classpath:/clientConfig
#  profiles: 
#    active: native
#  application:
#    name: config-server
    
#config server 从远程git仓库中读取配置文件   
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenkai2008/PojectForMe
          searchPaths: SpringCloudConfig #仓库的文件夹地址
          username: chenkai2008
          password: '@Ck13145201'
        label: master  #git仓库分支名
  application:
    name: config-server      
          
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true 
    serviceUrl:
      defaultZone:   
        http://eureka-server-7001:7001/eureka/
  instance:
    instance-id: config-server-8500
    prefer-ip-address: true  

4.2. Client

 

4.2.1. 依赖引入

<!-- 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-starter-config</artifactId>
        </dependency>
        <!-- eureka 客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.2.2. 启动器

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ConfigClient_StartApplication {
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ConfigClient_StartApplication.class, args);
    }
    
    @Value("${testmsg}")
    String testmsg;
    
    @RequestMapping("/getTestMessage")
    public String getTestMessage() {
        return testmsg;
    }

4.2.3. Yml配置

 

Bootstrap.yml:

spring:
  application:
    name: config-client  #需要与server端要读取的配置文件名匹配config-client-dev.yml({spring.application.name}-{spring.profiles.active}.yml)
  cloud:
    config:
      #uri: http://localhost:8500
      fail-fast: true #如果没有读取成功,执行快速失败
      discovery:
        enabled: true
        serviceId: config-server    #为Eureka server 中 config server的application name,配置serviceId同时对 config server做了负载均衡
  profiles:
    active: dev       #需要与server端要读取的配置文件名匹配config-client-dev.yml({spring.application.name}-{spring.profiles.active}.yml)
    
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true 
    serviceUrl:
      defaultZone:   
        http://eureka-server-7001:7001/eureka/
  instance:
    instance-id: config-client-8501
prefer-ip-address: true 

4.3. 测试截图

http://localhost:8501/getTestMessage

 

第五章 使用spring cloud bus刷新配置

 

需要安装 RabbitMQ 服务器

 

5.1. 依赖引入

<!-- bus刷新配置 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-bus-amqp</artifactId>

</dependency>

5.2. Client端启动器

 

@SpringBootApplication

 

@EnableEurekaClient

 

@RestController

 

@RefreshScope 

 

public class ConfigClient_StartApplication {

5.3. Clientyml配置

 

#bus刷新配置

spring: 
  rabbitmq:
    host: localhost
    port: 8600 
    username: guest 
    password: guest
  management:
    security:
      enabled: false

5.4. 刷新配置

发送POST请求:

http://localhost:8600/bus/refresh

http://localhost:8600/bus/refresh?config-client:**   

//刷新服务名为:config-client的所有服务实例

 

POST请求:

 

<html>
    <head>
        <title>post submit</title>
    </head>
    <body>
        <form action="http://localhost:8600/bus/refresh" method="POST">
            <input type="submit" value="post submit"/>
        </form>
    </body>
</html>

 

汇总:

posted @ 2021-02-28 15:46  無_茗  阅读(60)  评论(0)    收藏  举报