ribbon負載均衡

ribbon是一款spring的負載均衡器,.

springcloud的組件絕大部分都依賴於eureka服務註冊中心

因此,我們先搭建一個eureka服務註冊中心

如果還不會搭建請參考eureka服務註冊中心搭建教程https://blog.csdn.net/qq_25861361/article/details/89510079

因爲idea一個窗口一個項目,所以這裏我們採用springboot的多元化項目來實現,首先創建一個springboot的項目,修改pom文件

具體如下,先添加了eureka子項目

<?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>org.springcloud.demo</groupId>
    <artifactId>ervice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>eureka</module>
        <module>ribbon</module>
        <module>f1</module>
        <module>f2</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
</project>

當然,這裏會報一個錯誤,因爲我們沒有eureka,ribbon,f1,f2四個子項目,那麼我們就創建一個創建方法如下,在項目上右擊,new-mdule

四個子項目依次創建,完成以後結構如下

將四個子項目的pom文件父項目修改爲我們的父項目

  <parent>
        <groupId>org.springcloud.demo</groupId>
        <artifactId>ervice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

這裏我把之前eureka的demo複製了一下,具體參照這裏https://blog.csdn.net/qq_25861361/article/details/89510079

剩餘的三個項目是幹什麼的呢?

ribbon:負載均衡器

f1:普通項目1

f2:普通項目2

既然是負載均衡,那麼我們肯定要有多個項目才能負載均衡

第一步還是添加依賴

在ribbon的配置文件加入如下依賴

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

修改ribbon的配置文件,具體如下

 

負載均衡的策略

我們修改ribbon的啓動文件,具體如下

package org.ribbon.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class RibbonApplication {

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

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(String key) {
        loadBalancerClient.choose("隨便起個名字");//官方說這裏是服務id,但是我發現隨便起個名字都沒事
        String skey = restTemplate.getForEntity("http://f1/test?key="+key, String.class).getBody();
        System.out.println(skey);
        return skey;

    }

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


}

因爲是測試,這裏我就把接口寫在了啓動文件

在f1,與f2中分別寫上1個接口,用做負載均衡測試,具體如下

package org.f2.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class F2Application {

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


    @RequestMapping(value = "/test" ,method = RequestMethod.GET)
    public String test(String key) {
        return "this is e f2----" + key;
    }
}
package org.f1.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class F1Application {

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


    @RequestMapping(value = "/test" ,method = RequestMethod.GET)
    public String test(String key) {
        return "this is e f1----" + key;
    }
}

當然,這裏的兩個我們都需要定義爲Eureka客戶機,需要加入對應依賴

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

修改配置文件,把客戶機加入到服務註冊中心,f1與f2都是一樣(端口號不一樣)

在瀏覽器中輸入http://127.0.0.1:8003/test?key=1

多試幾次,然後我們看一下控制檯日誌

這樣的話我們的負載均衡器就完成了

配套源碼:https://gitee.com/ww2510095/spring_cloud_learning

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章