Consul可以用于实现分布式系统的服务发现与配置
1.服务注册发现中心
启动Consul
consul agent -dev
2.服务端
添加服务provider,provider1
provider测试方法
packagecom.xyz.provider.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;
@RestControllerpublic classdemoController {
@RequestMapping("/hello")publicString Hello(){return "hello,provider";
}
}
provider1测试方法
packagecom.xyz.provider1.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;
@RestControllerpublic classdemoController {
@RequestMapping("/hello")publicString Hello(){return "hello,another provider";
}
}
启动provider和provider1
浏览器访问http://localhost:8500
有两个服务提供者节点实例
3.客户端
(1)添加依赖
1.8
Greenwich.SR4
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
(2)添加配置
server.port=8015spring.application.name=xyz-comsumer
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500spring.cloud.consul.discovery.register=falsespring.cloud.consul.discovery.health-check-url=/actuator/health
spring.cloud.consul.discovery.heartbeat.enabled=truemanagement.endpoints.web.exposure.include=*management.endpoint.health.show-details=always
(3)测试方法
packagecom.xyz.comsumer.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cloud.client.ServiceInstance;importorg.springframework.cloud.client.discovery.DiscoveryClient;importorg.springframework.cloud.client.loadbalancer.LoadBalancerClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;
@RestControllerpublic classHelloController {
@AutowiredprivateLoadBalancerClient loadBalancer;
@AutowiredprivateDiscoveryClient discoveryClient;private String serviceName = "service-provider";
@RequestMapping("/services")publicObject services() {returndiscoveryClient.getInstances(serviceName);
}
@RequestMapping("/discover")publicObject discover() {returnloadBalancer.choose(serviceName).getUri().toString();
}
@RequestMapping("/hello")publicString hello() {
ServiceInstance serviceInstance=loadBalancer.choose(serviceName);
String callServiceResult= new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);returncallServiceResult;
}
}
说明:
客户端调用的服务名,是在服务端指定的,在服务端配置里使用 spring.cloud.consul.discovery.service-name指注册到 Consul 的服务名称
(4)测试
启动Consul
启动provider和provider1
启动comsumer
http://localhost:8015/services
返回结果
[
{"instanceId": "provider-8010","serviceId": "service-provider","host": "hkgi-PC","port": 8010,"secure": false,"metadata": {"secure": "false"},"uri": "http://hkgi-PC:8010","scheme": null},
{"instanceId": "provider-8011","serviceId": "service-provider","host": "hkgi-PC","port": 8011,"secure": false,"metadata": {"secure": "false"},"uri": "http://hkgi-PC:8011","scheme": null}
]
http://localhost:8015/discover
返回结果
http://hkgi-PC:8011 或 http://hkgi-PC:8011
http://localhost:8015/hello
返回结果
hello,provider 或 hello,another provider
结果交替出现的,这是因为负载均衡器是采用的是轮询的方式