基于Dubbo的动态远程调用
2017-06-15 18:58
393 查看
非常感谢 http://blog.csdn.net/michaelzhaozero/article/details/44079655
基于Dubbo的动态远程调用
问题:为解决实际业务,由我方提供接口定义,具体的实现交给第三方处理。然后由第三方将开发好的服务注册到他们自己的Dubbo服务上,由我方调用。问题就在于多个第三方开发具体实现,对于我方而言如果按照配置方式切入调用是无法满足这种需求。所以找寻了dubbo的根据URL远程调用服务的机制。以下是Demo
关于Zookeeper的安装&配置此次不详细介绍了。
服务调用方
定义服务接口:
[java] view
plain copy
package com.demo.service;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public interface DemoService {
public String deal(String someting);
}
定义Consumer配置文件:
[html] view
plain copy
<?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="dubbo_consumer" />
</beans>
远程调用测试类:
[java] view
plain copy
package com.demo.test;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Desc:测试Dubbo实时映射bean
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/spring/root-context.xml"
})
@Slf4j
public class RealReference {
//用于将bean关系注入到当前的context中
@Autowired
private ApplicationContext applicationContext;
@Test
public void realReference() {
String url = "dubbo://localhost:21880/com.demo.service.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口
ReferenceBean<DemoService> referenceBean = new ReferenceBean<DemoService>();
referenceBean.setApplicationContext(applicationContext);
referenceBean.setInterface(com.demo.service.DemoService.class);
referenceBean.setUrl(url);
try {
referenceBean.afterPropertiesSet();
DemoService demoService = referenceBean.get();
System.out.print(demoService.deal("Tester"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务提供方实现接口:
实现1:
[java] view
plain copy
package com.dubboT.service;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* Desc:Dubbo动态加载服务测试
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
@Service
@Slf4j
public class DemoServiceImpl implements DemoService {
@Override
public String deal(String s) {
return "My Name is " + s;
}
}
实现2:
[java] view
plain copy
package com.dubboT.service;
import com.demo.service.DemoService;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-05.
*/
public class DemoServiceNImpl implements DemoService {
@Override
public String deal(String s) {
return "This is DemoServiceNImpl " + s;
}
}
分别在不同的端口暴露Service的实现
[html] view
plain copy
<?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 id="demoService" class="com.rabbit.service.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubboProvider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />
</beans>
DemoServiceNImpl在21800端口暴露
[html] view
plain copy
<?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 id="demoService" class="com.rabbit.service.DemoServiceNImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubboProvider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="21880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />
</beans>
分别提供服务
[java] view
plain copy
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/rabbit-dubbo-provider.xml"});
context.start();
System.in.read();
}
}
[html] view
plain copy
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public class DubboProvider2Main {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-provider.xml"});
context.start();
System.in.read();
}
}
启动服务后,运行RealReference实现根据URL调用远程服务
[html] view
plain copy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My Name is Tester
Process finished with exit code 0
基于Dubbo的动态远程调用
问题:为解决实际业务,由我方提供接口定义,具体的实现交给第三方处理。然后由第三方将开发好的服务注册到他们自己的Dubbo服务上,由我方调用。问题就在于多个第三方开发具体实现,对于我方而言如果按照配置方式切入调用是无法满足这种需求。所以找寻了dubbo的根据URL远程调用服务的机制。以下是Demo
关于Zookeeper的安装&配置此次不详细介绍了。
服务调用方
定义服务接口:
[java] view
plain copy
package com.demo.service;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public interface DemoService {
public String deal(String someting);
}
定义Consumer配置文件:
[html] view
plain copy
<?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="dubbo_consumer" />
</beans>
远程调用测试类:
[java] view
plain copy
package com.demo.test;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Desc:测试Dubbo实时映射bean
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/spring/root-context.xml"
})
@Slf4j
public class RealReference {
//用于将bean关系注入到当前的context中
@Autowired
private ApplicationContext applicationContext;
@Test
public void realReference() {
String url = "dubbo://localhost:21880/com.demo.service.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口
ReferenceBean<DemoService> referenceBean = new ReferenceBean<DemoService>();
referenceBean.setApplicationContext(applicationContext);
referenceBean.setInterface(com.demo.service.DemoService.class);
referenceBean.setUrl(url);
try {
referenceBean.afterPropertiesSet();
DemoService demoService = referenceBean.get();
System.out.print(demoService.deal("Tester"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务提供方实现接口:
实现1:
[java] view
plain copy
package com.dubboT.service;
import com.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* Desc:Dubbo动态加载服务测试
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
@Service
@Slf4j
public class DemoServiceImpl implements DemoService {
@Override
public String deal(String s) {
return "My Name is " + s;
}
}
实现2:
[java] view
plain copy
package com.dubboT.service;
import com.demo.service.DemoService;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-05.
*/
public class DemoServiceNImpl implements DemoService {
@Override
public String deal(String s) {
return "This is DemoServiceNImpl " + s;
}
}
分别在不同的端口暴露Service的实现
[html] view
plain copy
<?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 id="demoService" class="com.rabbit.service.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubboProvider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />
</beans>
DemoServiceNImpl在21800端口暴露
[html] view
plain copy
<?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 id="demoService" class="com.rabbit.service.DemoServiceNImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubboProvider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="21880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />
</beans>
分别提供服务
[java] view
plain copy
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/rabbit-dubbo-provider.xml"});
context.start();
System.in.read();
}
}
[html] view
plain copy
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Desc:
* Mail:v@terminus.io
* author:Michael Zhao
* Date:2015-03-04.
*/
public class DubboProvider2Main {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-provider.xml"});
context.start();
System.in.read();
}
}
启动服务后,运行RealReference实现根据URL调用远程服务
[html] view
plain copy
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My Name is Tester
Process finished with exit code 0
相关文章推荐
- 基于Dubbo的动态远程调用
- 基于Dubbo的动态远程调用
- 基于Dubbo的Hessian协议实现远程调用
- 基于Dubbo的Hessian协议实现远程调用
- [转载] 基于Dubbo的Hessian协议实现远程调用
- 使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。
- 基于dubbo的hessian协议远程调用实例
- 基于XML-RPC的远程调用(Python,Java)
- 基于Remoting的远程调用_客户端实现
- 【转】java的反射机制中的动态代理代理(二)--在远程方法调用中运用代理类
- jquery动态创建div,调用dialog方法显示远程iframe文件,封装jquery方法
- 基于XML-RPC的远程调用(Python,Java)
- 基于COM组件技术C++调用C#的动态链接库
- RemoteObject动态连接(调用远程services-config.xml)
- html5-websocket基于远程方法调用的数据交互实现
- alibaba远程调用框架dubbo原理
- 基于用户级线程的远程调用效率测试
- 基于Remoting的远程调用_服务器端实现
- 基于用户级线程的远程调用效率测试
- Delphi动态调用Java的WebService —— 基于“Axis2发布WebService例子(HelloWorld)”