转载

springcloud 配置文件管理动态刷新

当修改一个配置文件,我们需要重新打包项目或重新启动服务才能生效,springcloud的config实现了配置文件的统一管理,当配置文件修改后提交Git后系统会自动刷新获取修改后的配置文件。

springcloud 配置文件管理动态刷新

eureka集群搭建

rabbitmq的安装配置

  • 将配置文件上传至Git上
springcloud 配置文件管理动态刷新

新建一个maven项目:

springcloud 配置文件管理动态刷新

建好的项目结构如下:

springcloud 配置文件管理动态刷新
  • pom文件相关依赖
<?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>cloud</groupId>
   <artifactId>cloud-config-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>cloud-config-server</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.10.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>Greenwich.M3</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </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>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <exclusions>
               <exclusion>
                  <groupId>com.fasterxml.jackson.dataformat</groupId>
                  <artifactId>jackson-dataformat-xml</artifactId>
               </exclusion>
            </exclusions>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
               <encoding>UTF-8</encoding>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
               <encoding>UTF-8</encoding>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
   <repositories>
      <repository>
         <id>spring-milestones</id>
         <name>Spring Milestones</name>
         <url>https://repo.spring.io/milestone</url>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
      </repository>
   </repositories>
</project>
复制代码
  • 启动文件
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

   public static void main(String[] args) {
      SpringApplication.run(ConfigServerApplication.class, args);
   }
}
复制代码
  • 服务器端配置文件详情
server.port=8081
spring.cloud.config.server.git.uri=https://github.com/castanea-kang/config-repo.git
spring.cloud.config.server.git.searchPaths=cloud-config-repo
spring.application.name=cloud-config
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=cloud-config

spring.rabbitmq.host=114.115.243.135
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

eureka.client.serviceUrl.defaultZone=http://eureka01:8080/eureka/
#eureka.instance.prefer-ip-address=true       
#eureka.instance.ip-address=114.115.243.135  #配置从eureka注册中心访问接口的ip
#eureka.instance.status-page-url-path=/cloud-config/test/  #接口访问路径
management.security.enabled=false   #是否开启安全认证
复制代码
  • 启动config server端可以再eureka注册中心找到该服务,如下图:
springcloud 配置文件管理动态刷新

可以通过访问地址http://114.115.243.135:8081/cloud-config/test/ 得到数据:

springcloud 配置文件管理动态刷新
springcloud 配置文件管理动态刷新

config客户端相关配置

  • pom配置文件相关依赖
<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>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 总线刷新,通过rabbitmq -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
复制代码
  • 配置文件详情bootstrap.properties
spring.application.name=cloud-config
spring.cloud.config.profile=test
eureka.client.serviceUrl.defaultZone=http://114.115.243.135:8080/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=cloud-config
eureka.client.register-with-eureka=false

spring.rabbitmq.host=114.115.243.135
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

management.security.enabled=false
spring.cloud.bus.trace.enabled=true
复制代码
  • 利用git的webhook实现实时更新通知,当向git进行事件操作时,进行配置文件刷新应用。
springcloud 配置文件管理动态刷新

注:如果出现配置正常刷新日志打印正常,但是并没有刷新,在config client 控制层上加上注解

@RefreshScope
原文  https://juejin.im/post/5bff9fd26fb9a049bc4c5fd9
正文到此结束
Loading...