怎么在SpringCloud中使用Consul实现服务注册和发现
这期内容当中小编将会给大家带来有关怎么在Spring Cloud中使用Consul实现服务注册和发现,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联建站为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都做网站、成都网站设计、成都外贸网站建设负责任的成都网站制作公司!
一:服务端1:
项目依赖
org.springframework.cloud spring-cloud-consul-dependencies 1.0.1.RELEASE pom import org.springframework.boot spring-boot-actuator 1.3.5.RELEASE org.springframework.cloud spring-cloud-starter-consul-discovery
注意,增加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); } }
上述就是小编为大家分享的怎么在Spring Cloud中使用Consul实现服务注册和发现了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
名称栏目:怎么在SpringCloud中使用Consul实现服务注册和发现
分享地址:http://hbruida.cn/article/jghspc.html