2021-11-13 SpringBoot整合Dubbo的步骤

SpringBoot整合Dubbo的步骤

接口工程

这个不用多说了,在接口工程配置好实体bean和服务接口

提供者

  1. pom加入依赖:

    <!--dubbo集成jengSpringBoot起步依赖-->
    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    
    <!--注册中心-->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
    
    <!--接口工程-->
    <dependency>
        <groupId>com.mycode.springboot</groupId>
        <artifactId>springboot-dubbo-interface</artifactId>
        <version>1.0.0</version>
    </dependency>
    
  2. application.properties配置文件处,加入以下配置:

    # tomcat设置
    server.port=8081
    server.servlet.context-path=/
    
    # dubbo设置  协议方式  端口可以不去设置
    # 服务名称  唯一标识符
    spring.application.name=spring-boot-dubbo-provider
    # 声明这是一个提供者
    sping.dubbo.server=true
    # 设置注册中心
    spring.dubbo.registry=zookeeper://192.168.0.4:2181
    
  3. 在你的服务实现类上 加上注解:

    @Component  //加载到spring容器中
    @Service(interfaceClass = StudentService.class, version="1.0.0", timeout = 15000)
    public class StudentServiceImpl implements StudentService{
    	//your code
    }
    

    注意这个Service不是spring的Spring 而是com.alibaba.dubbo.config.annotation.Service

  4. 最后,在主函数入口上,开启Dubbo配置文件 不然无法识别你做的配置哦

    @EnableDubboConfiguration //dubbo配置开启
    
  5. 其实你上面的这些配置,就等同于在经典SSM下的dubboXml文件里的这些配置:

    	<!--名称-->
    	<dubbo:application name="spring-boot-dubbo-provider" />
    	
    	<!--注册中心-->
    	<dubbo:regisrt address:"zookeeper://192.168.0.4:2181" />
    	
    	<!--暴露服务-->
    	<dubbo:protocol name="dubbo" port="20880"/>
    	<bean id="studentServiceImpl" class="com.mycode.service.StudentServiceImpl"/>
        <dubbo:service 
        	interface="com.mycode.dubbo.service.StudentService" 
        	ref="studentServiceImpl" version="1.0.0" timeout="15000"/>
    

消费者

  1. 依赖:

    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.mycode.springboot</groupId>
        <artifactId>springboot-dubbo-interface</artifactId>
        <version>1.0.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    
  2. application.properties文件

    server.port=8080
    server.servlet.context-path=/
    
    spring.application.name=spring-boot-dubbo-consumer
    spring.dubbo.registry=zookeeper://192.168.0.4:2181
    
  3. 你需要RPC的Controller类,使用@Reference注解进行远程调用

    @Reference(interfaceClass = StudentService.class, version = "1.0.0", check = false)
    private StudentService service;
    
  4. 同样的,别忘了开启Dubbo配置文件

    @EnableDubboConfiguration //dubbo配置开启
    
  5. 经典SSM下的等同配置:

    <!--名称-->
    <dubbo:application name="spring-boot-dubbo-consumer" />
    
    <!--注册中心-->
    <dubbo:regisrt address:"zookeeper://192.168.0.4:2181" />
    
    <dubbo:reference
            interface="com.mycode.dubbo.service.StudentService"
            id="studentService"
            check="false"
            version="1.0.0"/>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值