赞
踩
准备好两个模块,一个product(生产者),一个customer(消费者)
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<?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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 服务器名,要唯一--> <dubbo:application name="product"></dubbo:application> <!-- 定义注册中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--用dubbo协议在端口20880暴露服务--> <dubbo:protocol port="20880" name="dubbo"></dubbo:protocol> <!-- 按自己需求写出自己想要暴露给customer消费者的接口和实现类,按照下面格式自定义 声明暴露服务的接口,和导入使用的实现类--> <dubbo:service interface="com.lmh.api.facade.userFacade" ref="userService"></dubbo:service> <dubbo:service interface="com.lmh.api.facade.StudentFacade" ref="studentFacadeImpl"></dubbo:service> <dubbo:service interface="com.lmh.api.facade.OperationFacade" ref="operationLogFacadeImpl"></dubbo:service> <!-- 接口实现类--> <bean id="userService" name="userService" class="com.lmh.product.service.UserService"></bean> <bean id="studentFacadeImpl" name="studentFacadeImpl" class="com.lmh.product.facade.impl.StudentFacadeImpl"></bean> <bean id="operationLogFacadeImpl" name="operationLogFacadeImpl" class="com.lmh.product.facade.impl.OperationLogFacadeImpl"></bean> </beans>
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="customer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference interface="com.lmh.api.facade.userFacade" id="userFacade"></dubbo:reference>
<dubbo:reference interface="com.lmh.api.facade.StudentFacade" id="StudentFacade"></dubbo:reference>
<dubbo:reference interface="com.lmh.api.facade.OperationFacade" id="OperationFacade"></dubbo:reference>
</beans>
注入后,customer模块就可以使用product模块实现的方法
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private userFacade userService;
@operationLog(TargetName = "getUser",Kind = 1,Operation = "测试",module = ModuleType.operationalog)
@RequestMapping("/getName")
public void getUser(){
String user = userService.getUser("李敏镐");
System.out.println(user);
}
}
dubbo:
#服务器名称唯一
application:
name: product
#注册中心配置,使用zookeeper做注册中心
registry:
address: zookeeper://127.0.0.1:2181
protocol:
# 使用dubbo协议,端口默认20880
port: 20880
name: dubbo
monitor:
address: registry
注意yml文件格式
#服务器名称唯一
application:
name: customer
#注册中心配置,使用zookeeper做注册中心
registry:
address: zookeeper://127.0.0.1:2181
#如果仅作为消费者,则不需要想生产者一样配置
@SpringBootApplication
@EnableDubbo
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
System.out.println("生产者启动成功");
}
}
@SpringBootApplication
@EnableDubbo
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
System.out.println("消费者启动成功");
}
}
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private userFacade userService;
@operationLog(TargetName = "getUser",Kind = 1,Operation = "测试",module = ModuleType.operationalog)
@RequestMapping("/getName")
public void getUser(){
String user = userService.getUser("李敏镐");
System.out.println(user);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。