本文主要介绍在 SpringCloudAlibaba 和 Nacos 微服务框架下,使用 openfeign 的 FeignClient 实现客户端负载均衡的效果。关于服务提供者和服务消费者的程序请直接查阅这篇文章。基于 SpringCloudAlibaba 和 Nacos 构建微服务入门实例
OpenFeign 简介
Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness.
打包服务提供者程序
将基于 SpringCloudAlibaba 和 Nacos 构建微服务入门实例中的服务提供者程序打包输出:myclouds-nacos-discovery-provider-example-3.0.0.jar。
启动 2 个服务提供者实例
java -jar myclouds-nacos-discovery-provider-example-3.0.0.jar --server.port=18000
java -jar myclouds-nacos-discovery-provider-example-3.0.0.jar --server.port=18001
启动服务消费者程序
将基于 SpringCloudAlibaba 和 Nacos 构建微服务入门实例中的服务提供者程序打包输出:myclouds-nacos-discovery-consumer-example-3.0.0.jar。
java -jar myclouds-nacos-discovery-consumer-example-3.0.0.jar --server.port=18000
查看 Nacos 服务状态
验证负载均衡效果
请求 http://127.0.0.1:19000/bankcard/findCardInfo?cardNo=5301001,查看服务提供者控制台。
小结
springcloudalibaba 下 feignclient 的负载均衡实现也是依赖 netflix-ribbon。在 spring-cloud-starter-alibaba-nacos-discovery pom 中对其进行了依赖 spring-cloud-starter-netflix-ribbon。