SpringBoot整合Dubbo+zookeeper(注册中心) 配置文件和注解方式

一,基于xml配置整合

准备好两个模块,一个product(生产者),一个customer(消费者)

1.导入Dubbo依赖

		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>

2.再product模块中配置Dubbo属性

<?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>

3.在customer模块配置

<?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>

4.使用注解@Reference注入

注入后,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

1.配置product模块application.yml

dubbo:
#服务器名称唯一
  application:
    name: product
#注册中心配置,使用zookeeper做注册中心
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
#    使用dubbo协议,端口默认20880
    port: 20880
    name: dubbo
  monitor:
    address: registry

注意yml文件格式

2.配置customer模块application.yml

#服务器名称唯一
  application:
    name: customer
#注册中心配置,使用zookeeper做注册中心
  registry:
    address: zookeeper://127.0.0.1:2181
#如果仅作为消费者,则不需要想生产者一样配置

3.两个模块都在启动类上开启Dubbo

@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("消费者启动成功");
	}
}

4.使用@Reference注入

@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);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值