SpringBoot/SpringCloud初探七(配置中心)

演示架构:

SpringBoot/SpringCloud初探七(配置中心)_第1张图片

采用本地加载方式(模拟从git仓库加载),在本地创建一个文件夹D:\XXX\ConfigService\git_server_mock,存放一份配置文件microservice-a-dev.yml(特别注意,文件命名有讲究的,要符合命名规则,否则无效,这里样例使用“/{application}-{profile}.yml”方式)

配置文件三种命名规则(application是服务名,profile是环境变量,label是分支名):
/{application}/{profile}[/{label}]
/{application}-{profile}.yml 或者 /{application}-{profile}.properties
/{label}/{application}-{profile}.yml 或者 /{label}/{application}-{profile}.properties

这里微服务名称明确指示,是service A的配置文件,所以service A会来这里读配置文件。

一、配置中心

配置文件microservice-a-dev.yml,比较简单,样例只写了nickName一个字段:

nickName: 十维之眼

pom.xml

引入spring-cloud-config-server



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.1
         
    
    com.abcd
    Config
    0.0.1-SNAPSHOT
    Config
    Config
    
        1.8
        2021.0.0
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.yml

指定方式(本地或者远程)和配置文件目录,这里不需要指定具体配置文件,配置文件是通过名称被识别的

server:
  port: 8690

spring:
  application:
    name: config-center
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: D:\XXX\ConfigService\git_server_mock

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

java代码

增加注解:@EnableConfigServer

@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {

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

}

二、Service A

pom.xml

引入spring-cloud-starter-config和spring-cloud-starter-bootstrap

        ...

        
            org.springframework.cloud
            spring-cloud-starter-config
        

        
            org.springframework.cloud
            spring-cloud-starter-bootstrap
        

       ...

bootstrap.yml

注意:yml名称必须叫bootstrap。8690是配置中心端口。

server:
  port: 7901

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: microservice-a
  cloud:
    config:
      uri: http://localhost:8690/
      profile: dev
      label: master

java代码,用于读取配置文件的内容(nickName)

Controller代码

@RestController
public class HelloController {

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

    @RequestMapping("/hello")
    public String hello() {
        return "hello " + nickName;
    }
}

这样,访问http://localhost:7901/hello,就能够从配置中心读取配置文件的内容(nickName)。

三、在线更新配置(免重启服务)

配置中心的值,修改之后,仍需要重启服务A才能生效,体验较差。所以,提供自动刷新方式,免重启服务A即可让配置文件修改生效。

改造:

Service A的pom.xml

引入spring-boot-starter-actuator

        ...

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        ...

Service A的bootstrap.yml

增加配置:

...

#For config refresh by posting refresh request
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"

 ...

Service A的java代码

增加注解@RefreshScope

//use this command to refresh the config: POST http://localhost:xxxx/actuator/refresh
@RefreshScope
@RestController
public class HelloController {

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

    @RequestMapping("/hello")
    public String hello() {
        return "hello " + nickName;
    }
}

这样,POST http://localhost:7901/actuator/refresh便可以在线刷新配置值,之后再调用http://localhost:7901/hello会发现新的配置值(比如把“十维之眼”改成“九维之眼”)已经生效。

你可能感兴趣的:(工程化知识,spring,cloud,eureka,java)