1.Consul简介
1.1.是什么
https://www.consul.io/intro/index.html
Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式”,内置了服务注册与发现框 架、具有以下性质:
分布一致性协议实现、
健康检查、
Key/Value存储、
多数据中心方案,
不再需要依赖其他工具(比如ZooKeeper等)。
使用起来也较 为简单。Consul使用Go语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合 。
基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对.
一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制.
1.2.下载
https://www.consul.io/downloads.html
1.3.怎么玩
https://www.springcloud.cc/spring-cloud-consul.html
2.安装并运行Consul
2.1.官网说明
https://learn.hashicorp.com/consul/getting-started/install.html 下载完成后只有一个consul.exe文件 硬盘路径下双击运行,查看版本信息
步骤:
下载完成后解压,根据自己实际情况选择路径
解压完成后,在解压路径下的地址栏输入“cmd”,打开命令行窗口。并键入“consul”,若出现一连串英文则表示安装成功,见图二。
启动:
命令 consul agent -dev 启动,看到Consul agent running! 启动成功
通过以下地址可以访问 Consul 首页
http://localhost:8500
3.服务提供者
新建Module支付服务provider8006
POM
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8006</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itxiongmao.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
</project>
YML
server:
port: 8006
spring:
application:
name: consul-payment-service
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
主启动类
package com.itxiongmao;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao
* @CreateTime: 2020-11-11 22:40
* @Description: TODO
*/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
业务类Controller
package com.itxiongmao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.controller
* @CreateTime: 2020-11-11 22:41
* @Description: TODO
*/
("payment")
public class PaymentController {
("${server.port}")
private String serverPort;
("consul")
public String paymentConsul(){
return "springcloud with consul:" + serverPort +"\t" + UUID.randomUUID().toString();
}
}
验证测试
http://localhost:8006/payment/consul
4.服务消费者
新建Module消费服务order80
POM
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-comsumerconsul-order80</artifactId>
<dependencies>
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itxiongmao.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
YML
server:
port: 80
spring:
application:
name: cloud-comsumer-order
cloud:
consul:
port: 8500
host: localhost
discovery:
service-name: ${spring.application.name}
主启动类
package com.itxiongmao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
配置bean
package com.itxiongmao.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.config
* @CreateTime: 2020-11-11 22:50
* @Description: TODO
*/
public class ApplicationContextConfig {
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Controller
package com.itxiongmao.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.controller
* @CreateTime: 2020-11-11 22:51
* @Description: TODO
*/
("order")
public class OrderController {
final private String INVOKE_URL="http://consul-payment-service";
private RestTemplate restTemplate;
("payment/consul")
public String getPaymentInfo() {
String info = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
return info;
}
}
访问测试地址
http://localhost/order/payment/consul
5.三个注册中心异同点
5.1.CAP
分区容错性要保证,所以要么是CP,要么是AP
Consistency(强一致性)
Availability(可用性)
Parttition tolerance(分区容错性)
CAP理论关注粒度是否是数据,而不是整体系统设计的策略
5.2.经典CAP图
AP(eureka)
CP(Zookeeper/Consul)
当网络分区出现后,为了保证一致性,就必须拒绝请求,否则无法保证一致性
结论:违背了可用性A的要求,只满足一致性和分区容错,即CP