spring 整合dubbo
一、创建服务提供者 (创建多模块项目的目的是将服务的接口和实现分开)
1.1、在提供者的pom文件中添加依赖(我这里使用的是dubbo-2.5.10版本,版本不同可能依赖版本不同,这里有坑 需要注意)
<!--dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!--zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
1.2、在服务提供者的spring配置文件中增加dubbo的配置信息
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 组件扫描-->
<context:component-scan base-package="com.aliyun.dubbo"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<dubbo:application name="dubbo-demo"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<dubbo:service interface="com.demo.dubbo.cwl.api.DemoApi" ref="DemoServiceImp"></dubbo:service>
</beans>
1.3 、启动项目 若无报错则dubbo服务提供成功
二、创建服务消费者
创建项目
2.1、在消费者的pom文件中添加依赖(我这里使用的是dubbo-2.5.10版本,版本不同可能依赖版本不同,这里有坑 需要注意)
<!--dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!--zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
2.2、在服务消费者的spring配置文件中增加dubbo的配置信息
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 组件扫描-->
<context:component-scan base-package="com.aliyun.dubbo"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<dubbo:application name="dubbo-demo"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="true"></dubbo:registry>
<dubbo:protocol port="20880"></dubbo:protocol>
<dubbo:reference id ="questionApi" interface="com.demo.dubbo.cwl.api.DemoApi"></dubbo:reference>
</beans>
2.3、启动应用若无报错 则证明成功 调用接口验证
三、启动监控中心
监控中心 下载
dubbo-admin-2.5.10 然后放到tomcat的ROOT目录下,启动tomcat
这里默认的注册中心是zookeeper:127.0.0.1:2181
启动tomcat后可以看到服务的提供者和服务的消费者