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

您的位置:首頁技術文章
文章詳情頁

深入學習Spring Cloud-Ribbon

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

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

ribion=負載均衡+重試

深入學習Spring Cloud-Ribbon

ribbon的工作步驟:

第一步先選擇 EurekaServer ,它優先選擇在同一個區域內負載較少的server。 第二步再根據用戶指定的策略,在從server取到的服務注冊列表中選擇一個地址。 其中Ribbon提供了多種策略:比如輪詢、隨機和根據響應時間加權。

深入學習Spring Cloud-Ribbon

創建spring ribbon項目

第一步:新建spring項目

深入學習Spring Cloud-Ribbon

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

深入學習Spring Cloud-Ribbon

第三步:添加sp01-commons工具API依賴;eureka-client 中已經包含 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 #服務器命名 server: port: 3001 # 設置服務器端口號 # 配置添加注冊中心集群 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka遠程調用RestTemplate

RestTemplate 是SpringBoot提供的一個Rest遠程調用工具。

類似于 HttpClient,可以發送 http 請求,并處理響應。RestTemplate簡化了Rest API調用,只需要使用它的一個方法,就可以完成請求、響應、Json轉換

方法:

getForObject(url, 轉換的類型.class, 提交的參數) postForObject(url, 協議體數據, 轉換的類型.class)

RestTemplate 和 Dubbo 遠程調用的區別:

RestTemplate:

http調用

效率低

Dubbo:

RPC調用,Java的序列化

效率高

第一步:創建RestTemplate實例

RestTemplate 是用來調用其他微服務的工具類,封裝了遠程調用代碼,提供了一組用于遠程調用的模板方法,例如: 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 { //創建 RestTemplate 實例,并存入 spring 容器 @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Sp06RibbonApplication.class, args); }}

第二步:創建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) { //向指定微服務地址發送 get 請求,并獲得該服務的返回結果 //{1} 占位符,用 orderId 填充 return rt.getForObject('http://localhost:8001/{1}', JsonResult.class, orderId); } @PostMapping('/item-service/decreaseNumber') public JsonResult decreaseNumber(@RequestBody List<Item> items) { //發送 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); }}

第三步:啟動服務,進行測試

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

等。。

ribbon負載均衡

深入學習Spring Cloud-Ribbon

第一步:RestTemplate設置@LoadBalanced

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

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); }}

第二步:訪問路徑設置為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) { //這里服務器路徑用 service-id 代替,ribbon 會向服務的多臺集群服務器分發請求 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 會把請求分發到 8001 和 8002 兩個服務端口上

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

深入學習Spring Cloud-Ribbon

第一步:添加spring-retry依賴

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

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

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

第三步:設置 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); } /** * 創建RestTemplate實例 * 放入spring容器 * @LoadBalanced-對RestTemplate進行增強,封裝RestTemplate,添加負載均衡功能 */ @LoadBalanced @Bean public RestTemplate restTemplate(){ //設置調用超時時間,超時后認為調用失敗 SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); f.setConnectTimeout(1000);//建立連接等待時間 f.setReadTimeout(1000);//連接建立后,發送請求后,等待接收響應的時間 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注入到這個變量 //是為了后面做負載均衡測試,可以直接看到調用的是那個服務器 @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);//根據訂單id獲取商品列表 return JsonResult.ok(items).msg('port='+port); } //減少商品庫存 /** * @RequestBody 完整接收請求協議體中的數據 * @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 會重試請求集群中其他服務器

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

到此這篇關于深入學習Spring Cloud-Ribbon的文章就介紹到這了,更多相關Spring Cloud-Ribbon內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
青草综合视频| 精品中文字幕一区二区三区 | 国产麻豆精品久久| 中文字幕av亚洲精品一部二部| 亚洲综合小说| 欧美国产另类| 日韩一区二区在线免费| 99精品视频在线| 男人天堂欧美日韩| 欧美在线观看天堂一区二区三区| 国产免费av一区二区三区| 国产一区国产二区国产三区| 久久久久美女| 五月激激激综合网色播| 久久伊人国产| 欧美a级一区| 日本特黄久久久高潮| 久久gogo国模啪啪裸体| 日韩欧美一区二区三区免费看| 狠狠爱www人成狠狠爱综合网| 综合亚洲自拍| 国产一区二区三区网| 午夜精品影院| 欧美激情aⅴ一区二区三区| 天堂а√在线最新版中文在线| 国产一区成人| 牛牛精品成人免费视频| 亚洲婷婷免费| 国产美女视频一区二区| 91精品国产成人观看| 日韩精品视频网站| 日韩在线观看不卡| 蜜臀av一区二区三区| 久久麻豆视频| 亚洲我射av| 国产高清不卡| 欧美日本久久| 国产精品日韩欧美一区| 欧美激情99| 亚洲免费网址| 国产夫妻在线| 国产美女亚洲精品7777| 日韩网站在线| 国产精选在线| 国产毛片精品| 免费人成精品欧美精品| 日韩在线高清| 人人精品久久| 欧美a级一区| 色婷婷亚洲mv天堂mv在影片| 日本麻豆一区二区三区视频| 欧美粗暴jizz性欧美20| 精品久久网站| 欧美日一区二区三区在线观看国产免| 欧美性感美女一区二区| 精品免费视频| 国产日韩欧美三级| 亚洲免费在线| 久久精品高清| 久久精品国产999大香线蕉| 四虎精品永久免费| 亚洲精品1区2区| 国产成人免费av一区二区午夜| 亚洲香蕉久久| av亚洲免费| 日韩电影免费网址| 精品丝袜久久| 精品一区视频| 六月丁香综合在线视频| 日本91福利区| 日韩激情一二三区| 石原莉奈在线亚洲三区| 蜜桃国内精品久久久久软件9| 成人国产精品一区二区网站| 欧美视频久久| 97成人在线| 日韩**一区毛片| 免费在线观看日韩欧美| 中文一区在线| 黄色日韩在线| 欧美日韩激情| 欧美精品一区二区三区精品| 日韩中文在线播放| 日韩在线短视频| 国产成人精品亚洲日本在线观看| 国产成人免费视频网站视频社区| 国产精品一线| 久久国际精品| 国产精品乱战久久久| 国产日产高清欧美一区二区三区| 日本一区二区三区中文字幕| 日本综合精品一区| 日本va欧美va欧美va精品| 亚洲色图国产| 日韩黄色av| 国产精品麻豆成人av电影艾秋 | 日韩精品一区二区三区中文在线| 午夜久久av| 国产免费av国片精品草莓男男| 国产精品久久久久久久免费软件| 麻豆一区在线| 日韩欧美三级| 亚洲视频www| 婷婷成人av| 国产情侣一区| 国精品产品一区| 日韩一区自拍| 亚洲精品一二三区区别| 美女黄网久久| 日本成人在线一区| 麻豆91精品视频| 日本国产精品| 蘑菇福利视频一区播放| 青草久久视频| 成人国产精品久久| 久久一区二区中文字幕| 亚洲免费精品| 日韩国产欧美在线播放| 精品无人区麻豆乱码久久久| 亚洲成人av观看| 亚洲有吗中文字幕| 国产精品手机在线播放| 精品国产乱码久久久| 激情婷婷欧美| 亚洲欧洲日韩精品在线| 九九久久国产| 欧美特黄一级| 国产精品综合| 欧美成人久久| 国产欧美自拍一区| 天堂√中文最新版在线| 蜜桃av一区二区| 国产精品1区在线| 久久蜜桃精品| 日韩av中文字幕一区| 高清不卡一区| 蜜桃久久av| 欧美韩一区二区| 91久久国产| 老牛国内精品亚洲成av人片| 黄色亚洲在线| 免费在线亚洲| 亚洲综合欧美| 国产aⅴ精品一区二区四区| 伊人久久亚洲热| 精品中文字幕一区二区三区四区| 99国产精品一区二区| 国产精品大片| 亚洲欧洲一区二区天堂久久| 久久香蕉网站| 综合色就爱涩涩涩综合婷婷| 国产一区二区视频在线看| 视频在线观看91| 日韩国产欧美| 国产精品一区二区精品视频观看| 好看的av在线不卡观看| 国产成人精品三级高清久久91| 在线观看一区| 一本大道色婷婷在线| 日韩高清一级| 午夜精品一区二区三区国产| 精品免费av| 日本h片久久| 亚洲精华国产欧美| 中文字幕人成乱码在线观看| 少妇精品久久久一区二区| 久久精品免费一区二区三区| 国产精品v日韩精品v欧美精品网站 | 欧美日韩精品免费观看视频完整| 欧美交a欧美精品喷水| 日韩精品一卡二卡三卡四卡无卡| 色在线中文字幕| 国产精东传媒成人av电影| 亚洲精品自拍| 亚洲经典在线| 精精国产xxxx视频在线野外| 国产欧美精品久久| 日本中文字幕一区二区| 亚洲一区欧美激情| 九色精品91| 亚洲天堂黄色| 亚洲成人国产| 欧美成人基地| 88xx成人免费观看视频库| 国产一区二区三区不卡视频网站| 亚洲精品在线a| 在线免费观看亚洲| 老司机久久99久久精品播放免费| 精品一区免费| 色88888久久久久久影院| 国产自产自拍视频在线观看| 久久国产精品免费一区二区三区 | 亚洲一区免费| 蜜臀91精品国产高清在线观看 | 日韩毛片视频| 夜鲁夜鲁夜鲁视频在线播放| а√天堂8资源在线| 美女av在线免费看| 欧美黄色网页| 香蕉久久精品|