欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

程序员文章站 2022-06-05 17:35:07
场景 Dubbo环境搭建-管理控制台dubbo-admin实现服务监控: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103624846 Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载) ......

场景

dubbo环境搭建-管理控制台dubbo-admin实现服务监控:

https://blog.csdn.net/badao_liumang_qizhi/article/details/103624846

dubbo搭建helloworld-搭建服务提供者与服务消费者并完成远程调用(附代码下载):

https://blog.csdn.net/badao_liumang_qizhi/article/details/103675259

在上面搭建好dubbo的helloworld后,将其与springboot项目进行整合。

注:

博客:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

还是采用上面用户服务提供者和订单服务消费者的模式。

将公共的接口和实体类抽离出来,放在gmall-interface中。

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

新建服务提供者

打开eclipse-新建一个spring starter project

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

点击next,输入相关包名与应用名

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

点击next,这里新建的是服务提供者,只需要简单的springboot项目,不用选择web依赖,直接点击next,

注意这里的springboot的版本为2.2.2,在后面选择dubbo-starter依赖时有版本对应关系。

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

建完之后的目录为

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

然后打开pom.xml,添加上面公共接口的依赖以及dubbo-starter的依赖。

引入公共接口依赖,具体实现参照上面博客

  <dependency>
   <groupid>com.badao.gmall</groupid>
   <artifactid>gmall-interface</artifactid>
   <version>0.0.1-snapshot</version>
  </dependency>

 

引入dubbo-starter以及相关依赖

按照其官方指示:

添加相应的dubbo的依赖和dubbo-starter的依赖

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

完整pom.xml代码

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>2.2.2.release</version>
  <relativepath/> <!-- lookup parent from repository -->
 </parent>
 <groupid>com.badao</groupid>
 <artifactid>boot-user-service-provider</artifactid>
 <version>0.0.1-snapshot</version>
 <name>boot-user-service-provider</name>
 <description>demo project for spring boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupid>com.badao.gmall</groupid>
   <artifactid>gmall-interface</artifactid>
   <version>0.0.1-snapshot</version>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter</artifactid>
  </dependency>
  <!-- 引入dubbo -->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>dubbo</artifactid>
   <version>2.6.2</version>
  </dependency>
  <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
  <dependency>
   <groupid>org.apache.curator</groupid>
   <artifactid>curator-framework</artifactid>
   <version>2.12.0</version>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
   <exclusions>
    <exclusion>
     <groupid>org.junit.vintage</groupid>
     <artifactid>junit-vintage-engine</artifactid>
    </exclusion>
   </exclusions>
  </dependency>
  <!-- dubbo spring boot starter -->
     <dependency>
         <groupid>com.alibaba.boot</groupid>
         <artifactid>dubbo-spring-boot-starter</artifactid>
         <version>0.2.1.release</version>
     </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>

</project>

 

参考上面博客搭建服务提供者时配置信息是在provider.xml中进行配置,在这里要将其配置在application.properties中。

 

dubbo.application.name=user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#
dubbo.monitor.protocol=registry
dubbo.scan.base-packages=com.badao.gmall

 

具体配置信息作用见上面博客。或者参照其官方指示

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

然后将上面博客搭建好的serviceimpl复制到springboot项目中

package com.badao.gmall.service.impl;

import java.util.arrays;
import java.util.list;

import org.springframework.stereotype.component;

import com.alibaba.dubbo.config.annotation.service;
import com.badao.gmall.bean.useraddress;
import com.badao.gmall.service.userservice;

@service  //暴露服务
@component
public class userserviceimpl implements userservice {

 public list<useraddress> getuseraddresslist(string userid) {
  
  // todo auto-generated method stub
  useraddress address1 = new useraddress(1, "霸道流氓气质", "1", "李老师", "123456789", "y");
  useraddress address2 = new useraddress(2, "公众号:霸道的程序猿)", "1", "王老师", "987654321", "n");
  return arrays.aslist(address1,address2);
 }

}

 

注意的是之前在上面搭建的spring项目中使用的@service注解是spring的注解,而这里使用的dubbo的注解

import com.alibaba.dubbo.config.annotation.service;

 

为了区分spring的@service注解,所以使用spring的@componment注解。

dubbo的@service注解的作用是指定要暴露的服务,让别人能引用。其作用就是上面在provider.xml中使用dubbo-service标签

暴露服务一样

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

然后在springboot的主程序中添加注解@enabledubbo时支持dubbo

package com.badao.gmall;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

import com.alibaba.dubbo.config.spring.context.annotation.enabledubbo;

@enabledubbo   //开启基于注解的dubbo功能
@springbootapplication
public class bootuserserviceproviderapplication {

 public static void main(string[] args) {
  springapplication.run(bootuserserviceproviderapplication.class, args);
 }

}

 

启动提供者主程序

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

使用上面博客中搭建好的管理平台可见服务提供者搭建成功

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

新建服务消费者

参照上面搭建服务提供者的流程,新建服务消费者

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

此时添加web依赖

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

此时的pom.xml同样引入公共接口依赖与dubbo和dubbo-starter相关依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>2.2.2.release</version>
  <relativepath/> <!-- lookup parent from repository -->
 </parent>
 <groupid>com.badao</groupid>
 <artifactid>boot-order-service-consumer</artifactid>
 <version>0.0.1-snapshot</version>
 <name>boot-order-service-consumer</name>
 <description>demo project for spring boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
  <dependency>
   <groupid>com.badao.gmall</groupid>
   <artifactid>gmall-interface</artifactid>
   <version>0.0.1-snapshot</version>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
   <exclusions>
    <exclusion>
     <groupid>org.junit.vintage</groupid>
     <artifactid>junit-vintage-engine</artifactid>
    </exclusion>
   </exclusions>
  </dependency>
  <!-- 引入dubbo -->
  <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>dubbo</artifactid>
   <version>2.6.2</version>
  </dependency>
  <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端 -->
  <dependency>
   <groupid>org.apache.curator</groupid>
   <artifactid>curator-framework</artifactid>
   <version>2.12.0</version>
  </dependency>
  <!-- dubbo spring boot starter -->
     <dependency>
         <groupid>com.alibaba.boot</groupid>
         <artifactid>dubbo-spring-boot-starter</artifactid>
         <version>0.2.1.release</version>
     </dependency>
     
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>

</project>

 

 

然后打开其配置文件进行相关配置

 

server.port=8081

dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

 

 

注意:这里修改端口号是因为8080已经被dubbo-monitor所占用,

具体配置的作用参照上面博客。

然后将服务消费者的接口实现复制过来

package com.badao.gmall.service.impl;

import java.util.list;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;

import com.alibaba.dubbo.config.annotation.reference;
import com.badao.gmall.bean.useraddress;
import com.badao.gmall.service.orderservice;
import com.badao.gmall.service.userservice;

/**
 * 1、将服务提供者注册到注册中心(暴露服务)
 *   1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
 *   2)、配置服务提供者
 * 
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 * @author badao
 *
 */
@service
public class orderserviceimpl implements orderservice {

 //@autowired
 @reference
 userservice userservice;
 public list<useraddress> initorder(string userid) {
  // todo auto-generated method stub
  system.out.println("用户id:"+userid);
  //1、查询用户的收货地址
  list<useraddress> addresslist = userservice.getuseraddresslist(userid);
  for (useraddress useraddress : addresslist) {
   system.out.println(useraddress.getuseraddress());
  }
  return addresslist;
 }
 
 

}

 

注意:这里的自动注入的@autowired注解要修改为dubbo的@reference,其作用是能远程引用userservice的服务,自己能从服务注册中心发现。

其作用相当于之前的consumer.xml中使用dubbo:reference标签声明需要调用的远程服务接口,即生成远程服务代理。

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

在包下新建controller包以及ordercontroller类

package com.badao.gmall.controller;

import java.util.list;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;
import com.badao.gmall.bean.useraddress;
import com.badao.gmall.service.orderservice;


@controller
public class ordercontroller {
 
 @autowired
 orderservice orderservice;
 
 @responsebody
 @requestmapping("/initorder")
 public list<useraddress> initorder(@requestparam("uid")string userid) {
  return orderservice.initorder(userid);
 }
 

}

 

然后修改主程序开启dubbo支持

package com.badao.gmall;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

import com.alibaba.dubbo.config.spring.context.annotation.enabledubbo;

@enabledubbo
@springbootapplication
public class bootorderserviceconsumerapplication {

 public static void main(string[] args) {
  springapplication.run(bootorderserviceconsumerapplication.class, args);
 }

}

 

启动应用主程序

此时在管理平台就会监控到服务消费者

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

然后打开浏览器,输入:localhost:8081/initorder?uid=1

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

 

 

Dubbo与SpringBoot整合流程(从实例入手,附代码下载)

示例代码下载

https://download.csdn.net/download/badao_liumang_qizhi/12052055