当前位置:网站首页>7.Consul服务注册与发现
7.Consul服务注册与发现
2022-06-26 12:53:00 【苦 糖 果】
1. Consul简介
1.1是什么

1.2能干嘛
服务发现:提供HTTP和DNS两种发现方式
健康监测:支持多种协议,HTTP、TCP、Docker、Shell脚本定制化
KV存储:key , Value的存储方式
多数据中心:Consul支持多数据中心
可视化Web界面
官网:https://www.consul.io/intro/index.html
下载地址:https://www.consul.io/downloads.html
中文文档:https://www.springcloud.cc/spring-cloud-consul.html
1.3 安装并运行Consul
下载完成后只有一个consul.exe文件,硬盘路径下双击运行,查看版本信息

使用开发模式启动:consul agent -dev
通过以下地址可以访问Consul的首页:http://localhost:8500

2.新建Module支付服务provider8006
2.1 新建module cloud-providerconsul-payment8006
2.2 改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>springcloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-providerconsul-payment8006</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.3 改yml
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${
spring.application.name}
2.4 启动类与业务类
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
2.5验证测试
http://localhost:8006/payment/consul
3.新建Module消费服务order8006
3.1 新建Modulecloud-consumerconsul-order80
3.2 改pom
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.3改yml
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${
spring.application.name}
3.4启动类与业务类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOME_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/consul")
public String payment (){
String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);
return result;
}
}
3.5验证测试
http://localhost/consumer/payment/consul
4.三个注册中心异同点
4.1CAP理论
C:Consistency(强一致性)
A:Availability(可用性)
P:Partition tolerance(分区容错)
CAP理论关注粒度是数据,而不是整体系统设计的策略

4.2 三个注册中心异同点

4.2.1AP(Eureka)


4.2.2 CP(Zookeeper/Consul)


边栏推荐
- What features are added to Photoshop 2022 23.4.1? Do you know anything
- 李航老师新作《机器学习方法》上市了!附购买链接
- Mediapipe gestures (hands)
- 【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
- ES基于Snapshot(快照)的数据备份和还原
- Es common grammar I
- sed编辑器
- [MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
- Es snapshot based data backup and restore
- Beifu twincat3 can read and write CSV and txt files
猜你喜欢

Echart stack histogram: add white spacing effect setting between color blocks

Included angle of 3D vector

Chapter 10 setting up structured logging (2)

嵌入式virlog代码运行流程

Variable declaration of typescript

Update and download of Beifu EtherCAT XML description file

Beifu PLC based on NT_ Shutdown to realize automatic shutdown and restart of controller
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system

Ring queue PHP

Mysql database explanation (III)
随机推荐
[proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
Detailed sorting of HW blue team traceability process
[shell] generate strings between specified dates
ES基於Snapshot(快照)的數據備份和還原
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
Uva11582 [fast power]colossal Fibonacci numbers!
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
Log in to the server using SSH key pair
Electron official docs series: Best Practices
Beifu cx5130 card replacement and transfer of existing authorization files
HDU 3555 Bomb
Chapter 10 setting up structured logging (2)
HW蓝队溯源流程详细整理
基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
Stack, LIFO
Included angle of 3D vector
H5视频自动播放和循环播放
HDU 3709 Balanced Number
MySQL数据库讲解(三)
遍历指定目录获取当前目录下指定后缀(如txt和ini)的文件名