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
网友评论