使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)

第三步:创建消费者实现服务层面的负载均衡

同样创建Springboot项目,1.5.9版本
引入dubbo ,zookeeper依赖
由于需要写Controller获取Redis中的json数据,方便操作就引入了阿里的fastjson的依赖

使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第1张图片
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wdw</groupId>
    <artifactId>server_consumer1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server_consumer1</name>
    <description>First Consumer</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Dubbo依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.2.RELEASE</version>
        </dependency>
        <!--zookeeper依赖-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!--自定义接口-->
        <dependency>
            <groupId>com.wdw</groupId>
            <artifactId>Interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml:

server:
  port: 8083
  context-path: /person

dubbo:
  application:
    #这里模拟2个消费者,名字不一样,对Dubbo来说就是2个消费者
    name: Server_Consumer1
  registry:
    address: zookeeper://127.0.0.1:2181
    protocol: zookeeper

写一个控制器用于外部访问
此处的@Reference是dubbo的jar包中,偶尔自动导包会出现jdk包中的@Reference,需要修改
使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第2张图片
同样在ServerConsumer1Application加上@EnableDubbo注解,运行后,可以直接在Dubbo-admin页面下看见发布的ServerConsumer消费者

这里就不贴图了,直接运行,测试:
首先存数据:
使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第3张图片
使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第4张图片
可以看到多次发送请求会出现不同的返回结果,同理取数据也是2种结果
到此就实现了服务层的负载均衡
使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第5张图片
使用Springboot + Dubbo + Zookeeper + Redis实现远程RPC的数据存储和服务层负载均衡(三)_第6张图片

如有错误欢迎指正,接下来可能接着会加入dubbo monitor对调用的相关统计,和用nginx实现Http层的负载均衡。

你可能感兴趣的:(学习)