dubbo直连

dubbo的直连方式即消费方不通过zookeeper注册中心去调用注册的服务,而是直接绕过zookeeper环节直接调用服务

2017-05-21_154952.png

直接上代码

【服务方provider】
接口:dubbo.direct.provider.DirectService.java

public interface DirectService {

    public String direct() throws Exception;
}

接口实现:dubbo.direct.provider.impl.DirecyServiceImpl.java

public class DirectServiceImpl implements DirectService{

    @Override
    public String direct() throws Exception {
        
        return "执行 DirectService exe";
    }

}

服务发布类:dubbo.direct.test.provider.java

public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"direct-provider.xml"});
        context.start();
        System.in.read();//为保证服务一直开着,利用输入流阻塞来模拟,开发中,这些应该打jar包在服务器上运行
    }
}

XML配置:direct-provider.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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
                        http://www.springframework.org/schema/beans/spring-beans.xsd        
                        http://code.alibabatech.com/schema/dubbo        
                        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 具体实现bean  扫描代替 -->
    <bean id="directService" class="dubbo.direct.provider.impl.DirectServiceImpl" />
   
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="direct-provider"  />
 
    <!-- 注意:dubbo直连不需要zookeeper做注册中心,但是还得配置否则报错 ,register="false"(只订阅,不注册) sbuscribe=false(只注册,不订阅)  -->
    <dubbo:registry address="zookeeper://192.168.22.128:2181" register="false" />
    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20882" />
    
    <!-- 声明需要暴露的服务接口,写操作可以设置retries=0避免重复调用SOA服务  注解代替--> 
    <dubbo:service interface="dubbo.direct.provider.DirectService" ref="directService" />
 
</beans>

启动服务

2017-05-21_162349.png

这里可以设置订阅与注册(只订阅不注册、只注册不订阅)

【消费方consumer】
接口:dubbo.direct.provider.DirectService.java

public interface DirectService {

    public String direct() throws Exception;
}

测试调用类:dubbo.direct.test.Consumer.java

public class Consumer {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"direct-consumer.xml"});
        context.start();
        
        DirectService directService = (DirectService) context.getBean("directService");
        
        System.out.println(directService.direct());
    }
}

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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
                        http://www.springframework.org/schema/beans/spring-beans.xsd        
                        http://code.alibabatech.com/schema/dubbo        
                        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="direct-consumer"  />
 
    <!-- 使用zookeeper注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://192.168.22.128:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="directService" url="dubbo://localhost:20882" interface="dubbo.direct.provider.DirectService" />
 
</beans>

调用

2017-05-21_162913.png
2017-05-21_162944.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Dubbo是什么 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式...
    Coselding阅读 17,287评论 3 196
  • 0 准备 安装注册中心:Zookeeper、Dubbox自带的dubbo-registry-simple;安装Du...
    七寸知架构阅读 14,026评论 0 88
  • ① 最近我把工作辞了,因为这份工作做得越来越不开心,我还是想顺从自己的意思。 ② 我想休息几天,所以不急着上班。开...
    慵懒姑娘阅读 226评论 0 0
  • 第一次读柳宗元的《小石潭记》,恍然间想到我也曾遇到过这样的地方。翻开记忆找寻那心中的小石潭。 小时候每到周末我总会...
    梦姿书斋阅读 2,777评论 2 9