目錄
微服務:整合 Spring Cloud Eureka - 注冊中心 Eureka Server
微服務:整合 Spring Cloud Eureka - 服務注冊 Eureka Client
微服務:整合 Spring Cloud Eureka - 服務發現 DiscoveryClient
微服務:整合 Spring Cloud Eureka - 服務消費以及Ribbon簡單使用
微服務:整合 Spring Cloud Eureka - 高可用集群
微服務:整合 Spring Cloud Eureka - .NET Core Mvc Api (C#)
微服務:整合 Spring Cloud Eureka - 服務治理機制
微服務:整合 Spring Cloud Eureka - 服務事件監聽
微服務:整合 Spring Cloud Eureka - 高級屬性Region、Zone
微服務:整合 Spring Cloud Eureka - Rest接口文檔
微服務:整合 Spring Cloud Eureka - Security 安全保護
一、前言
前面三篇文章已經介紹如何搭建Eureka注冊中心以及服務注冊、服務發現。本篇文章將會介紹如何使用Ribbon調用服務,並演示在客戶端如何使用負載均衡。
二、Ribbon中RestTemplate的使用
1、代碼結構
2、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-register</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-service-consumer</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka-client 服務注冊與發現 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- eureka-client 服務注冊與發現 --> <!-- ribbon 負載均衡 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!-- ribbon 負載均衡 --> </dependencies> </project>
3、RibbonConfig:
package com.demo.service.consumer.config; import 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 RibbonConfig { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
RibbonConfig 類中很簡單,只是配置了一個 Ribbon中的RestTemplate,並聲明是一個Bean。
4、ConsumerHelloController
package com.demo.service.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ConsumerHelloController { @Autowired RestTemplate restTemplate; @RequestMapping("/hello/{name}") public String hello(@PathVariable("name")String name){ String ribbon_url = "http://demo-service-provider/hello/sayhello/" + name; String msg =restTemplate.getForEntity(ribbon_url,String.class).getBody(); System.out.println("服務返回信息為:" + msg); return "服務返回信息為:" + msg; } }
三、運行分析
1、第一步啟動Eureka注冊中心 - demo-register,可以參考《微服務:整合 Spring Cloud Eureka - 注冊中心 Eureka Server》。
2、第二步啟動服務提供者 - demo-service-provider,可以參考 《微服務:整合 Spring cloud Eureka - 服務注冊 Eureka Client 》 。
3、第三步啟動服務消費者 - demo-service-consumer。
4、在瀏覽器中打開:http://localhost:8201/hello/tom
服務消費者使用Ribbon-restTemplete調用服務已經成功。
四、總結
@RequestMapping("/hello/{name}") public String hello(@PathVariable("name")String name){ String ribbon_url = "http://demo-service-provider/hello/sayhello/" + name; String msg =restTemplate.getForEntity(ribbon_url,String.class).getBody(); System.out.println("服務返回信息為:" + msg); return "服務返回信息為:" + msg; }
從以上代碼可以看出來:
1、restTemplete調用的url地址為:"http://demo-service-provider/hello/sayhello/" + name。
2、Ribbon拿到這個地址后,會解析出路由中的ServiceId:demo-service-provider
3、通過demo-service-provider找到服務提供者的ip:port 列表。
4、通過ip:port 列表按照負載均衡策略選出一個url地址。
5、重新拼接url地址,此時才開始真正的調用遠程服務:demo-service-provider