Spring Cloud(十四)Config 分布式配置中心

Table of Contents

1.Config  介绍

2.github 上存放配置文件

3.服务端构建

1.pom.xml中添加依赖

2.代码实现

3.访问规则

4.客户端构建

1.pom.xml中添加依赖

2.代码实现

3.动态刷新配置现


 

1.Config  介绍

https://spring.io/projects/spring-cloud-config#overview

https://github.com/akeung/springclouddemo-config

        Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以集中管理所有环境中应用程序的外部属性。客户端和服务器上的概念与Spring EnvironmentPropertySource抽象,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。当应用程序从开发人员到测试人员进入生产过程进入生产过程时,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持配置环境的带标签的版本,并且可用于管理内容的各种工具。添加替代实现并将其插入Spring配置很容易。

2.github 上存放配置文件

https://github.com/akeung/springclouddemo-config

 

3.服务端构建

config-center-3344

1.pom.xml中添加依赖

<dependencies>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ak.demo</groupId>
            <artifactId>api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

 

2.代码实现

yml文件配置

server:
  port: 3344

spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/akeung/springclouddemo-config
          search-paths:
            - springclouddemo-config #搜索目录
      label: master #读取分支

eureka:
  client:
    register-with-eureka: true #表示向注册中心注册自己 默认为true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

主启动类

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigCenter3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenter3344.class,args);
    }
}

 

3.访问规则

https://docs.spring.io/spring-cloud-config/docs/2.2.4.RELEASE/reference/html/

访问规则:通过这套规则在浏览器上直接访问

#http://localhost:3344/config/dev/master
/{application}/{profile}[/{label}] 

 #http://localhost:3344/config-dev.yml
/{application}-{profile}.yml

 #http://localhost:3344/master/config-dev.yml
/{label}/{application}-{profile}.yml

 #http://localhost:3344/config-dev.properties 
/{application}-{profile}.properties

 #http://localhost:3344/master/config-dev.properties 
/{label}/{application}-{profile}.properties

4.客户端构建

config-client-3370

1.pom.xml中添加依赖

<dependencies>
        <!--config-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ak.demo</groupId>
            <artifactId>api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

 

2.代码实现

bootstrap..yml文件配置

applicaiton.yml 是用户级的资源配置项
bootstrap.yml 是系统级的,优先级更加高

Spring Cloud会创建一个"Bootstrap Context" ,作为Spring应用的Application Context的父上下文。初始化的时候,`BootstrapContext负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的Environment.

Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离

要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的。
因为bootstrap.yml是比application.yml先加载的. bootstrap.yml优先级高于application.ym

server:
  port: 3370

spring:
  application:
    name: config-client
  cloud:
    config: #根据访问配置中心的访问规则配置
      label: master #读取分支
      name: config #配置文件名称
      profile: dev #后缀名称
      uri: http://localhost:3344 #配置中心地址

eureka:
  client:
    register-with-eureka: true #表示向注册中心注册自己 默认为true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

 

主启动类

@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConfigClient3370 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient3370.class,args);
    }
}

测试

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

3.动态刷新配置现

当github上更新配置后,我们的服务要重新启动才能使用新的配置,否则配置无法生效。故需要 动态的修改配置。

1.必须依赖

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.修改yml,暴露监控端口

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: refresh
#        include: "*"

3.@RefreshScope修饰控制类

4.POST 请求执行刷新

curl -X POST "http://localhost:3370/actuator/refresh"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值