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在注册的时候是默认会检查服务的依赖关系的

dubbo + zookeeper 配置的更多相关文章

  1. dubbo+zookeeper集群配置

    集群服务注册到多台zookeeper配置: <dubbo:registry protocol="zookeeper" address="10.20.153.10:2 ...

  2. Dubbo + Zookeeper 简单配置

    Dubbo + Zookeeper Zookeeper 下载及配置 下载到本机/usr/local目录 wget https://mirrors.tuna.tsinghua.edu.cn/apache ...

  3. windows下 zookeeper dubbo 安装+配置+demo 详细图文教程

    Java集群优化——dubbo+zookeeper构建 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这 ...

  4. 基于CentOS6.5的Dubbo及Zookeeper配置

    基于CentOS的Dubbo及Zookeeper配置 需要提前准备好的资料: 1.首先配置java环境 步骤: 将jdk的包上传至centos服务器的/opt目录下,并且解压 tar -zxvf jd ...

  5. 从头开始搭建一个dubbo+zookeeper平台

    本篇主要是来分享从头开始搭建一个dubbo+zookeeper平台的过程,其中会简要介绍下dubbo服务的作用. 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后 ...

  6. 通过单元测试理解spring容器以及dubbo+zookeeper单元测试异常处理

    一.先说一个结论:单元测试与主项目的spring容器是隔离的,也就是说,单元测试无法访问主项目spring容器,需要自己加载spring容器. 接下来是代码实例,WEB主项目出于运行状态,单元测试中可 ...

  7. 用dubbo+zookeeper+spring搭建一个简单的http接口程序

    dubbo是一个分布式服务框架,是阿里巴巴开发的一个解决RPC远程调用优化的核心框架,包含负载均衡算法,能提高分布式系统的性能. zookeeper是hadoop的一个子项目,主要用来解决分布式系统的 ...

  8. DUBBO安装配置注意事项

    DUBBO安装配置注意事项 参考URL:http://blog.csdn.net/lichunan/article/details/40349645 ====== 管理端: 记得更改TOMCAT的端口 ...

  9. Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!   “看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然.所以,无论什么事情,仔细想一 ...

随机推荐

  1. [Xcode 实际操作]四、常用控件-(6)UISwitch开关控件的使用

    目录:[Swift]Xcode实际操作 本文将演示开关控件的基本用法. 开关控件有两个互斥的选项,它是用来打开或关闭选项的控件. 在项目导航区,打开视图控制器的代码文件[ViewController. ...

  2. android手机设备查看/data/data

    打开cmd 进入安卓 SDK的'Platform tools'   cd F:\software\adt-bundle-windows-x86_64-20130522\sdk\platform-too ...

  3. 洛谷 P1547 Out of Hay (最小生成树)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P1547 思路: 嗯...既然题中已经说了是最小生成树,那么是需要在最小生成树的模板上稍作修改即可.要 ...

  4. 将vue和element-ui写在一个html里面方便调试(小白篇)

    声明:纯属小白进门文档 vue的官方文档: https://vuejs.bootcss.com/v2/guide/ 第一步:引入vue.js <script src="https:// ...

  5. BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树

    既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...

  6. HDU6438:Buy and Resell(贪心+数据结构)

    题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少 分析:对每一个元素产生的贡献可以先计算 ...

  7. Windows开机自动登录及取消自动登录的设置

    开机自动登录 1.开始菜单搜索框输入 “netplwiz” 按回车 或“Win+R”组合键打开“运行”框内输入“netplwiz” 或“运行”框内输入“control userpasswords2”( ...

  8. 【ACM】一种排序

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数 ...

  9. java中过滤器、监听器、拦截器的区别

    1.过滤器:所谓过滤器顾名思义是用来过滤的,在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的actio ...

  10. NopI 导出数据

    protected void exportAward(DataSet dsResult) { if (dsResult != null) { string fileName = System.Web. ...