项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,具备分布式、高可用、高扩展性html
Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其余分布式服务注册与发现的方案,Consul的方案更“一站式” ,内置了服务注册与发现框 架、具备如下性质:
● 分布一致性协议实现
● 健康检查
● Key/Value存储
● 多数据中心方案
再也不须要依赖其余工具(好比ZooKeeper等)git
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
<!--消息总线,提供配置实时刷新,再也不依赖中间件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-bus</artifactId>
</dependency>
<!--consul的配置中心功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!--服务注册和发现功能-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
复制代码
这里要注意是要配置在 bootstarp.ymlspring
spring:
application:
name: pig-consul
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
format: KEY_VALUE
watch:
enabled: true
prefix: pig-config
复制代码
下载: https://www.consul.io/downloads.html
使用:(dev模式,生成建议cluster模式)bash
-dev表示开发模式运行,使用-client 参数可指定容许客户端使用什么ip去访问,例如-client 127.0.0.1 表示能够使用。
consul agent -dev -client 127.0.0.1
复制代码
生产配置参考:
https://www.consul.io/intro/getting-started/join.html
http://127.0.0.1:8500/ui/ 去访问 app
@RestController
public class DemoController {
@Value("${author}")
private String author;
@GetMapping("/demo")
public String demo() {
return author;
}
}
复制代码
spring:
cloud:
consul:
config:
watch:
enabled: true
复制代码
而后应用要开启定时任务分布式
@EnableScheduling
复制代码