Cloud整合Zookeeper代替Eureka-CSDN博客
Consul简介
Consul是一套开源的分布式服务发现和配置管理系统
What is Consul? | Consul | HashiCorp DeveloperConsul is a service networking solution that delivers service discovery, service mesh, and network security capabilities. It supports multi-cloud infrastructure by automating connectivity between cloud providers. Learn how Consul can help you scale operations and provide high availability across your network.https://www.consul.io/intro/index.html
由HashiCorp公司用Go语言开发
提供了微服务系统中的服务治理,配置中心,控制总线等功能,这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案.
如果没有Alibaba的Nacos 那么Consul ..........学话
基于raft(是一种用于实现分布式系统中的一致性算法,它是为了易于理解和实现而设计的)协议,比较简洁,支持健康检查,同时支持HTTP和DNS协议,支持跨数据中心的wan集群,提供图形界面,跨平台,支持Linux,Mac,Window
Spring Cloud中文网-官方文档中文版
Spring Cloud Consul 中文文档 参考手册 中文版
Consul安装
Install | Consul | HashiCorp Developer
uname -m
打开您的 Linux 终端或控制台,输入以上任何一个命令,然后按下回车键,就可以看到您的系统架构信息。如果您看到的是 x86_64、i686 或 i386,那么您的系统是基于 AMD 或 Intel 的 x86 架构。如果您看到的是 arm、arm64、aarch64 等,那么您的系统是基于 ARM 架构
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install consul
which consul
nohup consul agent -dev -ui -node=consul-dev -client=122.22.222.222 &
注意后面是内网ip
先这么玩着
启动后
IP:8500
支付模块(生产者)
控制器
@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();} }
启动类 @EnableDiscoveryClient可以不加
application.yml
server:port: 8086spring:application:name: consul-provider-paymentcloud:consul:host: 2222.222.222.22port: 8500discovery:service-name: ${spring.application.name}# 打开心跳机制 不然前面带红Xheartbeat:enabled: true
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>SpringCloud</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-provider-payment8086</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><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.example</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</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>
订单模块(消费者)
配置类
@Configuration
public class ApplicationContextConfig {@LoadBalanced@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}}
业务类
@RestController
@Slf4j
public class OrderConsulController {public static final String INVOME_URL = "http://consul-provider-payment";@Resourceprivate RestTemplate restTemplate;@GetMapping("/consumer/payment/consul")public String payment (){String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);return result;}}
启动类
@SpringBootApplication
//@EnableDiscoveryClient//老版本需要 新版不用
public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class, args);}
}
application.yml
server:port: 80spring:application:name: consul-consumer-ordercloud:consul:host: 222.222.22.2port: 8500discovery:service-name: ${spring.application.name}# 打开心跳机制 不然前面带红Xheartbeat:enabled: true
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>SpringCloud</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumerconsul-order80</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><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.example</groupId><artifactId>cloud-api-commons</artifactId><version>1.0-SNAPSHOT</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>