当前位置:网站首页>Consul服务注册与发现
Consul服务注册与发现
2022-06-26 05:26:00 【RB_VER】
Consul简介
Consul是一套开源的分布式服务发现和配置管理系统,由HashiCorp公司用Go语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点,包括:基于raft协议,比较简洁;支持健康检查,同时支持HTTP和DNS协议;支持跨数据中心的WAN集群;提供图形界面;跨平台。支持Linux、Mac、Windows。
安装运行Consul
官网下载,文件中只有consul.exe文件,在cmd中输入consul agent -dev启动。
在浏览器访问http://localhost:8500打开consul控制界面。
服务提供者
创建cloud-providerconsul-payment8006模块。
pom.xml
<?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>cloud2022</artifactId>
<groupId>com.qrxqrx.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-providerconsul-payment8006</artifactId>
<dependencies>
<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-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>
application.yml
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${
spring.application.name}
PaymentMain8006
package com.qrxqrx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
PaymentController
package com.qrxqrx.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/consul")
public String paymentConsul() {
return "consul: " + serverPort + UUID.randomUUID().toString();
}
}
启动模块后,查看consul控制界面:
服务消费者
创建cloud-consumerconsul-order80模块。
pom.xml
<?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>cloud2022</artifactId>
<groupId>com.qrxqrx.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerconsul-order80</artifactId>
<dependencies>
<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-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>
application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${
spring.application.name}
OrderConsulMain80
package com.qrxqrx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
OrderController
package com.qrxqrx.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderController {
public static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/consul")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
return result;
}
}
consul界面:
三个注册中心异同点
| 组件名 | 语言 | CAP | 服务健康检查 | 对外暴露接口 | spring cloud集成 |
|---|---|---|---|---|---|
| Eureka | Java | AP | 可配支持 | HTTP | 已集成 |
| Consul | Go | CP | 支持 | HTTP/DNS | 已集成 |
| Zookeeper | Java | CP | 支持 | 客户端 | 已集成 |
AP:当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。
CP:当网络分区出现后,为了保证一致性,就必须拒接请求,否则无法保证一致性。
边栏推荐
- The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
- 程序人生
- Create SSH key pair configuration steps
- Leetcode513. Find the value in the lower left corner of the tree
- Briefly describe the pitfalls of mobile IM development: architecture design, communication protocol and client
- uniCloud云开发获取小程序用户openid
- Two step processing of string regular matching to get JSON list
- cartographer_backend_constraint
- Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
- 百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
猜你喜欢

cartographer_backend_constraint

cartographer_local_trajectory_builder_2d

Setting pseudo static under fastadmin Apache

Introduction to alluxio

Windows下安装Tp6.0框架,图文。Thinkphp6.0安装教程

【ARM】讯为rk3568开发板buildroot添加桌面应用

Classic theory: detailed explanation of three handshakes and four waves of TCP protocol

ECCV 2020 double champion team, take you to conquer target detection on the 7th

递归遍历目录结构和树状展现

As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
随机推荐
Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
Security problems in wireless networks and modern solutions
Leetcode114. 二叉树展开为链表
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.
Learn from small samples and run to the sea of stars
Uni app ceiling fixed style
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
uniCloud云开发获取小程序用户openid
Daily production training report (16)
cartographer_ optimization_ problem_ 2d
9 common classes
Redis usage and memory optimization
[greedy college] Figure neural network advanced training camp
Codeforces Round #800 (Div. 2)
基于SDN的DDoS攻击缓解
出色的学习能力,才是你唯一可持续的竞争优势
[arm] build boa based embedded web server on nuc977
When was the autowiredannotationbeanpostprocessor instantiated?