spring cloud config 实现配置文件的动态刷新

1. 先构建一个配置中心,并注册到注册中心

1)首先我们来构建一个配置中心,方式很简单,创建一个普通的Spring Boot项目,叫做config-server,创建好之后,添加如下依赖:(21-springcloud-config-center)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

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

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

    <!--添加配置中心的依赖-->
    <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>


    <!--添加服务注册和发现依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

</dependencies>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


2)在入口类上添加@EnableConfigServer注解,表示开启配置中心服务端功能,如下:

@EnableDiscoveryClient  // 服务注册与发现
@EnableConfigServer
@SpringBootApplication
public class ConfigCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication.class, args);
    }
}
3)在application.properties中配置一下git仓库的信息以及服务注册的地址:

server.port=2007
spring.application.name=config-server
#向服务注册中心注册服务
eureka.client.service-url.defaultZone=http://localhost:1110/eureka/



#仓库配置中心的配置信息
#uri表示配置中心所在仓库的位置
spring.cloud.config.server.git.uri=https://github.com/lihy2018/newRepo.git
#search-paths表示仓库下的子目录
spring.cloud.config.server.git.search-paths=config-repo
#username表示你的GitHub用户名
spring.cloud.config.server.git.username=******
#password表示你的GitHub密码
spring.cloud.config.server.git.password=*******


#更改本地仓库clone的配置文件信息的路径
spring.cloud.config.server.git.basedir=D:\\localGitRepo\\

#配置中心服务端健康监测器
spring.cloud.config.server.health.repositories.check.name=app
spring.cloud.config.server.health.repositories.check.label=master
spring.cloud.config.server.health.repositories.check.profiles=prod


#安全保护
security.user.name=*******
security.user.password=*****


通过本地的git命令, 向github远程仓库上提交配置文件:
****-PC MINGW64 ~ (master)
$ cd  config-repo/

****-PC MINGW64 ~/config-repo (master)
$ ll
total 4
-rw-r--r-- 1 lihy 197121 20 六月 10 12:12 app.properties
-rw-r--r-- 1 lihy 197121 16 六月 10 12:13 app-dev.properties
-rw-r--r-- 1 lihy 197121 30 六月 20 16:46 app-prod.properties
-rw-r--r-- 1 lihy 197121 45 六月 11 19:01 app-test.properties  

提交远程仓库github上:

$ cat  app-prod.properties
sang=prod config first !!!

$ git  add  config-repo/

$ git  commit config-repo/   -m  "first"

$ git push  


2.构建一个配置客户端

1)首先创建一个普通的Spring Boot工程config-client,创建成功之后添加如下依赖:(21-springcloud-config-client)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

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

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

    <!--添加config 客户端的依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    <!--添加服务注册和发现依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>


    <!--添加  重试机制 的依赖
    因网络的抖动等原因导致config-client在启动时候访问config-server没有访问成功从而报错,
    希望config-client能重试几次,故重试机制
    -->
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>


    <!-- 动态刷新配置
      更新了Git仓库中的配置文件,那如何让config-client能够及时感知到呢?
      动态刷新配置
   -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2)创建bootstrap.properties文件,来获取配置信息,注意这些信息一定要放在bootstrap.properties文件中才有效,文件内容如下:

spring.application.name=app-client
#配置客户端信息
spring.cloud.config.profile=prod
spring.cloud.config.label=master

#服务注册发现配置
eureka.client.service-url.defaultZone=http://localhost:1110/eureka/

#表示开启通过服务名来访问config-server
spring.cloud.config.discovery.enabled=true
#表示注册中心config-server的服务名
spring.cloud.config.discovery.service-id=config-server


#启动失败时能够快速响应
spring.cloud.config.fail-fast=true


#安全保护
spring.cloud.config.username=****
spring.cloud.config.password=****



#动态刷新配置 ---需要忽略权限拦截
management.security.enabled=false

3)在application.properties中配置端口和重试机制

server.port=2008

#和重试机制相关的配置有如下四个:
# 配置重试次数,默认为6
spring.cloud.config.retry.max-attempts=6
# 间隔乘数,默认1.1
spring.cloud.config.retry.multiplier=1.1
# 初始重试间隔时间,默认1000ms
spring.cloud.config.retry.initial-interval=1000
# 最大间隔时间,默认2000ms
spring.cloud.config.retry.max-interval=2000


4)在入口类上添加@EnableDiscoveryClient注解,表示开启服务注册与发现功能,如下:

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

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

5)编写controller

@RestController
@RefreshScope
public class HelloController {

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

    @Autowired
    private Environment environment ;

    @RequestMapping("/hello1")
    public  String   getHello(){
        return  this.sang;
    }

    @RequestMapping("/hello2")
    public  String   getHello2() {
        return environment.getProperty("sang", "未定义");
    }
}

3.  依程序启动服务注册中心,配置中心config-server , 配置客户端app-client(启动两个服务,端口分别为2008 , 2009 )

4.访问服务 http://localhost:2009/hello2  或者 http://localhost:2008/hello2  


然后使用本地的git 修改配置文件app-prod.properties ,然后提交、push到远程仓库Github上。

继续访问服务 http://localhost:2009/hello2  或者 http://localhost:2008/hello2 ,此时访问的结果会不是最新的, 需要使用postman post请求访问 http://localhost:2009/refresh



然后再访问http://localhost:2009/hello2 时,可以看到是最新的修改结果。

然后再访问http://localhost:2008/hello2 时,可以看到是仍然是的原来的旧的结果。 config 的动态更新,需要手动刷新每一个服务。

使用postman post请求访问 http://localhost:2008/refresh 刷新后就可以看到http://localhost:2008/hello2也可以访问最新的了。

   



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值