SpringCloud的EurekaClient : 客户端应用访问注册的微服务(无断路器场景)

演示客户端应用如何访问注册在EurekaServer里的微服务

一、概念和定义

采用Ribbon或Feign方式访问注册到EurekaServer中的微服务。
1、Ribbon实现了客户端负载均衡,
2、Feign底层调用Ribbon
3、注册在EurekaServer中的微服务api,不通过ip访问,而是通过ServiceId访问
其中ServiceId即配置项spring.application.name标识的注册到EurekaServer中的名称。
使用Feign/Ribbon客户端访问微服务的api,如:
http://ServiceHelloA/HelloA?name=tom
http://ServiceHelloB/HelloB?name=Jerry
说明客户端负载均衡Ribbon解释了ServiceHelloA映射的ip+端口.

二、开发案例

1、Ribbon服务代码
@Service
public class HelloARibbonService {

@Autowired
RestTemplate restTemplate;

public String visitHelloA(String name) {
return restTemplate.getForObject("http://SERVICEHELLOA/HelloA?name="+name,String.class);
}

}

2、Feign服务代码
@FeignClient(value = "SERVICEHELLOA")
public interface HelloAFeignService {

@RequestMapping(value = "/HelloA",method = RequestMethod.GET)
String visitHelloA(@RequestParam(value = "name") String name);

}

三、案例说明

1、通过Ribbon访问HelloA

http://localhost:8301/HelloARibbon?name=lexiaofei

2、通过Feign访问HelloA

http://localhost:8321/HelloAFeign?name=liangshengqi

发现服务端调用是在8201 和 8202之间负载均衡的。

四、代码下载

https://github.com/lexiaofei/workspace_SpringRibbonFeign.git

原文地址:https://www.cnblogs.com/lexiaofei/p/6805522.html