springcloud config 配置文件管理動態刷新

配置文件管理server端

當修改一個配置文件,咱們須要從新打包項目或從新啓動服務才能生效;springcloud的config實現了配置文件的統一管理,當配置文件修改後提交Git後系統會自動刷新獲取修改後的配置文件(springcloud bus 會對外提供一個接口,即/bus/refresh, 將這個接口配置到Git的webhook上,當git上的內容有所變更時,它就會自動調用這個接口,通知config server端,config server會發布更新消息到消息總線的消息隊列中,總線接到消息會通知鏈接到它的客戶端,客戶端收到消息會從新請求服務器端的最新配置,從而進行配置文件的更新)。java

eureka集羣搭建git

rabbitmq的安裝配置github

  • 將配置文件上傳至Git上

新建一個maven項目:

建好的項目結構以下:

  • 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註冊中心找到該服務,以下圖:

能夠經過訪問地址http://114.115.243.135:8081/cloud-config/test/ 獲得數據:web

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進行事件操做時,進行配置文件刷新應用。

注:若是出現配置正常刷新日誌打印正常,可是並無刷新,在config client 控制層上加上註解

@RefreshScopespring

相關文章
相關標籤/搜索