导包:
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
首先在公共模块创建zk.properties
配置文件
zk.address=127.0.0.1:2181
与dubbo.properties
配置文件
dubbo.port=20881
dubbo.application=order
然后在 service
层创建applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--===== 配置 dubbo 服務 =====-->
<!--name指定协议为 dubbo,指定暴露服务的端口,如果不指定默认为20880-->
<dubbo:protocol name="dubbo" port="${dubbo.port}"/>
<!-- dubbo 服务的唯一名称 -->
<dubbo:application name="${dubbo.application}" />
<!-- 指定服务的注册中心 -->
<dubbo:registry protocol="zookeeper" address="${zk.address}" />
<dubbo:annotation package="cn.qingcheng.service" />
<!--<context:annotation-config/>-->
<dubbo:provider timeout="10000" threadpool="fixed" threads="100" accepts="1000"/>
</beans>
然后接着在web
层创建消费者applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--=== 引用dubbo 服务 ====-->
<dubbo:application name="${dubbo.application}" />
<dubbo:registry protocol="zookeeper" address="${zk.address}"/>
<dubbo:annotation package="cn.qingcheng.controller" />
<!-- check="false" 关闭启动时检测依赖服务是否可用 ,开发环境改为false,生产环境改为true(默认)-->
<dubbo:consumer timeout="8000" retries="0" check="false"/>
</beans>
网友评论