最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud客戶端的負載均衡Ribbon的實現

 更新時間:2018年06月20日 14:03:19   作者:lfalex  
微服務架構,不可避免的存在單個微服務有多個實例,這篇文章主要介紹了SpringCloud客戶端的負載均衡Ribbon的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言:微服務架構,不可避免的存在單個微服務有多個實例,那么客戶端如何將請求分攤到多個微服務的實例上呢?這里我們就需要使用負載均衡了

一、Ribbon簡介

Ribbon是Netflix發(fā)布的負載均衡器,它有助于控制HTTP和TCP客戶端的行為。為Ribbon配置服務提供者地址列表后,Ribbon就可基于某種負載均衡算法,自動地幫助服務消費者去請求。Ribbon默認為我們提供了很多的負載均衡算法,例如:輪詢,隨機等,也可自定義;

Ribbon的GitHub: https://github.com/Netflix/ribbon

而在SpringCloud中使用Ribbon和Eureka時,Ribbon會自動從EurekaServer中獲取服務提供者地址列表,并基于負載均衡算法。

二、Ribbon實戰(zhàn)

​1、創(chuàng)建EurekaServer,EurekaClient1,EurekaClient2,之前已經說過了Eureka的使用,這里直接上代碼:

EurekaServer:

ServerApplication.java

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class,args);
  }

}

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">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.cn</groupId>
 <artifactId>eureka-ribbon-server</artifactId>
 <version>1.0-SNAPSHOT</version>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.</groupId>
   <artifactId></artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
 </dependencies>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Edgware.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <!-- 添加spring-boot的maven插件 -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

application.properties

server.port=8761
#注意:這兩個配置eureka默認為true,要改成false,否則會報錯,connot connect server
#表示是否將自己注冊在EurekaServer上
eureka.client.register-with-eureka=false
#表示是否從EurekaServer獲取注冊信息
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

EurekaClient1:

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">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.cn</groupId>
 <artifactId>eureka-ribbon-client</artifactId>
 <version>1.0-SNAPSHOT</version>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
 </dependencies>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Edgware.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <!-- 添加spring-boot的maven插件 -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>
server.port=8762
spring.application.name=client-8762
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

而在啟動類中加入RestTemplate遠程調用實例到容器中,并且 添加LoadBalanced注解,使RestTemplate具備負載均衡的能力 :

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

  /** 
   * @Description: 加入@LoadBalanced注解,就可以為RestTemplate加入負載均衡的能力 
   * @Param:
   * @return: 
   * @Author: 
   * @Date: 2018/6/15 
   */ 
  @Bean
  @LoadBalanced
  public RestTemplate getRestTemplate() {
    return new RestTemplate();
  }

}

創(chuàng)建Controller,注入RestTemplate、LoadBalancerClient實例:

package com.cn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 15:55
 **/
@Controller
public class RibbonController {

  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/loadInstance")
  @ResponseBody
  public String loadInstance() {
    ServiceInstance choose = this.loadBalancerClient.choose("client-87");
    System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort());
    return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort();
  }

}
package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @program: springcloud-example
 * @description:
 * @author: 535504
 * @create: 2018-06-15 16:05
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

}

EurekaClient2:

pom.xml與EurekaClient1中一致

application.xml:

server.port=8763
spring.application.name=client-87
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

ClientController.java:

package com.cn.contorller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 16:12
 **/
@Controller
public class ClientController {

  @GetMapping("/getUser")
  @ResponseBody
  public String getUser() {
    System.out.println("獲取用戶成功");
    return "獲取用戶成功";
  }

}

2、啟動順序:

①、依次啟動EurekaServer =》 EurekaClient1 =》 EurekaClient2   ;

②、然后將EurekaClient2中的application.properties的server.port=8763改為server.port=8764,再次啟動該項目;

③、打開EurekaServer的配置頁面(http://localhost:8761/),如下:

④、我們在地址欄輸入http://localhost:8762/loadInstance,多刷新幾次,會發(fā)現每次調用的端口實例都不同,如下圖:

⑤、我們在看控制臺,如圖:

至此,Ribbon已經入門,是不是很簡單,但是這只是最簡單的應用,九牛一毛爾...學無止境乎!

示例代碼: https://gitee.com/lfalex/springcloud-example

相關文章

  • springboot實現對注解的切面案例

    springboot實現對注解的切面案例

    這篇文章主要介紹了springboot實現對注解的切面過程,首先定義一個注解、再編寫對注解的切面只是記錄的執(zhí)行時間和打印方法,可以實現其他邏輯,需要的朋友可以參考一下
    2022-01-01
  • spring boot 添加admin監(jiān)控的方法

    spring boot 添加admin監(jiān)控的方法

    這篇文章主要介紹了spring boot 添加admin監(jiān)控的相關知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢

    Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢

    這篇文章主要介紹了Mybatis-Plus insertBatch執(zhí)行緩慢的原因查詢,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • MyBatisPlus分頁的同時指定排序規(guī)則說明

    MyBatisPlus分頁的同時指定排序規(guī)則說明

    這篇文章主要介紹了MyBatisPlus分頁的同時指定排序規(guī)則說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • java解析excel文件的方法

    java解析excel文件的方法

    這篇文章主要介紹了java解析excel文件的方法,這里整理相關的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Java中的通用路徑轉義符介紹

    Java中的通用路徑轉義符介紹

    這篇文章主要介紹了Java中的通用路徑轉義符介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java JVM程序指令碼實例解析

    Java JVM程序指令碼實例解析

    這篇文章主要介紹了Java JVM程序指令碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Java不定參數使用及一些注意情況

    Java不定參數使用及一些注意情況

    不定參數是一種特殊的參數類型,它允許方法接受可變數量的參數,本文主要介紹了Java不定參數使用及一些注意情況,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Java用BigDecimal類解決Double類型精度丟失的問題

    Java用BigDecimal類解決Double類型精度丟失的問題

    這篇文章主要介紹了Java用BigDecimal類解決Double類型精度丟失的問題,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • CAT分布式實時監(jiān)控系統(tǒng)使用詳解

    CAT分布式實時監(jiān)控系統(tǒng)使用詳解

    這篇文章主要為大家介紹了CAT分布式實時監(jiān)控系統(tǒng)介紹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論

灵寿县| 南阳市| 内乡县| 仪陇县| 兴义市| 平定县| 莱州市| 泾源县| 永城市| 元朗区| 平果县| 三都| 崇义县| 社旗县| 昭平县| 建宁县| 大渡口区| 荃湾区| 阿瓦提县| 松滋市| 华宁县| 固安县| 尚志市| 平度市| 阿图什市| 介休市| 永宁县| 镇康县| 嵊泗县| 肥乡县| 桑日县| 玉环县| 延安市| 香河县| 巴林右旗| 新余市| 越西县| 垦利县| 禹城市| 怀来县| 和田县|