SpringCloud的EurekaClient : 客户端应用访问注册的微服务(无断路器场景)
2017-05-04 10:09
459 查看
演示客户端应用如何访问注册在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+端口.
@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);
}
http://localhost:8301/HelloARibbon?name=lexiaofei
2、通过Feign访问HelloA
http://localhost:8321/HelloAFeign?name=liangshengqi
发现服务端调用是在8201 和 8202之间负载均衡的。
一、概念和定义
采用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访问HelloAhttp://localhost:8301/HelloARibbon?name=lexiaofei
2、通过Feign访问HelloA
http://localhost:8321/HelloAFeign?name=liangshengqi
发现服务端调用是在8201 和 8202之间负载均衡的。
四、代码下载
https://github.com/lexiaofei/workspace_SpringRibbonFeign.git相关文章推荐
- SpringCloud的EurekaClient : 客户端应用访问注册的微服务(有断路器场景)
- 【Spring Cloud】二、Eureka Client 服务注册中心客户端启动注册服务
- SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务
- SpringCloud(第 005 篇)电影微服务,也注册到 EurekaServer 中,通过 Http 协议访问已注册到生态圈中的用户微服务
- springcloud入门之服务提供者client并注册到eureka
- SpringCloud教程第一篇:服务的注册与发现(Eureka)
- 建立eureka服务和客户端(客户端获取已经注册服务)
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
- Eureka 的 Application Client 客户端的运行示例
- Windows Server 2012 R2 DirectAccess功能测试(8)―配置NAT客户端及Client访问测试
- SpringCloud(第 007 篇)电影微服务,使用定制化 Ribbon 在客户端进行负载均衡,使用 RibbonClient 不同服务不同配置策略
- 注册中心 Eureka 源码解析 —— Eureka-Client 初始化(一)之 EurekaInstanceConfig
- Sqoop密码访问的3种应用场景解读
- springcloud-1.使用Eureka -robbon构建自动服务发现注册集群和消费者
- crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理
- Simple SJClientLibs (简单客户端应用封装)
- 【WEB API项目实战干货系列】- API访问客户端(WebApiClient适用于MVC/WebForms/WinForm)(四)
- 关于web程序调试相关工具谷歌访问助手,和测试 POST 请求客户端工具,Advanced REST Client的下载和安装
- SpringCloud将服务者和提供者注册到注册中心(Eureka)