创建过程和provider一样 测试方法换一下,并且在启动类上添加RestTemplate Bean
7.3.1 配置application.yml server: port: ${port:8081} spring: application: name: consumer cloud: consul: #consul服务地址 host: 127.0.0.1 #consul服务端口 port: 8500 discovery: #是否注册 register: true #实例ID # instance-id: ${spring.application.name}-${server.port} instance-id: ${spring.application.name}:${random.value} #服务实例名称 service-name: ${spring.application.name} #服务实例端口 port: ${server.port} #健康检查路径 healthCheckPath: /actuator/health #健康检查时间间隔 healthCheckInterval: 15s #开启ip地址注册 prefer-ip-address: true #实例的请求ip ip-address: ${spring.cloud.client.ip-address} metadata: #添加自定义元数据 my-name: zhangtieniu-consumer 7.3.2 添加支持负载均衡的RestTemplate package com.ldx.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ConsumerApplication { @Bean @LoadBalanced public RestTemplate loadbalancedRestTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } 7.3.3 添加测试方法 package com.ldx.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class TestController { @Autowired private RestTemplate restTemplate; @GetMapping() public String consumer(){ return this.restTemplate.getForObject("http://provider/products", String.class); } } 7.4 启动启动了两个 provider 和一个 consumer
浏览器输入localhost:8500 查看consul控制台,显示服务注册成功
测试服务调用
其中provider 输出的 实例信息如下:
[ConsulServiceInstance@4c2b7437 instanceId = 'consumer-6cfd981c90545313155d1f43c3ed23a5', serviceId = 'consumer', host = '192.168.0.101', port = 8081, secure = false, metadata = map['my-name' -> 'zhangtieniu-consumer', 'secure' -> 'false'], uri = :8081, healthService = HealthService{node=Node{id='3fe6ea9e-3846-ff8d-b01f-a6528caaa3fd', node='44a66c1caa9c', address='172.26.0.2', datacenter='dc1', taggedAddresses={lan=172.26.0.2, lan_ipv4=172.26.0.2, wan=172.26.0.2, wan_ipv4=172.26.0.2}, meta={consul-network-segment=}, createIndex=11, modifyIndex=13}, service=Service{id='consumer-6cfd981c90545313155d1f43c3ed23a5', service='consumer', tags=[], address='192.168.0.101', meta={my-name=zhangtieniu-consumer, secure=false}, port=8081, enableTagOverride=false, createIndex=275, modifyIndex=275}, checks=[Check{node='44a66c1caa9c', checkId='serfHealth',, status=PASSING, notes='', output='Agent alive and reachable', serviceId='', serviceName='', serviceTags=[], createIndex=11, modifyIndex=11}, Check{node='44a66c1caa9c', checkId='service:consumer-6cfd981c90545313155d1f43c3ed23a5',consumer' check', status=PASSING, notes='', output='HTTP GET :8081/actuator/health: 200 Output: {"status":"UP"}', serviceId='consumer-6cfd981c90545313155d1f43c3ed23a5', serviceName='consumer', serviceTags=[], createIndex=275, modifyIndex=278}]}]