当前位置:网站首页>Consul服务注册与发现
Consul服务注册与发现
2022-07-17 07:23:00 【幻清】
单机版
1.Consul简介
Consul是一套开源的分布式服务发 现和配置管理系统,由HashiCorp公司用Go语言开发。
提供了微服务系统中的服务治理。配置中心控制总线等功能。这些功能中的每一个都可以根据需要单独使用, 也可以一 起使用以构成全方位的服务网络,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括:基于raft 协议,比较简洁;支持健康检查 同时支持HTTP和DNS协议支持跨数据中心的WAN集群提供图形界面跨平台,支持Linux、Mac、 Windows。
1.1能干嘛?
1.2 Consul下载地址
1.3 怎么玩?
中文版教程
2.安装并运行Consul
2.1官网安装说明
官网安装说明
mac系统安装Consul见
mac安装brew和Consul
3.服务提供者
3.1新建Module支付服务provider8006
cloud-providerconsul-payment8006
3.2修改pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zhang.springcloud</groupId>
<artifactId>cloud-api-commons</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
3.3创建application.yml
#####consul服务端口号
server:
port: 8006
spring:
application:
name: consul-provider-payment
####consul注册中心地址
cloud:
consul:
host: localhost
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
3.4 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
3.5 controller类
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
4.服务消费者
4.1新建Module消费服务order8006
cloud-consumerconsul-order80
4.2 修改pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zhang.springcloud</groupId>
<artifactId>cloud-api-commons</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
4.3 创建application.yml文件
###consul服务端口号
server:
port: 80
spring:
application:
name: cloud-consumer-order
###consul注册中心
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname:127.0.0.1
service-name: ${spring.application.name}
4.4 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
4.5 配置Bean
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4.6 controller
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/consul")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
}
}
注意:
Eureka 、 Zookeeper 、 Consul三个注册中心异同点:

何为CAP?
最多只能同时较好的满足两个。
CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性这三个需求,因此,根据CAP原理将NoSQL数据库分成了满足CA原则、满足CP原则和满足AP原则三大类:
CA-单点集群,满足一致性,可用性的系统,通常在可扩展性上不太强大。
CP满足一致性,分区容忍必的系统,.通常性能不是特别高。
**AP **满足可用性,分区容忍性的系统,通常可能对一致性要求低一些。
AP架构
当网络分区出现后,为了保证可用性,系统B可以返回旧值,保证系统的可用性。结论:违背了一 致性C的要求,只满足可用性和分区容错,即AP
CP架构
当网络分区出现后,为了保证一致性, 就必须拒接请求,否则无法保证致性结论:违背了可用性A的要求,只满足-致性和分区容错, 即CP

边栏推荐
- Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi
- 超干货!彻底搞懂Golang内存管理和垃圾回收
- How to check whether the app has user information and data leakage vulnerabilities
- History and value of forked coins | eth, BCH, BSV 2020-03-08
- visual studio 2022(VS 2022)无法读取内存的问题
- Leetcode daily question 2021/7/11-2021/7/17
- What if the user information in the website app database is leaked and tampered with
- No module named ‘yaml‘ 解决办法
- Bean、
- TextView文字上下移动
猜你喜欢

Visual studio production environment configuration scheme: slowcheetah

Shenzhen Prudential written examination record

812. 最大三角形面积

YOLOV5-打标签建立自己的数据集

Visual Studio 生产环境配置方案:SlowCheetah

Redis distributed lock

oop_引用类型变量传值

Redis cluster

From the casino logic, analyze the investment value of platform currency 2020-03-03

V8 引擎如何进行垃圾内存的回收?
随机推荐
Niuke topic - house raiding 1, house raiding 2
警惕!又一起网络钓鱼攻击事件:Uniswap被盗810万美元
Real case: how to check the soaring usage of CPU after the system goes online?
Go language Bible
DP dynamic planning enterprise level template analysis (Digital triangle, rising sequence, knapsack, state machine, compressed DP)
TextView文字上下移动
SPARK中的FileSourceStrategy,DataSourceStrategy以及DataSourceV2Strategy
Unity custom sky ball model to prevent it from being cropped
YOLOV5-打标签建立自己的数据集
《痞子衡嵌入式半月刊》 第 58 期
5.1 安全漏洞与防范
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
演示集合注入
最新一代互联网:WEB 3.0
oop_引用类型变量传值
Deep learning 7 deep feedforward network 2
總結的太好了!終於有人把SQL的各種連接Join都講明白了
Local storage sessionstorage
Understanding of fast and slow pointer
Detailed explanation of type, user-defined type, preliminary understanding of structure