创建一个工作空间DubboDemo
在项目下新建module
新建service接口并编写实现类
将service提取到另一module中
新建服务消费者module
和提供者类似将service接口提取到主module中
可以看到消费者里需要用到提供者的Service
其他的domain和service需要引入主module 的依赖
<dependency>
<groupId>xff</groupId>
<artifactId>themainservice</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
这个时候在服务提供者里添加依赖和配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xff</groupId>
<artifactId>user-service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency> <dependency> <groupId>xff</groupId>
<artifactId>themainservice</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency> <groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency> <dependency> <groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency> </dependencies>
</project>
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="userserviceprovider"></dubbo:application>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<dubbo:service interface="service.UserService" ref="userServiceImpl"></dubbo:service>
<bean id="userServiceImpl" class="service.impl.UserServiceImpl"></bean>
</beans>
这样就能将服务暴露给其他服务使用
再写一个类运行
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
类似的,在服务消费者里
依赖一样
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"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="service.impl"></context:component-scan>
<dubbo:application name="orderserviceconsumer"></dubbo:application>
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<dubbo:reference interface="service.UserService" id="userService"></dubbo:reference>
</beans>
写一个类测试
public static void main(String[] args) throws IOException {
ApplicationContext ioc= new ClassPathXmlApplicationContext("consumer.xml");
OrderService one= ioc.getBean(OrderService.class);
Order order=new Order();
order.setPrice(123);
System.out.println(one.findPrice(order));
System.in.read();
}
运行
这个时候打开上篇的OPS控制台可以看到服务提供者,打开在下面可以看到服务消费者正在使用