这期内容当中小编将会给大家带来有关怎么在Spring Cloud中使用Consul实现服务注册和发现,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
一:服务端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); } }
上述就是小编为大家分享的怎么在Spring Cloud中使用Consul实现服务注册和发现了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。