SpringCloud访问/refresh报404

1.问题描述
在配置spring cloud config 动态刷新时,POST访问 /actuator/refresh刷新修改后的配置文件报404错误,报错信息如下:

post访问报错404

2.解决办法
actuator的配置信息中management.endpoints.web.exposure.include="*"这行配置需要修改为management.endpoints.web.exposure.include=*,即去掉双引号,如果您的这一行配置类似management.endpoints.web.exposure.include=["health","info","refresh"],那么需要修改为management.endpoints.web.exposure.include=refresh,info,health,修改后,如果没有对配置文件做修改,那么访问的结果就是如下:

沒有修改配置文件的访问情況.png

如果修改了配置文件,那么访问的结果就会是另一种情况,配图如下:

修改配置文件的访问情況.png

上图中的 from是配置文件被修改的属性

需要注意的细节点:

1.配置actuator后,需要配置management.endpoints.enabled-by-default=true开启actuator;
2.actuator的默认根路径是/actuator,而不是/
3.actuator的配置需要和端口的配置在同一个文件,例如:actuator的配置在bootstrap.properties文件,那么server.port也需要写在bootstrap.properties文件,当然,actuator的配置信息也可以写在application.properties文件中;
4.@RefreshScope注解的添加

3.客户端config-client主要代码展示

  • bootstrap.properties文件
#对应前配置文件中的{application}部分
spring.application.name=didispace

#方式一:通过服务名访问服务器
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server

#方式二:配置中文件的地址  通过uri访问配置服务
#spring.cloud.config.uri=http://localhost:5555/

#对应前配置文件中的{profile}部分
spring.cloud.config.profile=dev

#对应前配置文件的git分支
spring.cloud.config.label=feature-xiongzelin

eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true

server.port=8866

#actuator配置
management.endpoints.enabled-by-default=true
management.endpoint.health.show-details=always
#management.endpoints.web.exposure.include=refresh,info,health
management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/actuator

application.properties文件啥都没有,就不需要要展示了

  • 启动类

···
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientApplication.class, args);
    }
}
  • web控制器

···
@RefreshScope
@RestController
public class TestController {

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

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

4.代码地址
码云:

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容