javascript
SpringCloud教程- 服务消费者(rest+ribbon)(SpringCloud版本Finchley)
文章目錄
- 一、ribbon介紹
- 二、啟動兩個實例
- 三、建一個服務消費者(ribbon)
- 四、 Ribbon調用總結
代碼地址:github-spring-cloud地址
前言:在上一篇文章,講了服務的注冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基于http
restful的。Spring
cloud有兩種服務調用方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下ribbon+rest。
一、ribbon介紹
官方文檔簡介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
Ribbon是客戶端負載平衡器,它使您可以對HTTP和TCP客戶端的行為進行大量控制。
Feign已經使用了Ribbon,因此如果您使用@FeignClient,則本節也適用。
ribbon是一個負載均衡客戶端,可以很好的控制htt和tcp的一些行為。Feign默認集成了ribbon。
二、啟動兩個實例
這篇文章基于上一篇文章啟動eureka-server 工程;啟動service-hello工程,它的端口為8081;將service-hello的配置文件的端口改為8083,并啟動:service-hello在eureka-server注冊了2個實例,這就相當于一個小的集群。
三、建一個服務消費者(ribbon)
新建一個工程server-ribbon,引入pom文件
<?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-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version><relativePath>..</relativePath></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>server-ribbon</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency></dependencies></project>修改配置文件
server:port: 8082spring:application:name: server-ribboneureka:client:serviceUrl:defaultZone: http://localhost:8080/eureka/management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwaysshutdown:enabled: true在工程的啟動類中,通過@EnableDiscoveryClient向服務中心注冊;注入一個bean: restTemplate;并通過@LoadBalanced注解表明這個restRemplate開啟負載均衡的功能。
package com.sl.cloud.learn;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;/*** @author shuliangzhao* @Title: ServerRibbonApplication* @ProjectName spring-cloud-learn* @Description: TODO* @date 2019/11/24 15:59*/ @SpringBootApplication @EnableDiscoveryClient public class ServerRibbonApplication {public static void main(String[] args) {SpringApplication.run( ServerRibbonApplication.class, args );}@Bean@LoadBalancedRestTemplate restTemplate() {`在這里插入代碼片`return new RestTemplate();}}寫一個測試類HelloService
@Service public class HelloService {@Autowiredprivate RestTemplate restTemplate;public String helloService(String name) {return restTemplate.getForObject("http://EUREKA-HELLO/hi?name="+name,String.class);} }寫一個controller調用類
@RestController public class HelloController {@Autowiredprivate HelloService helloService;@GetMapping(value = "/hi")public String helloService(String name) {return helloService.helloService(name);}}在瀏覽器上多次訪問http://localhost:8082/hi?name=zhansan,瀏覽器交替顯示
hi zhansan ,my port is:8083
hi zhansan ,my port is:8081
四、 Ribbon調用總結
服務注冊中心,eureka server,端口為8080
eureka-hello工程跑了兩個實例,端口分別為8081,8983,分別向服務注冊中心注冊
sercvice-ribbon端口為8082,向服務注冊中心注冊
當sercvice-ribbon通過restTemplate調用eureka-hello的hi接口時,因為用ribbon進行了負載均衡,會輪流的調用eureka-hello:8081,8983 兩個端口的hi接口;
總結
以上是生活随笔為你收集整理的SpringCloud教程- 服务消费者(rest+ribbon)(SpringCloud版本Finchley)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git pull代码出现refusing
- 下一篇: Durid数据库连接池设置Connect