【SSM】ZooKeeper和Dubbo

前言

      Dubbo是阿里开源的一个分布式服务框架,可是阿里内部用的倒是HSF(High-speed Service Framework)。下面看看怎么使用吧。html

Zookeeper

      Dubbo是个RPC调用框架,本质上不须要依赖中间件就能够完成点对点的通讯,可是实际生产环境中,在动态扩容和下线等状况下,服务提供者和消费者的地址不多是一直固定的,因此须要一个固定的第三方让双方暴露服务和发现服务,这也就是注册中心的存在乎义,Dubbo官方推荐ZooKeeper。下面在windows下单机部署ZooKeeper做为演示。java

     1.官网下载ZooKeeper,地址 http://zookeeper.apache.org/web

     2.解压后在conf路径下将zoo_sample.cfg改为zoo.cfg,默认数据文件会存放在C:\tmp\zookeeper,端口号是2181spring

     3.在bin路径下,双击zkServer.cmd,正常会出现下面apache

binding to port 0.0.0.0/0.0.0.0:2181

    4.运行zkCli.cmd,正常会出现下面windows

WatchedEvent state:SyncConnected type:None path:null

Dubbo

     Dubbo内部封装了注册,心跳,RPC调用等复杂逻辑,因此使用上很是简单,只须要配置一下注册中心地址和相关的服务提供便可完成。下面是完成服务提供和消费所须要改动的文件api

 

 api模块负责向其余系统提供接口和模块,做为一个jar包分发出去,因此在这里只须要定义一个facade接口,如浏览器

public interface TestFacade { String getDeptName(String id); }

api接口的实如今biz模块中,而DeptServiceImpl文件中,须要将sal模块中定义的bean导入mvc

public class TestFacadeImpl implements TestFacade { @Override public String getDeptName(String id) { return "部门名称:"+id; } }
@Service public class DeptServiceImpl implements DeptService { @Autowired DeptDOMapper deptDOMapper; @Autowired TestClient testClient; @Override public List<DeptInfo> listDeptInfo(String name) { System.out.println(testClient.getDeptName("123")); List<DeptDO> deptDOList = deptDOMapper.listDept(name); List<DeptInfo> deptInfos = new ArrayList<>(); deptDOList.stream().forEach(x -> { DeptInfo info = new DeptInfo(); info.setName(x.getName()); info.setId(x.getId()); deptInfos.add(info); }); } }
DeptServiceImpl.java

sal负责调用其余应用的服务,处理相应数据,而后再给biz模块提供相应的服务,一般没有复杂的逻辑都是直接透传的,如app

public interface TestClient { String getDeptName(String id) ; }
@Service public class TestClientImpl implements TestClient { @Autowired TestFacade testFacade; @Override public String getDeptName(String id) { return testFacade.getDeptName(id); } }

另外还要在web模块配置Dubbo和依赖

<?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:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 -->
    <!--<dubbo:application name="consumer-of-test-app" />-->

    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/>


    <!-- 生成远程服务代理,能够和本地bean同样使用 -->
    <dubbo:reference id="testFacade" check="false" interface="cn.com.test.springmvc.api.TestFacade" />
</beans>
consumer.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:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="test-app"  />

    <!-- 使用zookeeper注册中心 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明须要暴露的服务接口 -->
    <dubbo:service interface="cn.com.test.springmvc.api.TestFacade" ref="testFacadeImpl" />

    <!-- 和本地bean同样实现服务 -->
    <bean id="testFacadeImpl" class="cn.com.test.springmvc.biz.apiimpl.TestFacadeImpl" />
</beans>
provider.xml
applicationContext.xml中加入引用资源
<import resource="provider.xml"></import>
   <import resource="consumer.xml"></import>

pom.xml加入依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>

测试

      这里一切从简就不拆成两个应用了,一个应用充当服务者和消费者两个角色,先运行zkServer.cmd,再运行应用,在浏览器输入例如这样的连接http://localhost:8080/dept/getList?name=1,会看控制台看到输出,如

部门名称:123

调用成功。

 

Dubbo官方文档  http://dubbo.apache.org/zh-cn/docs/user/preface/background.html

相关文章
相关标签/搜索