Consul官网
官网:https://www.consul.io/intro/index.html
什么是Consul
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows。
下载
下载:https://www.consul.io/downloads.html
安装说明:https://learn.hashicorp.com/consul/getting-started/install.html
Spring Cloud配置Consul
https://www.springcloud.cc/spring-cloud-consul.html
下载完成后
进入cmd控制台
输入consul --version 查看版本号
输入consul agent -dev 运行
访问Consul的首页:http://localhost:8500
改pom
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
写yml
###consul服务端口号
server:
port: 8006
spring:
application:
name: consul-provider-payment
####consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
主启动类
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class PaymentMain8006
{
public static void main(String[] args)
{
SpringApplication.run(PaymentMain8006.class,args);
}
}
controller
@RestController
public class PaymentController
{
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/consul")
public String paymentInfo()
{
return "springcloud with consul: "+serverPort+"\t\t"+ UUID.randomUUID().toString();
}
}
访问http://localhost:8006/payment/consul