日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

深入學(xué)習(xí)Spring Cloud-Ribbon

瀏覽:41日期:2023-07-20 18:06:20
ribbon簡介

Ribbon 是 Netflix 發(fā)布的開源項目,主要功能是提供客戶端的 軟件負載均衡算法 ,將 Netflix 的中間層服務(wù)連接在一起。Ribbon 客戶端組件提供一系列完善的配置項如連接超時,重試等。簡單的說,就是在配置文件中列出Load Balancer(簡稱LB)后面所有的機器,Ribbon 會自動的幫助你基于某種規(guī)則(如簡單輪詢,隨機連接等)去連接這些機器。我們也很容易使用 Ribbon 實現(xiàn)自定義的負載均衡算法。

ribion=負載均衡+重試

深入學(xué)習(xí)Spring Cloud-Ribbon

ribbon的工作步驟:

第一步先選擇 EurekaServer ,它優(yōu)先選擇在同一個區(qū)域內(nèi)負載較少的server。 第二步再根據(jù)用戶指定的策略,在從server取到的服務(wù)注冊列表中選擇一個地址。 其中Ribbon提供了多種策略:比如輪詢、隨機和根據(jù)響應(yīng)時間加權(quán)。

深入學(xué)習(xí)Spring Cloud-Ribbon

創(chuàng)建spring ribbon項目

第一步:新建spring項目

深入學(xué)習(xí)Spring Cloud-Ribbon

第二步:添加Eureka Discovery Client,Spring Web依賴

深入學(xué)習(xí)Spring Cloud-Ribbon

第三步:添加sp01-commons工具API依賴;eureka-client 中已經(jīng)包含 ribbon 依賴

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp06-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp06-ribbon</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions> </dependency> <dependency> <groupId>cn.tedu</groupId> <artifactId>sp01-commons</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

第四步:添加yml配置

spring: application: name: ribbon #服務(wù)器命名 server: port: 3001 # 設(shè)置服務(wù)器端口號 # 配置添加注冊中心集群 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka遠程調(diào)用RestTemplate

RestTemplate 是SpringBoot提供的一個Rest遠程調(diào)用工具。

類似于 HttpClient,可以發(fā)送 http 請求,并處理響應(yīng)。RestTemplate簡化了Rest API調(diào)用,只需要使用它的一個方法,就可以完成請求、響應(yīng)、Json轉(zhuǎn)換

方法:

getForObject(url, 轉(zhuǎn)換的類型.class, 提交的參數(shù)) postForObject(url, 協(xié)議體數(shù)據(jù), 轉(zhuǎn)換的類型.class)

RestTemplate 和 Dubbo 遠程調(diào)用的區(qū)別:

RestTemplate:

http調(diào)用

效率低

Dubbo:

RPC調(diào)用,Java的序列化

效率高

第一步:創(chuàng)建RestTemplate實例

RestTemplate 是用來調(diào)用其他微服務(wù)的工具類,封裝了遠程調(diào)用代碼,提供了一組用于遠程調(diào)用的模板方法,例如: getForObject() 、 postForObject() 等

package cn.tedu.sp06;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableDiscoveryClient@SpringBootApplicationpublic class Sp06RibbonApplication { //創(chuàng)建 RestTemplate 實例,并存入 spring 容器 @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Sp06RibbonApplication.class, args); }}

第二步:創(chuàng)建RibbonController

package cn.tedu.sp06.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.pojo.Order;import cn.tedu.sp01.pojo.User;import cn.tedu.web.util.JsonResult;@RestControllerpublic class RibbonController { @Autowired private RestTemplate rt; @GetMapping('/item-service/{orderId}') public JsonResult<List<Item>> getItems(@PathVariable String orderId) { //向指定微服務(wù)地址發(fā)送 get 請求,并獲得該服務(wù)的返回結(jié)果 //{1} 占位符,用 orderId 填充 return rt.getForObject('http://localhost:8001/{1}', JsonResult.class, orderId); } @PostMapping('/item-service/decreaseNumber') public JsonResult decreaseNumber(@RequestBody List<Item> items) { //發(fā)送 post 請求 return rt.postForObject('http://localhost:8001/decreaseNumber', items, JsonResult.class); } / @GetMapping('/user-service/{userId}') public JsonResult<User> getUser(@PathVariable Integer userId) { return rt.getForObject('http://localhost:8101/{1}', JsonResult.class, userId); } @GetMapping('/user-service/{userId}/score') public JsonResult addScore( @PathVariable Integer userId, Integer score) { return rt.getForObject('http://localhost:8101/{1}/score?score={2}', JsonResult.class, userId, score); } / @GetMapping('/order-service/{orderId}') public JsonResult<Order> getOrder(@PathVariable String orderId) { return rt.getForObject('http://localhost:8201/{1}', JsonResult.class, orderId); } @GetMapping('/order-service') public JsonResult addOrder() { return rt.getForObject('http://localhost:8201/', JsonResult.class); }}

第三步:啟動服務(wù),進行測試

http://localhost:3001/item-service/35

等。。

ribbon負載均衡

深入學(xué)習(xí)Spring Cloud-Ribbon

第一步:RestTemplate設(shè)置@LoadBalanced

@LoadBalanced 負載均衡注解,會對 RestTemplate 實例進行封裝,創(chuàng)建動態(tài)代理對象,并切入(AOP)負載均衡代碼,把請求分發(fā)到集群中的服務(wù)器

package cn.tedu.sp06;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;@EnableDiscoveryClient@SpringBootApplicationpublic class Sp06RibbonApplication { @LoadBalanced //負載均衡注解 @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Sp06RibbonApplication.class, args); }}

第二步:訪問路徑設(shè)置為id

package cn.tedu.sp06.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.pojo.Order;import cn.tedu.sp01.pojo.User;import cn.tedu.web.util.JsonResult;@RestControllerpublic class RibbonController { @Autowired private RestTemplate rt; @GetMapping('/item-service/{orderId}') public JsonResult<List<Item>> getItems(@PathVariable String orderId) { //這里服務(wù)器路徑用 service-id 代替,ribbon 會向服務(wù)的多臺集群服務(wù)器分發(fā)請求 return rt.getForObject('http://item-service/{1}', JsonResult.class, orderId); } @PostMapping('/item-service/decreaseNumber') public JsonResult decreaseNumber(@RequestBody List<Item> items) { return rt.postForObject('http://item-service/decreaseNumber', items, JsonResult.class); } / @GetMapping('/user-service/{userId}') public JsonResult<User> getUser(@PathVariable Integer userId) { return rt.getForObject('http://user-service/{1}', JsonResult.class, userId); } @GetMapping('/user-service/{userId}/score') public JsonResult addScore( @PathVariable Integer userId, Integer score) { return rt.getForObject('http://user-service/{1}/score?score={2}', JsonResult.class, userId, score); } / @GetMapping('/order-service/{orderId}') public JsonResult<Order> getOrder(@PathVariable String orderId) { return rt.getForObject('http://order-service/{1}', JsonResult.class, orderId); } @GetMapping('/order-service') public JsonResult addOrder() { return rt.getForObject('http://order-service/', JsonResult.class); }}

第三步:訪問測試,ribbon 會把請求分發(fā)到 8001 和 8002 兩個服務(wù)端口上

http://localhost:3001/item-service/34 ribbon重試

深入學(xué)習(xí)Spring Cloud-Ribbon

第一步:添加spring-retry依賴

<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId></dependency>

第二步:application.yml 配置 ribbon 重試

# 06項目用來測試遠程調(diào)用和ribbon工具# 等功能測試完成后,直接刪除spring: application: name: ribbonserver: port: 3001# 連接eureka,從eureka發(fā)現(xiàn)其他服務(wù)的地址eureka: client: service-url: defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka#配置ribbon 重試次數(shù)ribbon: # 次數(shù)參數(shù)沒有提示,并且會有黃色警告 # 重試次數(shù)越少越好,一般建議用0,1 MaxAutoRetries: 1 MaxAutoRetriesNextServer: 2

第三步:設(shè)置 RestTemplate 的請求工廠的超時屬性

package cn.tedu.sp06;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;@SpringBootApplicationpublic class Sp06RibbonApplication { public static void main(String[] args) { SpringApplication.run(Sp06RibbonApplication.class, args); } /** * 創(chuàng)建RestTemplate實例 * 放入spring容器 * @LoadBalanced-對RestTemplate進行增強,封裝RestTemplate,添加負載均衡功能 */ @LoadBalanced @Bean public RestTemplate restTemplate(){ //設(shè)置調(diào)用超時時間,超時后認為調(diào)用失敗 SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); f.setConnectTimeout(1000);//建立連接等待時間 f.setReadTimeout(1000);//連接建立后,發(fā)送請求后,等待接收響應(yīng)的時間 return new RestTemplate(f); }}

第四步:ItemController 添加延遲代碼

package cn.tedu.sp02.item.controller;import java.util.List;import java.util.Random;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.service.ItemService;import cn.tedu.web.util.JsonResult;import lombok.extern.slf4j.Slf4j;@Slf4j@RestControllerpublic class ItemController { @Autowired private ItemService itemService; //配置文件 application.yml中的server.port=8001注入到這個變量 //是為了后面做負載均衡測試,可以直接看到調(diào)用的是那個服務(wù)器 @Value('${server.port}') private int port; //獲取訂單的商品列表 @GetMapping('/{orderId}') public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException { log.info('server.port='+port+', orderId='+orderId); //模擬延遲代碼 if (Math.random()<0.9){ long t = new Random().nextInt(5000); log.info('延遲:'+t); Thread.sleep(t); } List<Item> items = itemService.getItems(orderId);//根據(jù)訂單id獲取商品列表 return JsonResult.ok(items).msg('port='+port); } //減少商品庫存 /** * @RequestBody 完整接收請求協(xié)議體中的數(shù)據(jù) * @param items * @return */ @PostMapping('/decreaseNumber') public JsonResult decreaseNumber(@RequestBody List<Item> items) { for (Item item : items){ log.info('減少商品庫存:'+item ); } itemService.decreaseNumbers(items); return JsonResult.ok(); }}

第五步:測試 ribbon 重試機制

通過 ribbon 訪問 item-service,當超時,ribbon 會重試請求集群中其他服務(wù)器

http://localhost:3001/item-service/35

到此這篇關(guān)于深入學(xué)習(xí)Spring Cloud-Ribbon的文章就介紹到這了,更多相關(guān)Spring Cloud-Ribbon內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产欧美69| 日本欧美一区| 在线中文字幕播放| 成人在线丰满少妇av| 日本一二区不卡| 麻豆精品在线观看| 精品国产亚洲一区二区三区在线| 狠狠久久伊人中文字幕| 亚洲国产福利| 99精品在线观看| 在线成人直播| 蜜桃视频第一区免费观看| 日韩中文字幕| 91成人在线| 精品三级国产| 久久精品电影| 亚洲午夜电影| 天堂成人国产精品一区| 亚洲精品在线国产| 国产精品亚洲综合在线观看| 国产毛片一区二区三区| 国产成人久久精品麻豆二区| 国内亚洲精品| 亚洲在线久久| 国产精品第十页| 黄毛片在线观看| 黄色日韩精品| 911亚洲精品| 成午夜精品一区二区三区软件| 国产综合色区在线观看| 夜久久久久久| 久久国产生活片100| 精品色999| 亚洲激情五月| 国产探花在线精品| 国产成人精品福利| 在线精品视频在线观看高清| 日本不卡中文字幕| 91一区二区三区四区| 亚洲激情不卡| 国产精品综合色区在线观看| 丝袜av一区| 日韩欧美三区| 福利在线免费视频| 丝袜诱惑制服诱惑色一区在线观看| 欧美一区二区三区免费看| 日韩av在线中文字幕| 丝袜美腿亚洲一区| 国产一区二区三区视频在线| 午夜久久美女| 日韩国产在线不卡视频| 日韩理论视频| 综合激情网站| 给我免费播放日韩视频| 热久久国产精品| 国内精品麻豆美女在线播放视频| 国产综合亚洲精品一区二| 国产毛片精品| 伊人久久婷婷| 精品久久91| 免费在线观看日韩欧美| 国产成年精品| 婷婷综合国产| 在线视频观看日韩| 国产精品伦一区二区| 日韩不卡免费高清视频| 欧美一区自拍| 女主播福利一区| 国产成人精品一区二区三区免费| 丝袜脚交一区二区| 亚洲成人不卡| 国产精品传媒麻豆hd| 久久99伊人| 亚洲不卡av不卡一区二区| 日本欧美久久久久免费播放网| 在线日韩电影| 国产激情在线播放| 人人爱人人干婷婷丁香亚洲| 中文精品视频| 日韩伦理在线一区| 日韩精品1区2区3区| 国产综合亚洲精品一区二| 国产一区二区三区四区二区 | 久久伊人亚洲| 色综合视频一区二区三区日韩 | 亚洲人妖在线| 在线观看精品| 国产乱码精品一区二区亚洲| 美女精品在线| 女人av一区| 麻豆91精品91久久久的内涵| 亚州精品视频| 老鸭窝毛片一区二区三区| 久久国产亚洲| 91综合视频| 国产精品乱战久久久| 日日夜夜免费精品视频| 亚洲在线观看| 99视频精品全国免费| 国产理论在线| 麻豆精品新av中文字幕| 国产欧美日韩在线观看视频| 免播放器亚洲| 日韩视频一区二区三区在线播放免费观看| 亚洲黄色中文字幕| 国产一区二区三区精品在线观看| 久久精品97| 日本亚洲视频在线| 蜜臀精品久久久久久蜜臀| 久久国产精品久久w女人spa| 精品在线91| 99精品视频在线观看免费播放| 日韩免费高清| 日韩三区在线| 久久影院一区| 亚洲一级网站| 亚洲精品电影| 久久香蕉国产| 国产一在线精品一区在线观看| 精品三级久久| 久久久五月天| 伊人久久婷婷| 丝瓜av网站精品一区二区| 免费国产自线拍一欧美视频| 久久亚洲风情| 亚洲精品在线二区| 欧美三区不卡| 麻豆精品视频在线观看免费| 精品成av人一区二区三区| 美女精品视频在线| 91麻豆国产自产在线观看亚洲| 日韩精品2区| 成人羞羞在线观看网站| 久久精品国产大片免费观看| 最新亚洲激情| 亚欧成人精品| 国产黄色精品| 国产精品原创| 国产在线欧美| 亚洲三级精品| 国产精区一区二区| 国产一区调教| 久久影院一区| 视频精品一区二区| 日韩国产欧美视频| 欧美国产中文高清| 亚洲免费福利| 亚洲国产综合在线看不卡| 午夜在线一区| 欧美日韩调教| zzzwww在线看片免费| 欧美在线亚洲| 97久久亚洲| 国产 日韩 欧美 综合 一区| 一区二区小说| 天海翼亚洲一区二区三区| 国产精品99久久久久久董美香| 麻豆成全视频免费观看在线看| 亚洲精品中文字幕乱码| 日韩成人av影视| 成人台湾亚洲精品一区二区| 亚洲高清激情| 亚洲精品在线国产| 加勒比视频一区| 亚洲激情不卡| 国产精品视频3p| 久久久久欧美精品| 免费精品视频最新在线| 麻豆国产精品| 午夜精品一区二区三区国产| 日韩精品第一| 日韩不卡免费高清视频| 亚洲九九精品| 日韩免费在线| 日本不卡中文字幕| 国产精品久久久久av电视剧| 中文字幕日韩亚洲| 激情综合婷婷| 蜜桃一区二区三区在线| 成人精品动漫一区二区三区| 日韩午夜一区| 国产一区三区在线播放| 麻豆精品网站| 91日韩在线| **爰片久久毛片| 久久国产精品成人免费观看的软件| 日韩1区2区3区| 色老板在线视频一区二区| 日韩1区2区日韩1区2区| 99国产精品免费视频观看| 日韩激情综合| 欧美在线观看视频一区| 日本综合精品一区| 久久久久国产精品一区二区| 久久国产精品色av免费看| 韩日一区二区三区| 精品日本视频| 日韩激情一区二区| 欧美国产91| 91亚洲国产|