B站尚硅谷最新版SpringCloud 学习笔记(4)-Consul服务注册与发现
上一篇:B站尚硅谷最新版SpringCloud 学习笔记(3)-Zookeeper服务注册与发现
B站视频地址:https://www.bilibili.com/video/BV18E411x7eT
文章目录
- B站尚硅谷最新版SpringCloud 学习笔记(4)-Consul服务注册与发现
- 上一篇:[B站尚硅谷最新版SpringCloud 学习笔记(3)-Zookeeper服务注册与发现](https://blog.csdn.net/weixin_42417934/article/details/112712676)
- 一、Consul简介
- 二、Centos7安装并运行Consul
- 三、三个注册中心异同点
- 下一篇:[B站尚硅谷最新版SpringCloud 学习笔记(5)-Ribbon负载均衡和OpenFeign服务接口调用](https://blog.csdn.net/weixin_42417934/article/details/112760705)
一、Consul简介
1.是什么
https://www.consul.io/intro/index.html
2.能干嘛
服务发现 | 提供HTTP和DNS两种发现方式 |
---|---|
健康监测 | 支持多种协议,HTTP、TCP、Docker、Shell脚本定制化 |
KV存储 | key , Value的存储方式 |
多数据中心 | Consul支持多数据中心 |
可视化Web界面 |
3.哪里下载和怎么操作
https://www.consul.io/downloads.html
https://www.springcloud.cc/spring-cloud-consul.html
二、Centos7安装并运行Consul
1.下载Consul包
进入官网https://www.consul.io/downloads.html
复制下载链接到迅雷
迅雷下载会快一点点(再下载的时间里,进行下面操作)
2.Centos7安装Consul
参考文章:https://www.cnblogs.com/yangk1996/p/12656667.html
①创建一个consul的文件夹,并修改权限
cd /usr/local
mkdir consul
chmod 777 consul
cd consul
②确认安装了
③将下载好的consul传输
④解压consul
unzip consul_1.9.1_linux_amd64.zip
验证consul是否安装完成
⑤启动consul
./consul agent -dev -ui -node=consul-dev -client=192.168.88.77
关闭防火墙 systemctl stopfirewalld
⑥访问http://192.168.88.77:8500
3.服务提供者
①创建cloud-providerconsul-payment8006
②修改pom,添加application.yml,添加启动类
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>mycloud2020</artifactId>
<groupId>org.yezhinao</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>org.yezhinao</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>
application.yml
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: http://192.168.88.77 #家里局域网
# host: http://192.168.75.215 #公司局域网
port: 8500
discovery:
service-name: ${spring.application.name}
启动类
@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();
}
}
④验证测试
我遇到的错误
4.服务消费者
①新建cloud-consumerconsul-order80
②修改pom,添加application.yml,添加启动类
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>mycloud2020</artifactId>
<groupId>org.yezhinao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerconsul-order80</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>org.yezhinao</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>
application.yml
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul:
host: http://192.168.88.77 #家里局域网
# host: http://192.168.75.215 #公司局域网
port: 8500
discovery:
service-name: ${spring.application.name}
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class, args);
}
}
③注入RestTemplate 和 创建接口
@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;
}
}
④测试验证
输入http://localhost/consumer/payment/consul
三、三个注册中心异同点
1.CAP
-
C:Consistency(强一致性)
-
A:Availability(可用性)
-
P:Partition tolerance(分区容错)
-
CAP理论关注粒度是数据,而不是整体系统设计的策略
2.AP和CP
AP(Eureka)
CP(Zookeeper/Consul)
代码:https://gitee.com/yezhinao/SpringCloud-Learn/tree/master/mycloud2020(4)