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

 

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