负载均衡框架 ribbon 一

↘锁芯ラ 提交于 2020-01-19 04:04:09

Ribbon开源地址:https://github.com/Netflix/ribbon/wiki/Getting-Started

1.Ribbon简介

 负载均衡框架,支持可插拔式的负载均衡规则
 支持多种协议,如HTTP, UDP等
 提供负载均衡客户端

 

 2.Ribbon 负载均衡器组件

 一个负载均衡器,至少要提供一下功能:
    要维护各个服务器的IP等信息
    根据特定的逻辑选取服务器

 为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块
  1.Rule
  2.Ping
  3.ServerList

 

3. 编写ribbon 接口服务 ribbon-server-test

(1)导入jar包

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

  <dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>                                   
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

(2)编写测试接口

public class Person {

    private Integer id;

    private String name;

    private String message;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
@RestController
public class TestController {

    @GetMapping(value = "/person")
    public Person getPerson (HttpServletRequest request) {
        Person person = new Person();
        person.setId(1);
        person.setName("delan");
        person.setMessage(request.getRequestURL().toString());
        return person;
    }
}

(3)编写启动类(由于要做负载均衡,此处通过动态输入端口,来开启两个服务)

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.util.Scanner;

@SpringBootApplication
public class Application {
    public static void main( String[] args ) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入端口号:");
        String port = scan.nextLine();
        new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args);
    }
}

 

4. 编写ribbon 客户端 ribbon-client-test

(1)导入jar包

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

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-core</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-httpclient</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!--官方文档中 未导入一下包 测试发现编写代码找不到 com.netflix.config.ConfigurationManage-->
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.10</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-loadbalancer</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.archaius</groupId>
            <artifactId>archaius-core</artifactId>
            <version>0.7.6</version>
        </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>                                   <!--热启动配置-->
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

(2)编写ribbon客户端测试类

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;

import java.net.URI;

public class TestRibbon {
    public static void main(String[] args) throws Exception{
        //通过properties配置文件导入配置信息
        ConfigurationManager.loadPropertiesFromResources("my-ribbon.properties");

        //通过java代码导入配置信息
//        ConfigurationManager.getConfigInstance().setProperty(
//                "my-client.ribbon.listOfServers", "localhost:8080, localhost:8081");

        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/person")).build(); // 3
        for (int i = 0; i < 10; i++)  {
            HttpResponse response = client.executeWithLoadBalancer(request); // 4
            System.out.println("返回结果为 Status code for " + response.getRequestedURI() + "  :" + response.getStatus()+"内容:"+response.getEntity(String.class));
        }
    }
}

(3)my-ribbon.properties

#指定my-client 客户端的服务地址,如果不写my-client则对所有的ribbon客户端生效
my-client.ribbon.listOfServers = localhost:8080, localhost:8081

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!