溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

發(fā)布時間:2021-06-18 15:47:52 來源:億速云 閱讀:157 作者:Leah 欄目:大數(shù)據(jù)

這篇文章給大家介紹Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

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.

<!-- more -->

Ribbon是負載均衡客戶端,可以很好的控制HTTP和TCP客戶端的行為。 Feign已經集成了Ribbon。

Spring Cloud Netflix默認為ribbon(BeanType beanName:ClassName)提供以下bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

準備工作

這篇文章基于上一篇文章項目,啟動eureka-server項目;啟動eureka-client項目,端口為8040; 將eureka-client的配置文件端口改為8041,并啟動,同一個項目修改端口號,啟動多個實例,只需要在IDEA中勾選Allow parallel run

Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

此時你會發(fā)現(xiàn)注冊服務中注冊了兩個eureka-client實例,相當于起了2個節(jié)點的集群。 訪問http://localhost:9090:

Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

新建一個服務消費者

使用Spring Initializr新建一個項目,取名為ribbon-service, 在Spring Cloud Discovery中勾選Eureka Discovery Client,在Spring Cloud Routing中勾選Ribbon,在Web中勾選Spring Web: Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡

創(chuàng)建成功后,項目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 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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.noodles.mars</groupId>
    <artifactId>ribbon-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ribbon-service</name>
    <description>Ribbon Service</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</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-test</artifactId>
            <scope>test</scope>
        </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>

ribbon-service配置服務中心地址、應用名、端口,配置文件內容:

server:
  port: 8050

spring:
  application:
    name: ribbon-service

eureka:
  client:
    service-url: 
      defaultZone: http://localhost:9090/eureka/

在項目啟動類上注解@EnableDiscoveryClient, 開啟向服務中心注冊;定義一個 RestTemplate Bean, 并注解@LoadBalanced開啟負載均衡功能:

@EnableEurekaClient
@SpringBootApplication
public class RibbonServiceApplication {

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

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

}

寫一個controller, 通過之前定義的RestTemplate來消費eureka-client服務的/hello接口,url中使用應用名,ribbon會根據(jù)應用名來選擇具體的服務實例,根據(jù)服務實例在請求的時候會用具體的url替換掉服務名:

@RestController
public class HelloController {

    private final RestTemplate restTemplate;

    @Autowired
    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);
    }
}

在瀏覽器上多次訪問 http://localhost:8050/hello?name=Mars :

Hello, My name is Mars, I'm from port: 8040
Hello, My name is Mars, I'm from port: 8041

關于Spring Cloud中怎么使用Ribbon實現(xiàn)負載均衡就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI