dubbo与spring mvc

安装

一、本地服务
 
  1、定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
public interface CustomerService {
	public String getName();
}
   2、在服务提供方实现接口:(对服务消费方隐藏实现)
  
public class CustomerServiceImpl implements CustomerService{
	@Override
	public String getName() {
		System.out.print("我打印");
		return "打印结果";
	}
}
  3、然后引入dubbo的几个包
     dubbo-2.5.3.jar
     log4j.jar
     netty-3.5.7.Final.jar
     slf4j.jar
     slf4j-log4j.jar
     zkclient.jar
     zookeeper.jar
 
  4、用Spring配置声明暴露服务:
   
      新建applicationProvider.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="demoService" class="com.jinbin.service.customer.CustomerServiceImpl" />  
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="xixi_provider"  />    
    <!-- 使用multicast广播注册中心暴露服务地址   
    <dubbo:registry address="multicast://localhost:1234" />-->   
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://192.168.1.3:2181" />     
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.jinbin.service.customer.CustomerService" ref="demoService" />  
</beans>  


我这里暴露服务器的地址交由zookeeper来管理的,使用者首先先要安装zookeeper应用才能使用此功能,相关安装步骤请参看相关博文

 

5、加载Spring配置,并调用远程服务:(也可以使用IoC注入)
public class DubooProvider {
	public static void main(String[] args) {
	    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[]{"applicationProvider.xml"});  
        context.start();  
        System.out.println("Press any key to exit.");  
        try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
}

并且启动,使其进入启动状态。
 
以上为服务器提供者的完整步骤,功能接口都已经写好,下面我们就开始怎么远程调用
 
二、服务消费者
 
 1、新建个配置文件applicationConsumer.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="consumer-of-helloworld-app" />     
      <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.1.3:2181" />       
      <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
    <dubbo:reference id="demoService" interface="com.jinbin.service.customer.CustomerService" />  
</beans>  


为了在web中使用,我们在web.xml中配置在spring启动读取过程中
   <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/application.xml /WEB-INF/applicationConsumer.xml</param-value>
    </context-param>

2、接口调用
 
  调用过程很简单,先把接口文件打成jar包,然后在此工程中进行引用
 
 在springmvc调用程序如下:
@Autowired CustomerService demoService ;	 
  @RequestMapping(value="duboo1")
  public void duboo1(){
		   demoService.getName();
}

即可执行成功
 
三、dubbo-admin的使用
 
 
    下载dubbo-admin-2.5.3.war
 
    将其放到tomcat下面,配置dubbo.properties,
vi webapps/ROOT/WEB-INF/dubbo.properties
dubbo.properties
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
      修改zookeeper的URL和端口
     

    启动:

./bin/startup.sh
posted @ 2014-11-29 19:56  火腿骑士  阅读(757)  评论(0编辑  收藏  举报