dubbo + zookeeper 配置

Dubbo与Zookeeper、SpringMVC整合和使用

windows环境介绍:

  myeclipse 10

  jdk1.6

  tomcat 6.0.35  

一、安装Zookeeper

  1.通过链接下载对应的包 http://www.apache.org/dist/zookeeper/

  2.Zookeeper下载后解压即可,见下图

  

  3.进入到conf里面,会看到zoo_sample.cfg文件。将zoo_sample.cfg改成bak文件,并复制一个修改为zoo.cfg,修改相关配置内容,注意修改的日志文件夹需要自己手动创建

   

  4.进入D:\zookeeper-3.4.6\bin,双击zkServer.cmd,见到如下界面,就表示zookeeper已启动成功

  

二、安装dubbo

  1.本人使用的是dubbo-admin-2.5.3.war

  2.拷贝一个新的tomcat,并将tomcat/webapps里面的ROOT文件夹删掉

  3.将dubbo-admin-2.5.3.war重命名为ROOT.war,并拷贝到tomcat/webapps目录下

  4.启动tomcat

   

    出现上述问题表示你没有启动zookeeper

   5.dubbo发布成功后,输入http://localhost:8889/dubbo-admin-2.5.3/,此时出现登录页面,输入root/root进行登录,登录成功后如图

    

三、创建服务提供服务(provider)

  项目结构如下图

  

  相应的类中的代码如下:

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  
  <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>

    <servlet>
        <servlet-name>provider</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml,classpath:applicationContext-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>provider</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- 字符过滤器 -->
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  DemoService.java  

package com.provider;

public interface DemoService {
    
    String sayHello(String name);
}

  DemoServiceImpl.java  

package com.provider;

import org.springframework.stereotype.Service;

@Service(value="demoService")
public class DemoServiceImpl implements DemoService{

    public String sayHello(String name) {
        return "Hello Dubbo,Hello " + name;
    }

}

  applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd          
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >

    <context:component-scan base-package="com.**"></context:component-scan>

    <!-- 引入服务提供者配置文件 -->
    <import resource="dubbo-provider.xml" /> 
</beans>

  dubbo-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">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-provider"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
   <!--  <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
 
     <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.provider.DemoService" ref="demoService" />
 
    <!-- 和本地bean一样实现服务 -->
    <!-- 
    <bean id="demoService" class="com.provider.DemoServiceImpl" />
     -->
</beans>

  applicationContext-servlet.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd          
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd          
      http://www.springframewor.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    default-autowire="byName">

    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
             <bean class ="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name ="supportedMediaTypes">  
                     <list>  
                         <value>text/plain;charset=UTF-8</value> 
                     </list>  
                </property>  
             </bean>  
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 视图解释类 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
    </bean>

    <!-- 加载静态资源 -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/images/**" location="/images/" />
</beans>

四、创建调用服务(customer)

  customer服务架构和provider一致,拷贝一个即可

  相应的代码如下:

  applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd          
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >

    <context:component-scan base-package="com.**"></context:component-scan>

    <!-- 引入消费者配置文件 -->
    <import resource="dubbo-consumer.xml" /> 
</beans>

  dubbo-consumer.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="hello-world-customer"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.provider.DemoService" check="false"/>
</beans>

  applicationContext-servlet.xml 和provider中的代码一致,就不贴出来了,本项目中其实这个文件可以不用

  CustomerAction.java测试类  

package com.customer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.provider.DemoService;

@Controller
@RequestMapping(value="/customerTest")
public class CustomerAction {
    
    @Resource(name="demoService")
    private DemoService demoService;

    @RequestMapping(value="/test.do")
    public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
        System.out.println("成功");
        String result = demoService.sayHello("world");
        System.out.println(result);
        return null;
    }
    
}

五、共享服务

  注意:需要将服务提供的接口打成jar包,放入customer中

六、测试

  步骤:1、先启动zookeeper服务

     2、启动dubbo服务

     3、启动provider服务

     4、启动customer服务

  正常会出现如下界面

  

  错误总结:

  若按照上述测试步骤分别在不同的tomcat中启动服务,应该一切正常

  若2、3、4这三步放在同一个tomcat中启动,会出现如下错误。

  

看下关键异常:No provider available for the service

此异常是由于服务没有可以使用的提供者,就是说在zookeeper注册中心(zookeeper-url)中没有可供消费者调用的url,消费者访问提供者就失败了。

具体原因分析:由于dobbo在启动的时候会去检查各服务之间的依赖关系,由于启动的时候消费者没有检查到提供者提供的服务(此时可能提供者还没启动),所以报错

在消费者配置文件中,需要将<dubbo:registry address="zookeeper://127.0.0.1:2181"/>修改为<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

由于dubbo在注册的时候是默认会检查服务的依赖关系的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值