详解使用Spring Cloud Consul实现服务的注册和发现
首先安装consul环境,参照之前的文章:https://www.jb51.net/article/141789.htm
项目规划,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 查看效果了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
java打印表格 将ResultSet中的数据打印成表格问题
这篇文章主要介绍了java打印表格 将ResultSet中的数据打印成表格问题。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-12-12关于IDEA2020.1新建项目maven PKIX 报错问题解决方法
这篇文章主要介绍了关于IDEA2020.1新建项目maven PKIX 报错问题解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-06-06SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据
在处理大量数据时,为了提高效率和性能,通常需要采用批量插入的方式,本文主要介绍了SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据,具有一定的参考价值,感兴趣的可以了解一下2024-03-03
最新评论