详解使用Spring Cloud Consul实现服务的注册和发现

时间:2021-05-19

首先安装consul环境,参照之前的文章:

项目规划,2个服务端,1个客户端

首先来看服务端,

一:服务端1:

项目依赖

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-consul-dependencies</artifactId> <version>1.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> <version>1.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies>

注意,增加spring-boot-actuator是为了项目可以访问/health 路径来判断服务是否健康

package com.pp.consul1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApp { @RequestMapping("/home") public Object home() { System.out.println("1111111111111"); return "OK11"; } public static void main( String[] args ) { SpringApplication.run(ConsulApp.class, args); } }

application.properties 配置内容

server.port=9955 spring.application.name=Consul-Server-1 spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.enabled=true spring.cloud.consul.discovery.enabled=true spring.cloud.consul.discovery.instanceId=tomcat1 spring.cloud.consul.discovery.serviceName=tomcat spring.cloud.consul.discovery.hostname=192.168.2.95 spring.cloud.consul.discovery.port=${server.port} spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9955/health spring.cloud.consul.discovery.healthCheckInterval=10s spring.cloud.consul.discovery.tags=dev

看过我之前文章的,应该对这些配置很清楚了。这样,一个服务端就配置写好了。

由于我们增加了@EnableDiscoveryClient注解,所以,系统启动的时候,就会向consul注册一个服务,服务的名字为tomcat, ID为tomcat1

访问consul的HTTP API /v1/catalog/service/tomcat 输出如下:

{ "Node":"192.168.1.100", "Address":"192.168.1.100", "ServiceID":"tomcat1", "ServiceName":"tomcat", "ServiceTags":["dev"], "ServiceAddress":"192.168.2.95", "ServicePort":9955, "ServiceEnableTagOverride":false, "CreateIndex":993, "ModifyIndex":1057 }

二:服务端2

项目依赖和上面一样

package com.pp.consul2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulApp { @RequestMapping("/home") public Object home() { System.out.println("2222222222222222"); return "OK22"; } public static void main( String[] args ) { SpringApplication.run(ConsulApp.class, args); } }

application.properties 配置内容:

server.port=9966 spring.application.name=Consul-Server-2 spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.enabled=true spring.cloud.consul.discovery.enabled=true spring.cloud.consul.discovery.instanceId=tomcat2 spring.cloud.consul.discovery.serviceName=tomcat spring.cloud.consul.discovery.hostname=192.168.2.95 spring.cloud.consul.discovery.port=${server.port} spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9966/health spring.cloud.consul.discovery.healthCheckInterval=10s spring.cloud.consul.discovery.tags=test

三:客户端

项目依赖,只需要spring-cloud-starter-consul-discovery

application.properties 配置内容:

server.port=9977 spring.application.name=Consul-Client spring.cloud.consul.host=192.168.1.100 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.register=false

注意,这里的spring.cloud.consul.discovery.register需要配置成false,否则系统启动的时候,会向consul注册一个服务

package com.pp.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient @RestController public class ConsulClient { @Autowired private LoadBalancerClient loadBalancer; @Autowired private DiscoveryClient discoveryClient; /** * 从所有服务中选择一个服务(轮询) */ @RequestMapping("/discover") public Object discover() { return loadBalancer.choose("tomcat").getUri().toString(); } /** * 获取所有服务 */ @RequestMapping("/services") public Object services() { return discoveryClient.getInstances("tomcat"); } public static void main( String[] args ) { SpringApplication.run(ConsulClient.class, args); } }

启动之后,就可以访问/discover,/services 查看效果了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章