1.这里采用的是docker-compose的方式来部署consul集群,代码如下,docker的安装以及docker-compose的安装在此不做介绍。
创建docker-compose.yml文件并将其上传到服务器中,在目录下执行docker-compose up -d既可。
version: '2'
networks:
consul:
driver: bridge
services:
consul1:
image: consul
container_name: node1
command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
networks:
- consul
consul2:
image: consul
container_name: node2
command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
- consul1
networks:
- consul
consul3:
image: consul
container_name: node3
command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
depends_on:
- consul1
networks:
- consul
consul4:
image: consul
container_name: node4
command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui
ports:
- 8500:8500
depends_on:
- consul2
- consul3
networks:
- consul
采用mvn的方式,完整的pom代码如下:
完整源代码地址:https://gitee.com/cosmosNi/consul-demo
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.cosmos
consul-demo
0.0.1-SNAPSHOT
consul-demo
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-config
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-bus
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
consul-web
org.springframework.boot
spring-boot-maven-plugin
repackage
主要配置:
spring:
cloud:
consul:
discovery:
health-check-path: /actuator/health ##检测实例健康
health-check-interval: 10s ##每隔10s检查
# hostname: 127.0.0.1 ##配置实例地址
register: true ##自动注册
service-name: ${spring.application.name} ##实例名称
host: 127.0.0.1 #consul地址
port: 8500 ##consul端口
config:
enabled: true ##是否开启配置中心
format: yaml ##配置中心解析样式
application:
name: consultest
创建configuration类用于接收远程配置:代码如下
@ConfigurationProperties(prefix = "student")
@Data
@ToString
public class StudentConfig {
private String name;
private int age;
private String sex;
}
创建一个feign类,用于进行远程访问以及负载均衡,这里的value的值即为上面配置的spring.application.name
@FeignClient(value = "consultest")
public interface FeignService {
@GetMapping("/test")
String test();
}
创建controller,用于消费以及服务提供
@RestController
public class TestController {
@Autowired
private FeignService feignService;
@Autowired
private StudentConfig studentConfig;
@GetMapping("/test")
public String test() {
System.out.println("------输出----------");
return "1232132132";
}
@GetMapping("/consul")
private String testConsul() {
return feignService.test();
}
@GetMapping("/config")
private String testConfig() {
return studentConfig.toString();
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableConfigurationProperties({StudentConfig.class})
public class ConsulDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulDemoApplication.class, args);
}
}
最后,根据自己需要启动多个服务,在这里笔者使用dev,test,client三个环境
访问consul地址:key指代的是作用的配置文件以及application.name
配置中心(当你在consul里更改配置时,客户端通过bus自动刷新,获取最新值)
服务注册:请求多次
负载均衡:
在Eureka2.0闭源的时代,consul也是一个不错的选择。