Consul 是 HashiCorp 公司推出的开源产品,用于实现分布式系统的服务发现、服务隔离、服务配置,这些功能中的每一个都可以根据需要单独使用,也可以同时使用所有功能。Consul 官网目前主要推 Consul 在服务网格中的使用。
与其它分布式服务注册与发现的方案相比,Consul 的方案更“一站式”——内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其它工具。Consul 本身使用 go 语言开发,具有跨平台、运行高效等特点,也非常方便和 Docker 配合使用。
Consul旨在对 DevOps社区和应用程序开发人员友好,使其成为现代、弹性基础架构的理想选择。
gRPC\DNS
多种访问方式。Feature | Euerka | Consul |
---|---|---|
服务健康检查 | 可配支持 | 服务状态,内存,硬盘等 |
多数据中心 | — | 支持 |
kv 存储服务 | — | 支持 |
一致性 | — | raft |
cap | ap | cp |
使用接口(多语言能力) | http(sidecar) | 支持 http 和 dns |
watch 支持 | 支持 long polling/大部分增量 | 全量/支持long polling |
自身监控 | metrics | metrics |
安全 | — | acl /https |
编程语言 | Java | go |
Spring Cloud 集成 | 已支持 | 已支持 |
以windows为例,到官网下载安装。添加consul.exe到环境变量。
控制台执行consul agent -dev
consul agent -dev
运行的开发环境,方便开发者使用,实际生产需要部署3台以上形成集群。
我们需要建两个模块,Server和Client,其实也就是Producer和Consumer,通过两个模块我们来演示如何用Consul轻松的替换Eureka。并且模拟Client远程调用 Server端。
新建项目模块register-consul,引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>group.group.zhouning</groupId>
<artifactId>feign-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
添加应用配置:
server:
port: 8861
spring:
application:
name: register-consul-server
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例ID
health-check-path: /actuator/health #健康检查
health-check-interval: 10s
新建启动类:
@EnableDiscoveryClient
@SpringBootApplication
public class RegisterConsulApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterConsulApplication.class, args);
}
}
配置对外接口
我们这里对外对外提供两个接口,一种是返回字符串类型的,一种是返回实体类型的。
@RestController
public class HelloWorldController{
/**
* 获取字符串信息
* @return
*/
@GetMapping("/helloWorld")
public String HelloWorld() {
return "Hello World!";
}
/**
* 获取用户信息
* @return
*/
@GetMapping("/user")
public User getUser() {
return new User(1L,"consul","test","abc@gmail.com","Consul 替换Eureka");
}
}
接下来新建项目模块cloud-client-consul,用于演示Feign远程调用。对于Feign 相信已经都了解了。引入maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>group.group.zhouning</groupId>
<artifactId>feign-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
应用配置:
server:
port: 8862
spring:
application:
name: register-consul-client
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} #实例ID
health-check-path: /actuator/health #健康检查
health-check-interval: 10s
main:
allow-bean-definition-overriding: true #当出现相同名字的类进行注册时,准许覆盖注册
启动类:
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ClientConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ClientConsulApplication.class, args);
}
}
编写Feign Api:
利用Feign 配置服务的远程调用,调用策略使用Feign 默认的。
@FeignClient(value = "register-consul-server")
public interface ConsulApi {
/**
* 获取字符串信息
* @return
*/
@GetMapping("/helloWorld")
String helloWorld();
/**
* 获取用户信息demo
* @return
*/
@GetMapping("/user")
User getUser();
}
Client 测试控制层:
测试控制层的路由读者可以重新定义
@RestController
public class TestController {
@Autowired
private ConsulApi helloApi;
/**
* 获取字符串信息
* @return
*/
@GetMapping("/hello")
public String hello() {
return helloApi.helloWorld();
}
/**
* 获取用户信息
* @return
*/
@GetMapping("/user")
public User user() {
return helloApi.getUser();
}
}
验证:
启动Server和Client,在http://localhost:8500/中可以看到服务注册成功
访问127.0.0.1:8862/user可以看到如下结果
当 Producer启动的时候,会向 Consul发送一个 POST请求,告诉 Consul自己的 IP和 Port;
Consul接收到 Producer的注册后,每隔 10s(默认)会向 Producer发送一个健康检查的请求,检验 Producer是否健康;
当 Consumer发送 GET方式请求 /user到 Producer时,会先从 Consul中拿到一个存储服务 IP和 Port的临时表,从表中拿到 Producer的 IP和 Port后再发送 GET方式请求 /user;
该临时表每隔 10s 会更新,只包含有通过了健康检查的 Producer。
Spring Cloud Consul项目是针对 Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中,为我们的基础设施提供服务发现和服务配置的工具。
本文由 寻非 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://www.zhouning.group/archives/springcloud15注册中心之consul
最后更新:2020-05-22 21:37:14
Update your browser to view this website correctly. Update my browser now