接上篇,实际项目中,可能会遇到有些配置项,例如:邮件地址、手机号等在服务已经上线之后做了改动(就当会出现这种情况好了)。然后你修改了配置信息,就得一个一个去重启对应的服务。spring-全局配置提供了一种热部署机制,可以在重启工程的前提下改变内存中配置项的值。
还是中client-config项目,首先我们要再添加一个jar引用:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
这个监控包,包含了/refresh的api接口,接受post方法。
上篇中的UserController类,我们只要在上面加入@RefreshScope这个注解,那么里面的 testProperty 这个属性值就会被动刷新。
package com.bing.User; import com.bing.model.User; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * Created by szt on 2016/11/18. */ @RestController @RefreshScope public class UserController { @Value("${bing.for.test}") private String testProperty; @RequestMapping(value = "/user/{id}",method = RequestMethod.GET) public User getUser(@PathVariable("id") String id) { User user=new User(); user.setName(testProperty); user.setId(id); user.setCreatedTime(new Date()); return user; } }