当前位置:网站首页>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)


边栏推荐
- Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
- I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
- Mysql database explanation (III)
- Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
- Electron official docs series: Best Practices
- Echart stack histogram: add white spacing effect setting between color blocks
- 创建一个自己的跨域代理服务器
- Taishan Office Technology Lecture: four cases of using bold font
- Wechat applet -picker component is repackaged and the disabled attribute is added -- above
- Detailed sorting of HW blue team traceability process
猜你喜欢

Calculate the distance between two points (2D, 3D)

NVM installation tutorial

Electron official docs series: Get Started

Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias

33. Use rgbd camera for target detection and depth information output

HW蓝队溯源流程详细整理

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

Reprint - easy to use wechat applet UI component library

防火墙介绍

Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
随机推荐
HDU 3709 Balanced Number
Composite mode
Map value
MySQL数据库讲解(五)
Calculate the distance between two points (2D, 3D)
ES6:Map
7-1 range of numbers
Electron official docs series: Best Practices
【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
Detailed sorting of HW blue team traceability process
计算两点之间的距离(二维、三维)
Reflect the technical depth (unable to speed up)
李航老师新作《机器学习方法》上市了!附购买链接
SQL assigns the field value of data table B to a column in data table a
Coprime and non coprime of template problems of Chinese remainder theorem
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
Basic type of typescript
Oplg: new generation cloud native observable best practices
Beifu PLC realizes data power-off maintenance based on cx5130