dubbo高级应用

dubbo高级应用:

1,dubbo直连方式

默认的连接服务提供端的端口是20880,用直连的的方式可以指定提供端的url和这个具体服务暴露的端口(一个合作企业一个端口)

用直连的方式可以绕过zk等注册中心

2,一个系统既可以配成提供端也可以配成消费端,配合直连方式指定消费哪个服务提供的那个接口

3,用配置的方式最后的原理还是通过dubbo的客户端,通过ip+端口获取服义务和暴露服务

@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();  

        }  

    }  

基于Dubbo的动态远程调用

问题:为解决实际业务,由我方提供接口定义,具体的实现交给第三方处理。然后由第三方将开发好的服务注册到他们自己的Dubbo服务上,由我方调用。问题就在于多个第三方开发具体实现,对于我方而言如果按照配置方式切入调用是无法满足这种需求。所以找寻了dubbo的根据URL远程调用服务的机制。以下是Demo

关于Zookeeper的安装&配置此次不详细介绍了。

服务调用方

定义服务接口:

 

[java]  view plain  copy
 
  1. package com.demo.service;  
  2.   
  3. /** 
  4.  * Desc: 
  5.  * Mail:[email protected] 
  6.  * author:Michael Zhao 
  7.  * Date:2015-03-04. 
  8.  */  
  9. public interface DemoService {  
  10.     public String deal(String someting);  
  11. }  


定义Consumer配置文件:

 

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  9.   
  10.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  11.     <dubbo:application name="dubbo_consumer" />  
  12. </beans>  


远程调用测试类:

 

 

[java]  view plain  copy
 
  1. package com.demo.test;  
  2.   
  3. import com.alibaba.dubbo.config.spring.ReferenceBean;  
  4. import com.demo.service.DemoService;  
  5. import lombok.extern.slf4j.Slf4j;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.context.ApplicationContext;  
  10. import org.springframework.test.context.ContextConfiguration;  
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  12.   
  13. /** 
  14.  * Desc:测试Dubbo实时映射bean 
  15.  * Mail:[email protected] 
  16.  * author:Michael Zhao 
  17.  * Date:2015-03-04. 
  18.  */  
  19. @RunWith(SpringJUnit4ClassRunner.class)  
  20. @ContextConfiguration(locations = {  
  21.         "classpath:/spring/root-context.xml"  
  22. })  
  23. @Slf4j  
  24. public class RealReference {  
  25.     //用于将bean关系注入到当前的context中  
  26.     @Autowired  
  27.     private ApplicationContext applicationContext;  
  28.   
  29.     @Test  
  30.     public void realReference() {  
  31.         String url = "dubbo://localhost:21880/com.demo.service.DemoService";//更改不同的Dubbo服务暴露的ip地址&端口  
  32.   
  33.         ReferenceBean<DemoService> referenceBean = new ReferenceBean<DemoService>();  
  34.         referenceBean.setApplicationContext(applicationContext);  
  35.         referenceBean.setInterface(com.demo.service.DemoService.class);  
  36.         referenceBean.setUrl(url);  
  37.   
  38.         try {  
  39.             referenceBean.afterPropertiesSet();  
  40.             DemoService demoService = referenceBean.get();  
  41.             System.out.print(demoService.deal("Tester"));  
  42.         } catch (Exception e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.     }  
  46. }  



 

服务提供方实现接口:

实现1:

 

[java]  view plain  copy
 
  1. package com.dubboT.service;  
  2.   
  3. import com.demo.service.DemoService;  
  4. import lombok.extern.slf4j.Slf4j;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. /** 
  8.  * Desc:Dubbo动态加载服务测试 
  9.  * Mail:[email protected] 
  10.  * author:Michael Zhao 
  11.  * Date:2015-03-04. 
  12.  */  
  13. @Service  
  14. @Slf4j  
  15. public class DemoServiceImpl implements DemoService {  
  16.     @Override  
  17.     public String deal(String s) {  
  18.         return "My Name is " + s;  
  19.     }  
  20. }  


实现2:

 

 

[java]  view plain  copy
 
  1. package com.dubboT.service;  
  2.   
  3. import com.demo.service.DemoService;  
  4.   
  5. /** 
  6.  * Desc: 
  7.  * Mail:[email protected] 
  8.  * author:Michael Zhao 
  9.  * Date:2015-03-05. 
  10.  */  
  11. public class DemoServiceNImpl implements DemoService {  
  12.     @Override  
  13.     public String deal(String s) {  
  14.         return "This is DemoServiceNImpl " + s;  
  15.     }  
  16. }  


分别在不同的端口暴露Service的实现

 

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.         http://code.alibabatech.com/schema/dubbo    
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd    
  9.         ">  
  10.   
  11.     <bean id="demoService" class="com.rabbit.service.DemoServiceImpl" />  
  12.   
  13.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  14.     <dubbo:application name="dubboProvider"  />  
  15.   
  16.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  17.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  18.   
  19.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  20.     <dubbo:protocol name="dubbo" port="20880" />  
  21.   
  22.     <!-- 声明需要暴露的服务接口 -->  
  23.     <dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />  
  24.   
  25. </beans>    


DemoServiceNImpl在21800端口暴露

 

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd    
  7.         http://code.alibabatech.com/schema/dubbo    
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd    
  9.         ">  
  10.   
  11.     <bean id="demoService" class="com.rabbit.service.DemoServiceNImpl" />  
  12.   
  13.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  14.     <dubbo:application name="dubboProvider"  />  
  15.   
  16.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  17.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  18.   
  19.     <dubbo:protocol name="dubbo" port="21880" />  
  20.   
  21.     <!-- 声明需要暴露的服务接口 -->  
  22.     <dubbo:service interface="com.elasticsearch.service.DemoService" ref="demoService" />  
  23.   
  24. </beans>    

 

 

分别提供服务

 

[java]  view plain  copy
 
  1. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  2.   
  3. /** 
  4.  * Desc: 
  5.  * Mail:[email protected] 
  6.  * author:Michael Zhao 
  7.  * Date:2015-03-04. 
  8.  */  
  9. public class DubboProviderMain {  
  10.     public static void main(String[] args) throws Exception {  
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/rabbit-dubbo-provider.xml"});  
  12.         context.start();  
  13.   
  14.         System.in.read();  
  15.     }  
  16. }  
[html]  view plain  copy
 
  1. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  2.   
  3. /**  
  4.  * Desc:  
  5.  * Mail:[email protected]  
  6.  * author:Michael Zhao  
  7.  * Date:2015-03-04.  
  8.  */  
  9. public class DubboProvider2Main {  
  10.     public static void main(String[] args) throws Exception {  
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-provider.xml"});  
  12.         context.start();  
  13.   
  14.         System.in.read();  
  15.     }  
  16. }  



启动服务后,运行RealReference实现根据URL调用远程服务

 

 

[html]  view plain  copy
 
  1. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".  
  2. SLF4J: Defaulting to no-operation (NOP) logger implementation  
  3. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.  
  4. log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).  
  5. log4j:WARN Please initialize the log4j system properly.  
  6. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  
  7. My Name is Tester  
  8. Process finished with exit code 0  

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2371317
今日推荐