使用Zookeeper作为配置中心,实现配置的修改和自动下发,基于Spriing Cloud我们可以轻松的实现。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>
server.port=8082
spring.application.name=waiter-service
spring.cloud.zookeeper.connect-string=localhost:2181
eureka.client.service-url.defaultZone=http://****:8761/eureka/
#默认的配置项,订单折扣率
order.discount=95
/config/应⽤用名,profile/key=value
1、/config是根目录
2、服务名是下一级目录,这里是waiter-service
3、配置的键和值对
spring.cloud.zookeeper.config.root=config
spring.cloud.zookeeper.config.default-context=application
spring.cloud.zookeeper.config.profile-separator=','
1、创建根路径/config
2、创建服务名路径waiter-service
3、配置一个配置项order.discount = 30
@ConfigurationProperties("order")
@RefreshScope
@Data
@Component
public class OrderProperties {
private Integer discount = 100;
private String waiterPrefix = "test-";
}
@RestController
@RequestMapping("/coffee")
@Slf4j
public class CoffeeController {
@Autowired
private OrderProperties orderProperties;
@PostMapping(path = "/getDiscount")
@ResponseStatus(HttpStatus.CREATED)
public String getDiscount() {
return String.valueOf(orderProperties.getDiscount());
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。