温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Spring Boot如何实现对Dubbo框架进行集成

发布时间:2020-11-19 16:16:51 来源:亿速云 阅读:160 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用Spring Boot如何实现对Dubbo框架进行集成,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1.创建工程在pom.xml中加入依赖

创建工程名为:

(1)springboot-dubbo-provide

(2)springboot-dubbo-api

(3)springboot-dubbo-consume

springboot-dubbo-api工程主要是放一些service接口,用于提供给消费者使用 。springboot-dubbo-provide工程用于提供服务。  springboot-dubbo-consume工程为消费者。在springboot-dubbo-provide工程中打开pom.xml加入以下依赖,完整代码如下:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.chengli</groupId> 
  <artifactId>springboot-dubbo-provide</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>jar</packaging> 
  <name>springboot-dubbo-provide</name> 
  <url>http://maven.apache.org</url> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.3.RELEASE</version> 
  </parent> 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
    <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> 
    <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version> 
    <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> 
  </properties> 
  <dependencies> 
    <dependency> 
      <groupId>com.chengli</groupId> 
      <artifactId>springboot-dubbo-api</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> 
      <exclusions> 
        <exclusion> 
          <groupId>org.springframework</groupId> 
          <artifactId>spring</artifactId> 
        </exclusion> 
      </exclusions> 
      <version>${com.alibaba.dubbo.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.zookeeper</groupId> 
      <artifactId>zookeeper</artifactId> 
      <version>${org.apache.zookeeper.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>com.github.sgroschupf</groupId> 
      <artifactId>zkclient</artifactId> 
      <version>${com.github.sgroschupf.zkclient.version}</version> 
    </dependency> 
  </dependencies> 
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build> 
</project> 

打开springboot-dubbo-consume工程,在pom.xml中加入以下依赖,完整代码如下:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.chengli</groupId> 
  <artifactId>springboot-dubbo-consume</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>jar</packaging> 
  <name>springboot-dubbo-consume</name> 
  <url>http://maven.apache.org</url> 
  <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.3.RELEASE</version> 
  </parent> 
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
    <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> 
    <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version> 
    <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> 
  </properties> 
  <dependencies> 
    <dependency> 
      <groupId>com.chengli</groupId> 
      <artifactId>springboot-dubbo-api</artifactId> 
      <version>0.0.1-SNAPSHOT</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <!-- dubbo --> 
    <dependency> 
      <groupId>com.alibaba</groupId> 
      <artifactId>dubbo</artifactId> 
      <exclusions> 
        <exclusion> 
          <groupId>org.springframework</groupId> 
          <artifactId>spring</artifactId> 
        </exclusion> 
      </exclusions> 
      <version>${com.alibaba.dubbo.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.apache.zookeeper</groupId> 
      <artifactId>zookeeper</artifactId> 
      <version>${org.apache.zookeeper.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>com.github.sgroschupf</groupId> 
      <artifactId>zkclient</artifactId> 
      <version>${com.github.sgroschupf.zkclient.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-configuration-processor</artifactId> 
      <optional>true</optional> 
    </dependency> 
  </dependencies> 
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
    </plugins> 
  </build> 
</project> 

2.Dubbo配置

2.1springboot-dubbo-provide服务提供者

(1)在springboot-dubbo-provide项目中创建入口启动类MainConfig,完整代码如下:

package com.chengli.springboot; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
public class MainConfig { 
  public static void main(String[] args) { 
    SpringApplication.run(MainConfig.class, args); 
    try { 
      System.in.read(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
} 

(2)创建Dubbo配置类

package com.chengli.springboot.dubbo; 
 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ImportResource; 
import org.springframework.context.annotation.PropertySource; 
 
@Configuration 
@PropertySource("classpath:dubbo/dubbo.properties") 
@ImportResource({ "classpath:dubbo/*.xml" }) 
public class DubboConfig { 
 
} 

(3)创建Dubbo配置文件

在src/main/resources下新建文件夹dubbo,并加入以下配置:

dubbo-provider.xml内容如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;> 
<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.xsd 
  http://code.alibabatech.com/schema/dubbo 
  http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
 
  <!-- 提供方应用信息,用于计算依赖关系 --> 
  <dubbo:application name="${dubbo.application.name}" /> 
 
  <!-- 注册中心暴露服务地址 --> 
  <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 
   
  <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> --> 
  <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> 
 
  <!-- 暴露服务 --> 
  <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" /> 
   
  <dubbo:service interface="com.chengli.springboot.example.service.ExampleService" 
    ref="exampleServiceImpl" retries="0" timeout="6000" /> 
</beans> 

注意:这里我发布的example服务是示例,具体的根据实际修改

(4)创建dubbo.properties

#应用名称 
dubbo.application.name=example-provider 
#注册中心类型 
dubbo.registry.protocol=zookeeper 
#注册中心地址 
dubbo.registry.address=127.0.0.1:2181 
#暴露服务方式 
dubbo.protocol.name=dubbo 
#暴露服务端口 
dubbo.protocol.port=20880 

2.2springboot-dubbo-consume服务消费者

(1)创建入口启动类MainConfig

package com.chengli.springboot; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
public class MainConfig { 
  public static void main(String[] args) { 
    SpringApplication.run(MainConfig.class, args); 
  } 
} 

(2)创建Dubbo配置类

package com.chengli.springboot.dubbo; 
 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.ImportResource; 
import org.springframework.context.annotation.PropertySource; 
 
@Configuration 
@PropertySource("classpath:dubbo/dubbo.properties") 
@ImportResource({ "classpath:dubbo/*.xml" }) 
public class DubboConfig { 
 
} 

(3)创建Dubbo配置文件

在src/main/resources下新建文件夹dubbo,并加入以下配置:

dubbo-consume.xml内容如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;> 
<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.xsd 
  http://code.alibabatech.com/schema/dubbo 
  http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
 
  <!-- 提供方应用信息,用于计算依赖关系 --> 
  <dubbo:application name="${dubbo.application.name}" /> 
 
  <!-- 注册中心暴露服务地址 --> 
  <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> 
   
  <dubbo:reference id="axempleService" interface="com.chengli.springboot.example.service.ExampleService" /> 
</beans> 

(4)创建dubbo.properties

#应用名称 
dubbo.application.name=example-consume 
#注册中心类型 
dubbo.registry.protocol=zookeeper 
#注册中心地址 
dubbo.registry.address=127.0.0.1:2181 

到这里基本上就已经可以了,不过测试类的代码我就不贴上来了。只要在API中定义接口实现即可。使用Spring Boot 与Dubbo集成的时候,需要注意的是,不要使用Spring Boot提供的devtools热启动,因为devtools提供了两个ClassLoader,加载策略问题导致出现错误,无法启动。如果开发中需要热加载,那么使用Spring 提供的springloaded。

上述就是小编为大家分享的使用Spring Boot如何实现对Dubbo框架进行集成了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI