美文网首页
SpringCloud访问/refresh报404

SpringCloud访问/refresh报404

作者: CXY_XZL | 来源:发表于2019-10-09 11:28 被阅读0次

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.代码地址
码云:

相关文章

网友评论

      本文标题:SpringCloud访问/refresh报404

      本文链接:https://www.haomeiwen.com/subject/kmvwpctx.html