简介:consul是一个开源分布式服务发现的配置管理系统,提供服务治理、配置中心、控制总线等功能。这些功能中的每个都可以单独使用,也可以一起使用
能做什么:服务发现:提供HTTP和DNS两种发现方法,
健康检测:支持多种方式,HTTP,TCP,Docker,Shell加班定制化,
kv存储:key,value的存储方式,
多数据中心:consel支持多数据中心,
可视化web界面
安装下载:
[root@centos79644centos 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 用时 20s2023-06-12 23:26:58 (1.95 MB/s) - 已保存 “consul_1.9.2_linux_amd64.zip” [41313084/41313084])
解压目录
[root@centos79644centos4centos consul]# unzip consul_1.9.2_linux_amd64.zipArchive: consul_1.9.2_linux_amd64.zip inflating: consul [root@centos79644centos4centos consul]# lsconsul consul_1.9.2_linux_amd64.zip
启动
[root@centos79644centos consul]# ./consulUsage: 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.13: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: 8006springpring: 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@EnableDiscoveryClientpublic class paymentmain8006aplication { public static void main(String[] args) { SpringApplication.run(paymentmain8006aplication.class, args); }}
创建controler
@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: 90spring: 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@EnableDiscoveryClientpublic class OrderMainconsul90aplication { public static void main(String[] args) { SpringApplication.run(OrderMainconsul90aplication.class, args); }}
创建Restemplatetetet bean对象
@Configurationpublic class OrderConfig { @Bean @LoadBalanced///不加总是报错java.net.UnknownHostException: consul-provider-payment,自己试一下 public RestTemplate getRestTemplate(){ return new RestTemplate(); }}
创建controler
@RestControllerpublic 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
