spring-cloud-config+spring-cloud-bus(实现配置中心动态刷新配置文件)

  • 前置条件
  1. rabbitmq安装
  2. 使用maven构建项目
  3. 安装postman(方便发送请求刷新配置)
  4. 环境:springboot 2.0.6版本

 

  • 创建项目config-server
  1. 项目结构

  1. bootstrap.yml配置(本文档使用本地配置文件,也可使用git和svn)

  1. 动态改变的配置文件(自定义文件中的值)

  1. pom.xml内容
<?xml version="1.0" encoding="UTF-8"?>

<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>

       <groupId>com.wei</groupId>

       <artifactId>config-server</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <packaging>jar</packaging>

       <name>config-server</name>

       <description>Demo project for Spring Boot</description>

       <parent>

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

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

              <version>2.0.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>

              <spring-cloud.version>Finchley.SR1</spring-cloud.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-test</artifactId>

                     <scope>test</scope>

              </dependency>

              <dependency>

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

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

        </dependency>

        <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>${spring-cloud.version}</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>

</project>

 

  1. 启动项目类注意事项

  • 创建项目config-client
  1. 项目结构

  1. bootstrap.yml配置

  1. pom.xml内容
<?xml version="1.0" encoding="UTF-8"?>

<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>



       <groupId>com.wei</groupId>

       <artifactId>config-client</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <packaging>jar</packaging>



       <name>config-client</name>

       <description>Demo project for Spring Boot</description>



       <parent>

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

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

              <version>2.0.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>

              <spring-cloud.version>Finchley.SR1</spring-cloud.version>

       </properties>



       <dependencies>

              <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>



              <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-starter-bus-amqp</artifactId>

              </dependency>

       </dependencies>



       <dependencyManagement>

              <dependencies>

                     <dependency>

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

                            <artifactId>spring-cloud-dependencies</artifactId>

                            <version>${spring-cloud.version}</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>





</project>

 

  1. service内容

  1. controller内容

 

 

  • 项目启动运行实现动态刷新配置
  1. 分别启动config-server和config-client项目
  2. postman发送请求访问 ” http://19168.1.8:8080/hello”查看返回值password

  1. 不重启项目,直接改变cykp-dev.yml文件中的password值

改之前:改之后

  1. postman发送请求访问 ” http://192.168.1.8:8888/actuator/bus-refresh”(不会返回任何值)

  1. 再次postman发送请求访问 ” http://192.168.1.8:8080/hello”查看返回值password (发生了改变)

 

 

 

  • 遇坑总结
  1. 由于springboot2.0后改变很多导致网上攻略有很多坑
  2. 使用@value注解的类上需要添加注解@RefreshScope
  3. 发送请求访问 ” http://192.168.1.8:8888/actuator/bus-refresh”的需要以post请求发送

 

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值