一、服务提供者配置与测试
(1)在服务提供者中引入依赖
<!-- dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.8</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
(2)配置服务提供者
创建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="user-service-provider"></dubbo:application>
<!-- 指定注册中心的位置-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!-- 指定通信规则(通信协议、通信端口)-->
<dubbo:protocol name="dubbo" port="20080"></dubbo:protocol>
<!-- 暴露服务 ref指向服务真正实现-->
<dubbo:service interface="com.buba.service.UserService" ref="UserServiceImpl"></dubbo:service>
<bean id="UserServiceImpl" class="com.buba.service.impl.UserServiceImpl"></bean>
</beans>
(3)测试
编写测试代码:
package com.buba;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
context.start();
System.in.read(); // 按任意键退出
}
}
启动zookeeper服务,然后启动zookeeper客户端,最后执行测试代码。
登录监控中心查看:
二、服务消费者配置与测试
(1)引入依赖
<!-- dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.8</version>
</dependency>
<!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
(2)消费者配置文件
<?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="order-service-consumer"></dubbo:application>
<!-- 指定注册中心的位置-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!-- 声明需要调用远程服务的接口,生成远程服务代理-->
<dubbo:reference interface="com.buba.service.UserService" id="userService"></dubbo:reference>
<bean class="com.buba.service.impl.OrderServiceImpl" id="orderServiceImpl">
<property name="userService" ref="userService"></property>
</bean>
</beans>
(3)补全业务层的实现
package com.buba.service.impl;
import com.buba.pojo.UserAddress;
import com.buba.service.OrderService;
import com.buba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class OrderServiceImpl implements OrderService {
private UserService userService;
@Override
public void initOrder(String userId) {
List<UserAddress> userAddressList = userService.getUserAddressList("1");
userAddressList.forEach(System.out::println);
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
(4)编写测试类执行测试
package com.buba.controller;
import com.buba.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService bean = classPathXmlApplicationContext.getBean(OrderService.class);
bean.initOrder("1");
System.in.read();
}
}
在监控中心可以看到服务关系。