作为dubbo框架初学者,能让框架跑起来非常不容易,非常感谢网上诸多大神提供的文章,本人参考文章地址是:https://my.oschina.net/xshuai/blog/891281

不过别人的记录终究不适合自己,所以还是按照自己的风格简单记录下学习dubbo整合的步骤。

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,下载地址:http://pan.baidu.com/s/1eSnuqEQ

  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、SpringMVC整合和使用的更多相关文章

  1. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  2. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  3. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  4. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)转

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  5. 【转载】Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及 ...

  6. Dubbo、Zookeeper、SpringMVC的整合使用

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  7. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  8. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  9. Dubbo与Zookeeper、SpringMVC整合和利用(负载均衡、容错)

    互联网发展,扩大了网站应用程序的大小.传统的垂直应用架构已经无法应付.分布式服务架构和流量计算架构势在必行,Dubbo是一个分布式服务框架.在这样的情况下诞生的.如今核心业务抽取出来.作为独立的服务, ...

  10. 六:Dubbo与Zookeeper、SpringMvc整合和使用

    DUBBO与ZOOKEEPER.SPRINGMVC整合和使用 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架 ...

随机推荐

  1. Spring集成RabbitMQ-必须知道的几个概念

    上篇<Spring集成RabbiMQ-Spring AMQP新特性>我们了解了最新spring-rabbit的2.0.0.M5版本相较于之前有哪些变化.其实使用Spring-amqp确实简 ...

  2. 【渗透课程】第三篇-体验http协议的应用

    之前我们都了解了,访问网页时,客户端与服务端之间的请求与响应数据交互.本篇就浅谈它的应用. 利用HTTP拦截突破前段验证 比方说,我们在某个网页提交某些数据(例如留言.上传.插入xss等),发生错误( ...

  3. Android studio一些常见技巧(不断更新)

    一.Android studio取消默认每次打开时打开最后一个项目 二.as添加jar包 新建一个libs目录,在java下 进行手动gragle同步或者如下图 三.代码边上的小图标 1.布局文件中存 ...

  4. Hue集成Hadoop和Hive

    一.环境准备 1.下载Hue:https://dl.dropboxusercontent.com/u/730827/hue/releases/3.12.0/hue-3.12.0.tgz 2.安装依赖 ...

  5. TCP/IP协议栈模型

    OSI七层模型介绍: 下面4层(物理层.数据链路层.网络层和传输层)主要提供数据传输和交换功能,即以节点到节点之间的通信为主:第4层作为上下两部分的桥梁,是整个网络体系结构中最关键的部分:而上3层(会 ...

  6. 使用spring框架处理编码问题

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp90   我们在开发时,经常要对中文字符进行处理,进行处理中文字符的方式也 ...

  7. 求N个元素的子集合个数

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt406 一个集合有n个元素,请问怎么算出来它的子集(包括空集和本身)是 2的n ...

  8. ROS学习记录(一)————创建简单的机器人模型smartcar

    这是我在古月居上找的(http://www.guyuehome.com/243),但直接运行的话,没办法跑起来,我也是查了好多博客和日志,才实现最后的功能的,所以,记录下来,以备后用吧,也欢迎其他和我 ...

  9. JavaScript学习日志(五):DOM

    一,基本定义 DOM是针对HTML和XML文档的API,根据W3C的HTML DOM标准,html文档中所以内容(无论是元素还是标签还是注释还是元素属性)都是节点. 二,Node类型:每一个节点都含有 ...

  10. 201521123084 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自 ...