本文主要介紹在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個服務提供者實例
<code>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/<code>
啟動服務消費者程序
將基於SpringCloudAlibaba和Nacos構建微服務入門實例中的服務提供者程序打包輸出:myclouds-nacos-discovery-consumer-example-3.0.0.jar。
<code>java -jar myclouds-nacos-discovery-consumer-example-3.0.0.jar --server.port=18000
/<code>
查看Nacos服務狀態
![基於SpringCloud和Nacos微服務體系下客戶端負載均衡入門實例](http://p2.ttnews.xyz/loading.gif)
![基於SpringCloud和Nacos微服務體系下客戶端負載均衡入門實例](http://p2.ttnews.xyz/loading.gif)
驗證負載均衡效果
請求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。
閱讀更多 Java架構胖胖 的文章