Dubbo和Zookeeper建立分布式入门

一、准备工作

先要安装Zookeeper,只需解压到非中文路径即可,注意,zookeeper端口为2181,务必不要被占用。
然后要创建服务提供者和服务消费者,即:提供service具体业务的service项目、和web层直接使用该业务的项目,两者均需要定为web工程。

引入pom文件:

<dependencies>
        <dependency>
            <groupId>com.fh</groupId>
            <artifactId>total-service-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

下面创建带有服务分组和版本号的dubbo案例:

二、创建服务提供者

1、xml配置文件方式:
编写xml配置文件:

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--    将实现了准备暴露服务的接口的两个实现类都注册到spring中-->
    <bean class="com.fh.service.user.impl.UserServiceImpl" id="usi1"/>
    <bean class="com.fh.service.user.impl.UserServiceImpl2" id="usi2"/>

    <dubbo:application name="u1_provider"></dubbo:application>
<!--    对UserService接口进行服务暴露,
        暴露内容关联具体实现内容usi1、usi2
        对两者各自进行服务分组g1、g2
        两者的负载均衡方式均为加权轮询
        两者权重分别为30、70
        版本号分别为1.0.0和2.0.0-->
    <dubbo:service interface="com.fh.service.user.UserService" ref="usi1" group="g1" loadbalance="roundrobin" weight="30" version="1.0.0"></dubbo:service>
    <dubbo:service interface="com.fh.service.user.UserService" ref="usi2" group="g2" loadbalance="roundrobin" weight="70" version="2.0.0"></dubbo:service>
<!--    注册中心地址-->
    <dubbo:registry address="zookeeper://localhost" port="2181"></dubbo:registry>
<!--    服务提供者的注册地址以及传输协议-->
    <dubbo:protocol port="21881" name="dubbo"></dubbo:protocol>
</beans>

编写暴露服务的具体接口实现类:

public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public User findUser(Integer id) {
        System.out.println("我是1号!!!!!!");
        return userMapper.findUser(id);
    }
}

2、注解方式
编写xml配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
    <dubbo:application name="u1_provider"></dubbo:application>
<!--    注册中心地址-->
    <dubbo:registry address="zookeeper://localhost" port="2181"></dubbo:registry>
<!--    服务提供者的注册地址以及传输协议-->
    <dubbo:protocol port="21881" name="dubbo"></dubbo:protocol>
<!--    定义dubbo去扫描的暴露具体服务内容实现类所在的包-->
    <dubbo:annotation package="com.fh.service"></dubbo:annotation>
</beans>

编写暴露服务的具体接口实现类:

/**
 * 对UserService接口进行服务暴露,
 * 暴露内容关联具体实现内容不需指定,直接指向注解修饰的类的对象
 * 服务分组为g1
 * 负载均衡方式均为加权轮询
 * 权重分别为30
 * 版本号为1.0.0
 */
@Service(interfaceClass = com.fh.service.user.UserService.class,group = "g1",loadbalance = "roundrobin",weight = 30,version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    @Override
    public User findUser(Integer id) {
        System.out.println("我是1号!!!!!!");
        return userMapper.findUser(id);
    }
}

三、创建服务消费者

1、xml配置文件方式:
编写xml配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <dubbo:application name="u1_consumer"></dubbo:application>
<!--    定义接收服务提供者数据的对象的具体信息:
        关联服务提供者暴露的接口UserService
        分别对相同服务分组g1、g2关联数据
        当前接收服务提供者数据的对象id分别为us1、us2
        超时时间
        版本号分别为1.0.0、2.0.0-->
    <dubbo:reference interface="com.fh.service.user.UserService" group="g1" id="us1" timeout="5000" version="1.0.0"></dubbo:reference>
    <dubbo:reference interface="com.fh.service.user.UserService" group="g2" id="us2" timeout="5000" version="2.0.0"></dubbo:reference>
    <!--    注册中心地址-->
    <dubbo:registry address="zookeeper://localhost" port="2181"></dubbo:registry>
    <!--指定是否使用合理化检查配置:服务的消费者启动时会检查服务的提供者的存在,在提供者不存在时,开启合理化检查,会直接报错,不开启则会在访问时获取不到数据才报错-->
    <dubbo:consumer check="true"></dubbo:consumer>
</beans>

编写接收服务数据的类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig(locations = {"classpath*:/spring/applicationContext-*.xml"})
public class ConsumerTest {
    @Autowired
    private UserService us1;
    @Autowired
    private UserService us2;
    @Test
    public void test1(){
        System.out.println(us1.findUser(1));
        System.out.println(us2.findUser(1));
    }
}

2、注解方式:
编写配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <dubbo:application name="u1_consumer"></dubbo:application>
    <!--    注册中心地址-->
    <dubbo:registry address="zookeeper://localhost" port="2181"></dubbo:registry>
    <!--指定是否使用合理化检查配置:服务的消费者启动时会检查服务的提供者的存在,在提供者不存在时,开启合理化检查,会直接报错,不开启则会在访问时获取不到数据才报错-->
    <dubbo:consumer check="true"></dubbo:consumer>
    <!--    定义dubbo去扫描的消费服务的对象所在的包-->
    <dubbo:annotation package="com.fh.web.controller"></dubbo:annotation>
</beans>

编写接收服务数据的类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig(locations = {"classpath*:/spring/applicationContext-*.xml"})
public class ConsumerTest {
    /**
     * 定义接收服务提供者数据的对象的具体信息:
     * 关联服务提供者暴露的接口UserService
     * 分别对相同服务分组g1、g2关联数据
     * 当前接收服务提供者数据的对象id分别为us1、us2
     * 超时时间
     * 版本号分别为1.0.0、2.0.0
     */
    @Reference(interfaceClass = com.fh.service.user.UserService.class,group = "g1",timeout = 5000,version = "1.0.0")
    private UserService us1;
    @Reference(interfaceClass = com.fh.service.user.UserService.class,group = "g2",timeout = 5000,version = "2.0.0")
    private UserService us2;
    @Test
    public void test1(){
        System.out.println(us1.findUser(1));
        System.out.println(us2.findUser(1));
    }
}

创建步骤完成,准备启动!

四、启动步骤

分别对服务提供者和服务消费者创建tomcat,确保设置不同的端口。同时,可以再创建第三个tomcat,设置新端口,并引入dubbo-admin的war包,创建监控器用以查看。
在启动服务提供者、服务消费者、监控器之前,需优先启动ZooKeeper。

且一般上线阶段才会将服务提供者运行在tomcat上,开发阶段都是在服务提供者内创建测试类:

package com.fh.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class TestDemo {
    public static void main(String[] args) throws IOException {
        //读取配置文件,创建服务运行容器applicationContext-dubbo和applicationContext-tx
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-*.xml");
        //开启applicationContext-dubbo和applicationContext-tx容器,applicationContext-dubbo的开启会启动服务提供者
        applicationContext.start();
        //设置在控制台按任意键退出,目的是为了pause卡主容器,让服务提供者一直保持开启状态
        System.in.read();
    }
}

在修改服务提供者的暴露端口(<dubbo:protocol name=“dubbo” port=“20881”></dubbo:protocol>)的情况下,服务提供者可以进行多次启动,达到多个服务提供者同时的共同运行。

最终在dubbo admin上,不同服务分组、版本号的服务提供者和服务消费者信息展示如下:
服务消费者:
在这里插入图片描述

服务提供者:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值