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请求发送

 

 

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页