成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringCloud(第 008 篇)電影微服務(wù),使用配置文件配置 Ribbon 在客戶端進行負載

wangjuntytl / 671人閱讀

摘要:第篇電影微服務(wù),使用配置文件配置在客戶端進行負載均衡調(diào)度算法一大致介紹通過配置來設(shè)置客戶端進行負載均衡的調(diào)度算法通過兩種代碼調(diào)用方式來測試客戶端負載均衡算法二實現(xiàn)步驟添加引用包模塊客戶端發(fā)現(xiàn)模塊

SpringCloud(第 008 篇)電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進行負載均衡調(diào)度算法

-

一、大致介紹
1、通過 application.yml 配置來設(shè)置 Ribbon 客戶端進行負載均衡的調(diào)度算法;
2、通過 restTemplate.getForObject、loadBalancerClient.choose 兩種代碼調(diào)用方式來測試客戶端負載均衡算法;
二、實現(xiàn)步驟 2.1 添加 maven 引用包


    4.0.0

    springms-consumer-movie-ribbon-properties
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    


    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

2.2 添加應(yīng)用配置文件(springms-consumer-movie-ribbon-propertiessrcmainresourcesapplication.yml)
spring:
  application:
    name: springms-consumer-movie-ribbon-properties
server:
  port: 8030
#做負載均衡的時候,不需要這個動態(tài)配置的地址
#user:
#  userServicePath: http://localhost:7900/simple/
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

# 配置 Ribbon 負載均衡調(diào)度規(guī)則
springms-provider-user:
  ribbon:
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #輪調(diào)
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #隨機分配
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
2.3 添加實體用戶類User(springms-consumer-movie-ribbon-propertiessrcmainjavacomspringmscloudentityUser.java)
package com.springms.cloud.entity;

import java.math.BigDecimal;

public class User {
  private Long id;

  private String username;

  private String name;

  private Short age;

  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Short getAge() {
    return this.age;
  }

  public void setAge(Short age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }

}
2.4 添加Web訪問層Controller(springms-consumer-movie-ribbon-propertiessrcmainjavacomspringmscloudcontrollerMovieRibbonPropertiesController.java)
package com.springms.cloud.controller;

import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieRibbonPropertiesController {

  @Autowired
  private RestTemplate restTemplate;
  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @GetMapping("/movie/{id}")
  public User findById(@PathVariable Long id) {
      // http://localhost:7900/simple/
      // VIP virtual IP
      // HAProxy Heartbeat

      ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-provider-user");
      System.out.println(">>>>>" + " " + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

      return this.restTemplate.getForObject("http://springms-provider-user/simple/" + id, User.class);
  }

    @GetMapping("/choose")
    public String test() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-provider-user");
        System.out.println("00000" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("springms-provider-user2");
        System.out.println("222222222222222222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());

        return "choose successful";
    }
}
2.5 添加電影微服務(wù)啟動類(springms-consumer-movie-ribbon-propertiessrcmainjavacomspringmscloudMsConsumerMovieRibbonPropertiesApplication.java)
package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進行負載均衡調(diào)度算法。
 *
 * LoadBalanced:該負載均衡注解,已經(jīng)整合了 Ribbon;
 *
 * Ribbon 的默認負載均衡的算法為:輪詢;
 *
 * 配置文件優(yōu)先級最高,Java代碼設(shè)置的配置其次,默認的配置優(yōu)先級最低;
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/17
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMovieRibbonPropertiesApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MsConsumerMovieRibbonPropertiesApplication.class, args);
        System.out.println("【【【【【【 電影微服務(wù)-Properties定制Ribbon 】】】】】】已啟動.");
    }
}
三、測試
/****************************************************************************************
 一、電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進行負載均衡調(diào)度算法(測試輪詢分配服務(wù)器地址):

 1、application.yml配置輪詢算法:springms-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule;
 2、啟動 springms-provider-user 模塊服務(wù),啟動3個端口(7900、7899、7898);
 3、啟動 springms-provider-user2 模塊服務(wù),啟動2個端口(7997、7996)(直接將用戶微服務(wù) spring.application.name 改了個名字為 springms-provider-user2 再啟動而已);
 4、啟動 springms-consumer-movie-ribbon-properties 模塊服務(wù),啟動1個端口;
 5、在瀏覽器輸入地址http://localhost:8030/choose,然后看看 springms-provider-user、springms-provider-user2 的各個對應(yīng)的端口的服務(wù)打印的信息是否均勻,正常情況下應(yīng)該是輪詢分配打印的;

 總結(jié):springms-provider-user(之所以輪詢是因為配置文件采用 RoundRobinRule 輪詢調(diào)度算法)、springms-provider-user2(之所以輪詢是因為沒有任何配置,默認調(diào)度算法就是輪詢算法)
 ****************************************************************************************/

/****************************************************************************************
 二、電影微服務(wù),使用 application.yml 配置文件配置 Ribbon 在客戶端進行負載均衡調(diào)度算法(測試隨機分配服務(wù)器地址):

 1、application.yml配置隨機算法:springms-provider-user.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule;
 2、啟動 springms-provider-user 模塊服務(wù),啟動3個端口(7900、7899、7898);
 3、啟動 springms-provider-user2 模塊服務(wù),啟動2個端口(7997、7996)(直接將用戶微服務(wù) spring.application.name 改了個名字為 springms-provider-user2 再啟動而已);
 4、啟動 springms-consumer-movie-ribbon-properties 模塊服務(wù),啟動1個端口;
 5、在瀏覽器輸入地址http://localhost:8030/choose,然后看看 springms-provider-user、springms-provider-user2 的各個對應(yīng)的端口的服務(wù)打印的信息是否均勻,正常情況下應(yīng)該是輪詢分配打印的;

 總結(jié):springms-provider-user(之所以隨機是因為配置文件采用 RandomRule 隨機調(diào)度算法)、springms-provider-user2(之所以輪詢是因為沒有任何配置,默認調(diào)度算法就是輪詢算法)
 ****************************************************************************************/
四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接

歡迎關(guān)注,您的肯定是對我最大的支持!!!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/70471.html

相關(guān)文章

  • SpringCloud 006 電影服務(wù)使用 Ribbon 戶端進行負載均衡

    摘要:第篇電影微服務(wù),使用在客戶端進行負載均衡一大致介紹是發(fā)布的云中間層服務(wù)開源項目,主要功能是提供客戶端負載均衡算法。而被注解后,能過用負載均衡,主要是維護了一個被注解的列表,并給列表中的添加攔截器,進而交給負載均衡器去處理。 SpringCloud(第 006 篇)電影微服務(wù),使用 Ribbon 在客戶端進行負載均衡 - 一、大致介紹 1、Ribbon 是 Netflix 發(fā)布的云中間層...

    nodejh 評論0 收藏0
  • SpringCloud 011 電影Ribbon服務(wù),脫離Eureka使用配置listOf

    摘要:第篇電影微服務(wù),脫離使用配置進行客戶端負載均衡調(diào)度一大致介紹通過嘗試脫離服務(wù)治理框架,脫離生態(tài)圈,單獨操作客戶端負載均衡調(diào)度本章節(jié)僅僅只是使用了來測試客戶端負載均衡算法二實現(xiàn)步驟添加引用包模塊客 SpringCloud(第 011 篇)電影Ribbon微服務(wù),脫離Eureka使用配置listOfServers進行客戶端負載均衡調(diào)度 - 一、大致介紹 1、通過嘗試脫離服務(wù)治理框架,脫離 ...

    newtrek 評論0 收藏0
  • SpringCloud 007 電影服務(wù)使用定制化 Ribbon 戶端進行負載均衡不

    摘要:中的名稱,一定要是服務(wù)中注冊的名稱。添加這個的注解,主要是因為定義的時候報錯,也就是說明沒有被實例化。采用隨機分配的策略。添加訪問層添加電影微服務(wù)啟動類電影微服務(wù),使用定制化在客戶端進行負載均衡,使用不同服務(wù)不同配置策略。 SpringCloud(第 007 篇)電影微服務(wù),使用定制化 Ribbon 在客戶端進行負載均衡,使用 RibbonClient 不同服務(wù)不同配置策略 - 一、大...

    scola666 評論0 收藏0
  • SpringCloud 012 服務(wù)接入 Feign 負載均衡通過 FeignClient

    摘要:添加電影微服務(wù)啟動類電影微服務(wù)接入進行客戶端負載均衡,通過調(diào)用遠程微服務(wù)。注解表示該電影微服務(wù)已經(jīng)接入模塊。 SpringCloud(第 012 篇)電影微服務(wù)接入 Feign 進行客戶端負載均衡,通過 FeignClient 調(diào)用遠程 Http 微服務(wù) - 一、大致介紹 1、本章節(jié)主要介紹在 SpringCloud 生態(tài)圈中,使用一個類似于 Java HTTP 客戶端的工具 Feig...

    Cobub 評論0 收藏0
  • SpringCloud 014 電影 Ribbon 服務(wù)集成 Hystrix 斷路器實現(xiàn)失

    摘要:當服務(wù)宕機或者不可用時,即請求超時后會調(diào)用此方法。添加電影微服務(wù)啟動類電影微服務(wù)集成斷路器實現(xiàn)失敗快速響應(yīng),達到熔斷效果。 SpringCloud(第 014 篇)電影 Ribbon 微服務(wù)集成 Hystrix 斷路器實現(xiàn)失敗快速響應(yīng),達到熔斷效果 - 一、大致介紹 1、Hystrix 斷路器的原理很簡單,如同電力過載保護器。它可以實現(xiàn)快速失敗,如果它在一段時間內(nèi)偵測到許多類似的錯誤,...

    StonePanda 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<