1. 引入dubbo依赖
dubbo 依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency>
zookeeper 依赖 客户端
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
2. 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"> <!--1. 指定当前服务的名字--> <dubbo:application name="Dubbo-server"/> <!--2. 指定注册中心的位置--> <dubbo:registry protocol="zookeeper" address="124.0.0.1:2181"></dubbo:registry> <!--3. 指定通信规则(通信协议?通信端口)--> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!--4. 暴露服务,指向服务的真正实现对象--> <dubbo:service interface="com.aynu.service.UserService" ref="userServiceImpl"></dubbo:service> <!--服务的实现--> <bean id="userServiceImpl" class="com.aynu.service.UserServiceImpl"></bean>
<!--6.配置当前消费者的统一规则:所有的服务都不检查 -->
<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>
<!-- 5. 连接监控中心,去注册中心找-->
<dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
3. 启动类
package com.aynu; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApplication { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); } }