码迷,mamicode.com
首页 > 其他好文 > 详细

Ribbon负载均衡(客户端)

时间:2018-08-14 14:23:40      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:manage   cto   project   路径   .net   服务器   dep   map   www.   

Ribbon是客户端的负载均衡机制,它有几种负载均衡机制。默认是轮询,我们也可以自定义规则。通过合理的分配网络请求来减小服务器的压力。

案例编写: 

  一:服务端搭建

  目录结构:

      技术分享图片

  1.1 导包

<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</groupId>
  <artifactId>Ribbon_server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <dependencies>
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
           <version>1.5.7.RELEASE</version>
        </dependency>
    </dependencies>
  
</project>

  1.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;
    }
}

  1.3编写controller层

@RestController
public class MyController {

    @RequestMapping(value = "/person", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(HttpServletRequest request) {
        Person p = new Person();
        p.setId(1);
        p.setName("angus");
        p.setMessage(request.getRequestURL().toString());
        return p;
    }
}

  1.4编写启动类

import java.util.Scanner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class ServiceApp {

    public static void main(String[] args) {
        //1. 启动main方法后,在控制台输入端口号.
        Scanner scan = new Scanner(System.in);
        String port = scan.nextLine();
        //以输入的端口号启动
        new SpringApplicationBuilder(ServiceApp.class)
                .properties("server.port="+port).run(args);
        //2. 启动2次,分别以8080, 8081启动,浏览器访问测试一下。
    }
}

  测试:  在控制台分别以8080 、 8081端口启动后,用浏览器访问目标接口。

      技术分享图片

  

   二:客户端搭建

   目录结构:

        技术分享图片

  2.1: 导入ribbon核心包

<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</groupId>
  <artifactId>Ribbon_client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <dependencies>
        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-core</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-httpclient</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
  
</project>

   2.2编写一个方法,用ribbon请求10次,看看是哪台服务器在为我们提供服务。 

public class Ribbon {

    public static void main(String[] args) throws Exception {
        //根据ribbon-httpclient发送请求
        ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        HttpRequest request = HttpRequest.newBuilder().uri("/person").build();
        //请求10次,看看每次的代理对象是谁
        for(int i = 0; i < 10; i++) {
            HttpResponse response = client.executeWithLoadBalancer(request);
            String json = response.getEntity(String.class);
            System.out.println(json);
        }
    }
}


=========================================
【控制台输出】

{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}

Ribbon负载均衡(客户端)

标签:manage   cto   project   路径   .net   服务器   dep   map   www.   

原文地址:https://www.cnblogs.com/wlwl/p/9473649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!