微架构 springcloud-12. 初识Ribbon

何为Ribbon?

同Eureka一样,Ribbon 是Netflix 下的一个框架,它主要负责客户端应用实现【负载均衡】目的!

应用Ribbon:第一个Ribbon程序

搭建工程

  1. 新建一个web工程,即:Springboot(Maven) 工程 RibbonService:
RibbonService
|   +---src
|   +---main
|   |   +---java
|   |   |   \---person
|   |   |       \---jack
|   |   |           |   App.java
|   |   |           |
|   |   |           \---controller
|   |   |                   AppController.java
|   |   |
|   |   \---resources
|   |           application-8080.properties
|   |           application-8081.properties
    +---pom.xml

pom.xml内容如下


    
        org.springframework.boot
        spring-boot-starter-web
        1.5.9.RELEASE
    

  1. AppController 内容如下:
package person.jack.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AppController {
    @GetMapping("/info")
    public @ResponseBody String info(HttpServletRequest req){
        return "springBoot 是javaWEB开发最优美得姿势!"+req.getRequestURL();
    }
}
  1. application-8080.properties 内容如下:
server.port=8080
  1. application-8081.properties 内容如下:
server.port=8081
  1. 配置maven启动

ribbonService-8080

Name: ribbonService-8080
Working dirctory: $Workspace/RibbonService
Command line:exec:java -Dexec.mainClass=person.jack.App -Dspring.profiles.active=8080

ribbonService-8081

Name: ribbonService-8081
Working dirctory: $Workspace/RibbonService
Command line:exec:java -Dexec.mainClass=person.jack.App -Dspring.profiles.active=8081
  1. 分别运行以上两个maven配置命令
  • 测试:http://localhost:8080/info
  • 测试:http://localhost:8081/info

页面全都打印:

springBoot 是javaWEB开发最优美得姿势!http://localhost:8080/info

# 即:一份程序运行了两个实例,8080、8081,完成负载均衡条件搭建

搭建Ribbon客户端

  1. 新建maven java 项目:RibbonClient
RibbonClient
+---src
|   +---main
|   |   +---java
|   |   |   \---person
|   |   |       \---jack
|   |   |           \---ribbon
|   |   |               \---client
|   |   |                       RibbonClientTest.java
+---pom.xml

pom.xml 配置如下:


    1.8
    1.8
    UTF-8




    
        com.netflix.ribbon
        ribbon-core
        2.2.2
    

    
        com.netflix.ribbon
        ribbon-httpclient
        2.2.2
    

    
        com.netflix.ribbon
        ribbon-loadbalancer
        2.2.2
    

    
        commons-configuration
        commons-configuration
        1.8
    

    
        com.netflix.archaius
        archaius-core
        0.7.4
    

    
        com.google.guava
        guava
        16.0
    



    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.5.1
            
                1.6
                1.6
                UTF-8
            
        
    

  1. RibbonClientTest.java 内容如下:
package person.jack.ribbon.client;

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;

public class RibbonClientTest {
    public static void main(String[] args) throws Exception {
        ConfigurationManager.getConfigInstance().setProperty(
                "myClient.ribbon.listOfServers", "localhost:8080,localhost:8081");
        RestClient client = (RestClient) ClientFactory.getNamedClient("myClient");
        HttpRequest request = HttpRequest.newBuilder().uri("/info").build();
        for(int i = 0; i < 10; i++) {
            HttpResponse response = client.executeWithLoadBalancer(request);
            String json = response.getEntity(String.class);
            System.out.println(json);
        }
    }
}
  1. 运行以上main 函数,控制台输出:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
springBoot 是javaWEB开发最优美的姿势!http://localhost:8081/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8081/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8081/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8081/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8081/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
  1. 由以上运行结果分析可知:Ribbon客户端 访问 /info 这个请求,该请求8080、8081都可提供,结果出现了“轮询”,压力被这两个实例分摊了。这就是Ribbon,其默认负载均衡算法采用的是“轮询”算法!

  2. 断开 8081端口服务,重新运行main函数,输出如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info
springBoot 是javaWEB开发最优美的姿势!http://localhost:8080/info

现在提供服务的只剩8080端口了,但并不影响服务的运行!负载功能测试成功!

你可能感兴趣的:(微架构 springcloud-12. 初识Ribbon)