0
点赞
收藏
分享

微信扫一扫

Ribbon 框架简介及搭建


Ribbon简介

1.  负载均衡框架,支持可插拔式的负载均衡规则

2.  支持多种协议,如HTTP、UDP等

3.  提供负载均衡客户端

Ribbon子模块

1.  ribbon-core(ribbon的核心,主要包含负载均衡器、负载均衡接口、客户端接口、内置负载均衡实现API)

2.  ribbon-eureka(为eureka客户端提供的客户端实现类)

3.  ribbon-httpclient(为负载均衡提供了REST客户端)

负载均衡器组件

1.  一个负载均衡器,至少提供以下功能

1.1  要维护各个服务器的IP等信息

1.2  根据特定逻辑选取服务器

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

2.1  Rule

2.2  Ping

2.3  ServerList

由于本次的教程是没有与SpringCloud整合的,是用来单独使用的,下面就教大家怎么搭建Ribbon程序 并 调用服务。

1:创建Ribbon服务器(一个单纯的SpringBoot程序)

pom.xml


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


为了方便Ribbon客户端测试,在这里建一个实体类:Person.java


public class Person {
    private String url;// 处理请求的服务器url
    private String message;// 提示信息
    
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
 
PersonController.java
 
@RestController
public class PersonController {

    @RequestMapping(value="/getPerson", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(HttpServletRequest request){
        Person p = new Person();
        p.setMessage("请求成功");
        p.setUrl(request.getRequestURL().toString());
        return p;
    }
}


启动类:Application.java(因为要测试负载均衡,所有这里需要启动多个服务,以下配置以手动输入端口号方式启动)


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String port = scan.nextLine();
        new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args);
    }
}


本次启动以端口:8080、8081分别启动,稍后我们配置完客户端 统一测试(配置后,将服务启动)

2:创建Ribbon客户端

pom.xml 中只需要引入核心及客户端的依赖即可

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


编写main方法测试,调用服务(服务列表也可以直接在配置文件中配置,本次用setProperty进行配置)其中的my-client,这个名称可以任意起,因为这个名称是用来命名配置创建客户端的。

public static void main(String[] args) {
    try {
        // 写入服务列表
        ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
        // 输出服务列表
        System.out.println("服务列表:" + ConfigurationManager.getConfigInstance().getProperty("my-client.ribbon.listOfServers"));
        // 获取客户端(如果获取不到,可通过getNamedClient方法自动创建)
        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        // 创建request对象
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/getPerson")).build();// 写入将要访问的接口
        // 多次访问测试
        for (int i = 0; i < 10; i++) {
            // 创建response对象
            HttpResponse response = client.executeWithLoadBalancer(request);
            // 接收请求结果
            String json = response.getEntity(String.class);
            // 打印结果
            System.out.println(json);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


以上就是Ribbon单独使用的全部过程,下面大家看一下Ribbon负载均衡默认的轮询规则


服务列表:[localhost:8080, localhost:8081]
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}

举报

相关推荐

0 条评论