【SSM】ZooKeeper和Dubbo

前言

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

Zookeeper

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

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

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

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

binding to port 0.0.0.0/0.0.0.0:2181

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

WatchedEvent state:SyncConnected type:None path:null

Dubbo

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

 

 api模块负责向其他系统提供接口和模块,作为一个jar包分发出去,所以在这里只需要定义一个facade接口,如

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

api接口的实现在biz模块中,而DeptServiceImpl文件中,需要将sal模块中定义的bean导入

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模块提供相应的服务,通常没有复杂的逻辑都是直接透传的,如

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

     

原文地址:https://www.cnblogs.com/caizl/p/10716785.html