基于Dubbo的动态远程调用
问题:为解决实际业务,由我方提供接口定义,具体的实现交给第三方处理。然后由第三方将开发好的服务注册到他们自己的Dubbo服务上,由我方调用。问题就在于多个第三方开发具体实现,对于我方而言如果按照配置方式切入调用是无法满足这种需求。所以找寻了dubbo的根据URL远程调用服务的机制。以下是Demo
关于Zookeeper的安装&配置此次不详细介绍了。
服务调用方
定义服务接口:
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配置文件:
<?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>
远程调用测试类:
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:
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:
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的实现
<?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端口暴露
<?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>
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();
}
}
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调用远程服务
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