一、Consul简介

Consul官网

1、Consul是什么

Consul是一套开源的分布式服务发现和配置管理系统,有HashiCorp公司使用GO语言开发。

提供了微服务系统中的服务治理,配置中心,控制总线等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以架构全方位的服务网格,总之Consul提供了一种完整的服务网个解决方案。

它具有很多优点,包括:基于raft协议,比较简介;支持健康检查,同时支持Http和DNS协议 支持跨数据中心的WAN集群提供图形界面 跨平台,支持Linux,mac windows

2、Consul能干吗

  • 服务发现
  • 健康检测
  • KV存储
  • 多数据中心
  • 可视化WEB界面

3、下载

下载地址
学习地址

二、安装并运行Consul

下载windows后,解压后就一个文件 consul.exe 文件

在该目录下打开 cmd 输入 consul --version 就可以查看consul的版本
image.png
Consul 路径: D:\Program Files\Consul
启动consul:consul agent -dev
访问控制界面:http://localhost:8500

1、服务提供者

创建 cloud-providerconsul-payment8006 模块

1.1 添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.springcloud</groupId>
  8. <artifactId>cloud-api-commons</artifactId>
  9. <version>${project.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-actuator</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-devtools</artifactId>
  22. <scope>runtime</scope>
  23. <optional>true</optional>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectlombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <optional>true</optional>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>

1.2 写yaml

  1. server:
  2. port: 8006
  3. spring:
  4. application:
  5. name: cloud-payment-service
  6. cloud:
  7. consul:
  8. host: localhost
  9. port: 8500
  10. discovery:
  11. service-name: ${spring.application.name}

1.3 主启动类

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class ConsulMain8006 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConsulMain8006.class,args);
  6. }
  7. }

1.4 业务类

  1. @RestController
  2. public class PaymentController {
  3. @Value("${server.port}")
  4. private String port;
  5. @RequestMapping("/get/Payment/{id}")
  6. public CommonResult get(@PathVariable Long id){
  7. return new CommonResult(200,"",port);
  8. }
  9. }

1.4 测试

image.png


2、服务消费者

创建 cloud-consumerconsul-order80 模块

2.1 添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.yixuexi.springcloud</groupId>
  8. <artifactId>cloud-api-commons</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-actuator</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-devtools</artifactId>
  22. <scope>runtime</scope>
  23. <optional>true</optional>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectlombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <optional>true</optional>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>

2.2 yaml

  1. server:
  2. port: 80
  3. spring:
  4. application:
  5. name: consul-consumer-order
  6. cloud:
  7. consul:
  8. host: localhost
  9. port: 8500
  10. discovery:
  11. service-name: ${spring.application.name}

2.3 主启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class OrderConsulMain80 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(OrderConsulMain80.class,args);
  6. }
  7. }

2.4 配置类

添加 RestTemplate 组件

  1. @Configuration
  2. public class MyConfig {
  3. @LoadBalanced
  4. @Bean
  5. public RestTemplate restTemplate(){
  6. return new RestTemplate();
  7. }
  8. }

2.5 业务类

远程调用的业务类

  1. @RestController
  2. public class PaymentController {
  3. @Autowired
  4. private RestTemplate restTemplate;
  5. @RequestMapping("/consumer/payment/get/{id}")
  6. public CommonResult<Payment> get(@PathVariable("id") Long id){
  7. String url = "http://cloud-payment-service/payment/get/" + id;
  8. return restTemplate.getForObject(url,CommonResult.class);
  9. }
  10. }

2.6 测试

image.png

三、三个注册中心异同点

1、 CAP 概念

C:Consistency 强一致性
A:Availability 可用性
P:Partition tolerance 分区容错
CAP 理论关注粒度是数据,而不是整体系统设计的策略。

2、经典CAP图

最多只能同时较好的满足两个
CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性着三个需求,因此根据CAP原理将NoSQL数据库分成了满足CA原则,满足CP原则和满足AP原则三大类:

  • CA-单点集群 满足一致性,可用性的系统,通常在可拓展性上不太强大。
  • CP-满足一致性,分区容忍比的系统,通常性能不是特别高。
  • AP-满足可用性,分区容忍性的系统,通常可能对一致性要求低一些。

image.png

2.1 AP架构(Eureka)

当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。
结论:违背了一致性C的要求,只满足可用性和分区容错,即AP
image.png

2.2 CP架构

当网络分区出现后,为了保证一致性,就必须拒接请求,否则无法保证一致性
结论:违背了可用性A的要求,只满足一致性和分区容错,即CP
image.png