1、 Consul简介
-
是什么
https://www.consul.io/intro/index.html
-
能干嘛
-
服务发现
提供HTTP/DN
-
健康检测
支持多种方式,HTTP、TCP、Docker、shell脚本定制化
-
KV存储
Key、Value的存储方式
-
多数据中心
Consul支持多数据中心
-
可视化界面
-
-
下载地址
https://www.consul.io/downloads.html
-
中文文档
https://www.springcloud.cc/spring-cloud-consul.html
2、安装并运行Consul
下载完成后只有一个consul.exe文件 硬盘路径下双击运行,查看版本信息
- consul --version
使用开发模式启动
- consul agent -dev
- 通过以下地址可以访问Consul的首页: http://localhost:8500
- 页面
3、服务提供者
-
新建cloud-providerconsul-payment8006
-
POM
<?xml version="1.0" encoding="UTF-8"?> <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>cloud2020</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-providerconsul-payment8006</artifactId> <description>支付服务的提供者之注册中心consul</description> <dependencies> <!--SpringCloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
-
YML
server: # consul服务端口 port: 8006 spring: application: name: cloud-provider-payment cloud: consul: # consul注册中心地址 host: localhost port: 8500 discovery: hostname: 127.0.0.1 service-name: ${spring.application.name}
-
主启动类
@SpringBootApplication @EnableDiscoveryClient public class PaymentMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class, args); } }
-
Controller
RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "payment/consul") public String paymentConsul() { return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString(); } }
-
验证测试
http://localhost:8006/payment/consul
4、服务消费者
-
新建cloud-consumerconsul-order80
-
POM
<dependencies> <!--SpringCloud consul-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
YML
server: port: 80 spring: application: name: cloud-consumer-order cloud: consul: # consul注册中心地址 host: localhost port: 8500 discovery: hostname: 127.0.0.1 service-name: ${spring.application.name}
-
主启动类
@SpringBootApplication @EnableDiscoveryClient public class OrderConsulMain80 { public static void main(String[] args) { SpringApplication.run(OrderConsulMain80.class, args); } }
-
配置bean
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
-
Controller
@RestController @Slf4j public class OrderConsulController { public static final String INVOKE_URL = "http://cloud-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/consul") public String paymentInfo() { return restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); } }
-
验证测试
http://localhost/consumer/payment/consul
5、三个注册中心异同点
CAP
-
C: Consistency(强一致性)
-
A: Availability(可用性)
-
P: Parttition tolerance(分区容错性)
CAP理论关注粒度是否是数据,而不是整体系统设计的策略
分区容错性要保证,所以要么是CP,要么是AP
欢迎转载和分享,记得点赞呀