整个模式只看order服务配置
配置restTemplate
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
控制台
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import com.qq.springcloud.entities.CommonResult; import com.qq.springcloud.entities.Payment; import lombok.extern.slf4j.Slf4j; 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 javax.annotation.Resource; @RestController @Slf4j public class OrderController { #这里服务集群name 不写死IP 通过restemplate 机制负载访问 public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult<Payment> create(Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class); //写操作 } @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class); } }
主启动注册至eureka
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15server: port: 90 spring: application: name: cloud-order-service eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
服务名称修改
配置更改:
效果就是隐藏ip
生产者服务可以通过EnableDiscoveryClient 获取服务信息
请在主启动使用@EnableDiscoveryClient注解
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16@Resource private DiscoveryClient discoveryClient; @GetMapping(value = "/payment/discovery") public Object discovery(){ List<String> services = discoveryClient.getServices(); for (String element : services) { log.info("***** element:"+element); } List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE"); for (ServiceInstance instance : instances) { log.info(instance.getServiceId()+"t"+instance.getHost()+"t"+instance.getPort()+"t"+instance.getUri()); } return this.discoveryClient;
最后
以上就是怕黑小蝴蝶最近收集整理的关于消费者通过eureka 集群消费生产者集群消息的全部内容,更多相关消费者通过eureka内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复