现象

今天尝试做SpringBoot2微服务 整合SpringCloud Config配置,Config Server 和Config Client,服务端配置从Git获取,Client端通过自定义接口方法获取,此时修改git配置文件,Server端能够获取新的配置信息,Client端无法刷新,所以要做client端的配置刷新

根据网上一通操作无法起效,书籍上是SpringBoot1.x ,client端项目resources/bootstrap.yml配置如下

spring:
  application:
    name: microservice-config-client
  cloud:
    config:
      profile: prod # 配置服务中的{profile}
      label: master # 对应git中的分支,默认为master
      uri: http://localhost:8888/  # 配置中心的地址
server:
  port: 8801
management: 
  security:
    enabled: false

client 启动Applicaiton类

package com.itheima.springcloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lifei
 * @date 2019/8/12 - 16:39
 */
@RefreshScope
@SpringBootApplication
@RestController
public class ConfigClientApplicaiton {

    @Value("${clientParam}")
    private String clientParam;

    @RequestMapping("/clientParam")
    public String getParam() {
        return this.clientParam;
    }


    @RequestMapping("/hello")
    public String hello() {
        return "hello world!";
    }

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

}

 

通过调用 post请求 http://localhost:8801/refresh 报404错误找不到

 

原因

Spring boot 2.0的改动较大,/bus/refresh全部整合到actuator里面了,变成了/actuator/refresh,所以之前1.x的management.security.enabled全部失效,不适用于2.0 

 

完整解决方案

Server端pom.xml配置

<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>-->
        <!--<spring-cloud.version>Greenwich.SR2</spring-cloud.version>-->
        <!--<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>-->
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>

Server端Applicaiton

package com.itheima.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @author lifei
 * @date 2019/8/8 - 15:30
 */
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

Sever端application.yml

spring:
  application:
    name: microservice-config-server
  #  profiles:
  #    active: native   # 使用本地文件系统的存储方式来保存配置信息
  cloud:
    config:
      server:
        git: # 使用git的方式
          uri: https://gitee.com/xxx/microservice-building-config.git

server:
  port: 8888

#Spring boot 2.0的改动较大,/bus/refresh全部整合到actuator里面了,变成了/actuator/bus-refresh,所以之前1.x的management.security.enabled全部失效,不适用于2.0
#  注意:config-server和config-client的配置都需要加上
management:
  endpoints:
    web:
      exposure:
        include: refresh

Client端pom.xml配置

 <artifactId>microservice-config-client</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
    </dependencies>

Client端Applicaiton 需要加注解@RefreshScope

package com.itheima.springcloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lifei
 * @date 2019/8/12 - 16:39
 */
@RefreshScope
@SpringBootApplication
@RestController
public class ConfigClientApplicaiton {

    @Value("${clientParam}")
    private String clientParam;

    @RequestMapping("/clientParam")
    public String getParam() {
        return this.clientParam;
    }


    @RequestMapping("/hello")
    public String hello() {
        return "hello world!";
    }

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

}

Client端bootstrap.yml配置

spring:
  application:
    name: microservice-config-client
  cloud:
    config:
      profile: prod # 配置服务中的{profile}
      label: master # 对应git中的分支,默认为master
      uri: http://localhost:8888/  # 配置中心的地址
server:
  port: 8801
#management: #过时
#  security:
#    enabled: false


management:
  endpoints:
    web:
      exposure:
        include: refresh

 

最后通过client端的Post请求http://localhost:8801/actuator/refresh刷新,再查看配置发现配置已经更新

curl -v -X POST "http://localhost:8888/actuator/refresh"

 

 

参考:

spring cloud config 动态刷新配置 /bus/refresh 404 not found

https://blog.csdn.net/weixin_39986856/article/details/83119858

https://blog.csdn.net/u012161563/article/details/81011214

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐