SpringCloud(Finchley) Config自动刷新配置文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caojidasabi/article/details/87910900

config server

  • 一.创建maven项目,加入依赖
	<!--消息队列-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
  • 二.编写启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}
        
  • 三.在resoures下创建bootstrap.yml(比application.yml先加载)
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ydoublemm/spring-cloud-config-repo.git
  rabbitmq:
    host: your host address
    port: port (default 5672)
    username: your name
    password: your password


management:
  endpoints:
    web:
      exposure:
        include: "*"
  • 四.启动项目

config client

  • 一.创建maven项目,加入依赖
	<!--消息队列-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
  • 二.编写启动类
@SpringBootApplication
public class ConfigClientApplication
{
   public static void main( String[] args ) {
       SpringApplication.run(ConfigClientApplication.class, args);
   }


}
  • 三.在resoures下创建bootstrap.yml(比application.yml先加载)
spring:
  application:
    name: application    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      profile: dev            # profile对应config server所获取的配置文件中的{profile}

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh
  • 创建application.yml
spring:
  rabbitmq:
   host: your host address
   port: port (default 5672)
   username: your name
   password: your password
  • 四.启动项目
    在git上修该配置后,打开cmd
    输入 curl -X POST “localhost:8080/actuator/bus-refresh”
    就能刷新配置了

猜你喜欢

转载自blog.csdn.net/caojidasabi/article/details/87910900