java相关:详解使用Spring Cloud Consul实现服务的注册和发现
发布于 2020-5-30|
复制链接
摘记: 项目规划,2个服务端,1个客户端首先来看服务端,一:服务端1:项目依赖
```xml
..
项目规划,2个服务端,1个客户端首先来看服务端,一:服务端1:项目依赖
```xml
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 路径来判断服务是否健康
```java
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 配置内容
```plain
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 输出如下:
```javascript
{
"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项目依赖和上面一样
```java
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 配置内容:
```plain
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-discoveryapplication.properties 配置内容:
```plain
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注册一个服务
```java
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 查看效果了。