当前位置:   article > 正文

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

dubbo yml配置文件详解

一,基于xml配置整合

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

1.导入Dubbo依赖

		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

二,基于注解整合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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注意yml文件格式

2.配置customer模块application.yml

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

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

@SpringBootApplication
@EnableDubbo
public class CustomerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
		System.out.println("生产者启动成功");
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
@SpringBootApplication
@EnableDubbo
public class CustomerApplication {

	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
		System.out.println("消费者启动成功");
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/284102
推荐阅读
相关标签
  

闽ICP备14008679号