美文网首页
微架构 springcloud-12. 初识Ribbon

微架构 springcloud-12. 初识Ribbon

作者: 程序员_超 | 来源:发表于2018-03-26 22:20 被阅读0次

    何为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内容如下

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>
    </dependencies>
    
    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配置命令

    页面全都打印:

    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 配置如下:

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <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>
    
        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-loadbalancer</artifactId>
            <version>2.2.2</version>
        </dependency>
    
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.8</version>
        </dependency>
    
        <dependency>
            <groupId>com.netflix.archaius</groupId>
            <artifactId>archaius-core</artifactId>
            <version>0.7.4</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    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

          本文链接:https://www.haomeiwen.com/subject/dbydcftx.html