简介:consul是一套开源的分布式服务发现的配置管理系统,提供了微服务系统中的服务治理,配置中心,控制总线等功能。这些功能中的每一个可以更具需要单独使用,也可以一起
可以干嘛:服务发现:提供HTTP和DNS两种发现方式,
健康检测:支持多种方式,HTTP,TCP,Docker,Shell加班呢定制化,
kv存储:key,value的存储方式,
多数据中心:consel支持多数据中心,
可视化web界面
安装和下载:
[root@centos7964 consul]# wget https://releases.hashicorp.com/consul/1.9.2/consul_1.9.2_linux_amd64.zip
--2023-06-12 23:26:36-- https://releases.hashicorp.com/consul/1.9.2/consul_1.9.2_linux_amd64.zip
正在解析主机 releases.hashicorp.com (releases.hashicorp.com)... 13.226.120.110, 13.226.120.68, 13.226.120.47, ...
正在连接 releases.hashicorp.com (releases.hashicorp.com)|13.226.120.110|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:41313084 (39M) [application/zip]
正在保存至: “consul_1.9.2_linux_amd64.zip”
100%[==========================================================================================================================================>] 41,313,084 3.21MB/s 用时 20s
2023-06-12 23:26:58 (1.95 MB/s) - 已保存 “consul_1.9.2_linux_amd64.zip” [41313084/41313084])
解压目录
[root@centos7964 consul]# unzip consul_1.9.2_linux_amd64.zip
Archive: consul_1.9.2_linux_amd64.zip
inflating: consul
[root@centos7964 consul]# ls
consul consul_1.9.2_linux_amd64.zip
启动
[root@centos7964 consul]# ./consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
开发环境启动命令如下(默认是8500端口):
./consul agent -dev -client=0.0.0.0 -ui
浏览器访问:192.168.133.130:8500
consul的使用
创建服务提供方:paymentMain8006,创建项目时已选搜索consul,自动注入,pom文件加入依赖为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
配置文件:application.yml
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: 192.168.133.130 # ??????
port: 8500 # ??????
discovery:
register: true # ????
instance-id: ${spring.application.name}-01 # ???? id ????
service-name: ${spring.application.name} # ????
port: ${server.port} # ????
prefer-ip-address: true # ???? ip ??
ip-address: ${spring.cloud.client.ip-address} # ???? ip
heartbeat:
enabled: true
主启动类上加注解:@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006Application {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006Application.class, args);
}
}
创建controller
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Value("${server.port}")
private String port;
@RequestMapping("/consul")
public String getPort(){
return "spring-boot-consul的端口号为:"+port;
}
}
创建服务调用方:orderMainConsul90,创建时加入依赖,服务提供方和调用方依赖相同,pom.xml
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
server:
port: 90
spring:
application:
name: cloud-consumer-order
cloud:
consul:
host: 192.168.133.130 # ??????
port: 8500 # ??????
discovery:
register: false # ????
instance-id: ${spring.application.name}-01 # ???? id ????
service-name: ${spring.application.name} # ????
port: ${server.port} # ????
prefer-ip-address: true # ???? ip ??
ip-address: ${spring.cloud.client.ip-address} # ???? ip
heartbeat:
enabled: true # ????????
在主启动类上加载注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class OrderMainConsul90Application {
public static void main(String[] args) {
SpringApplication.run(OrderMainConsul90Application.class, args);
}
}
创建RestTemplate bean对象
@Configuration
public class OrderConfig {
@Bean
@LoadBalanced//不加一直报错java.net.UnknownHostException: consul-provider-payment,自己试一下
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
创建controller类
@RestController
public class OrderConsulController {
@Autowired
RestTemplate restTemplate;
private static final String INVOKE_URL = "http://consul-provider-payment";
@RequestMapping("/getOrder")
public String getOrder(){
return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
}
}
浏览器或者postman掉用:http://localhost:90/getOrder