【springCloud基础篇-11】Spring Cloud Config 配置中心之refresh与SVN示例(他人的博客地址)
接上篇文章。
SVN示例的博客地址:http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html
refresh:
客户端client项目添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
修改配置文件(application.yml)注意关闭权限:
server: port: 9021 eureka: client: service-url: defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/ spring: application: name: spring-cloud-config-client management: security: enabled: false #springboot 1.5.X 以上默认开通了安全认证
控制器添加注解@RefreshScope:
package com.example.spirngcloudconfigclient.web; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ProjectName: spirng-cloud-config-client * @Package: com.example.spirngcloudconfigclient.web * @ClassName: HelloController * @Author: MC * @Description: ${description} * @Date: 2019/11/15 0015 11:45 * @Version: 1.0 */ @RestController @RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。 public class HelloController { @Value("${native.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
测试:
先访问http://localhost:9021/hello
返回的是hello_i_im_mysql
修改server项目中的mysql配置文件:
native.hello=hello_i_im_mysql_update
重启server项目。
cmd命令执行:curl -X POST http://localhost:9021/refresh返回结果如图:
再次访问浏览器:http://localhost:9021/hello
OK