溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot2.0整合dubbo的示例代碼

發(fā)布時(shí)間:2020-09-27 10:19:55 來源:腳本之家 閱讀:178 作者:z七夜 欄目:編程語言

寫在前面:

使用springboot作為web框架,方便開發(fā)許多,做分布式開發(fā),dubbo又不可少,那么怎么整合在一起呢,

跟我學(xué)一遍,至少會(huì)用

注意,springboot2.0和springboot1.x與dubbo整合不一樣,

1.環(huán)境

1.新建一個(gè)空的maven項(xiàng)目,作為父工程,新建moudle,,service(接口層,及實(shí)現(xiàn)層,沒有具體分,),web(web層,springboot項(xiàng)目)

項(xiàng)目結(jié)構(gòu)如下

springboot2.0整合dubbo的示例代碼

父pom如下

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>


      <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.0.3.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--如果要把springboot工程打包成war執(zhí)行,需要該jar-->
      <!--<dependency>-->
      <!--<groupId>org.springframework.boot</groupId>-->
      <!--<artifactId>spring-boot-legacy</artifactId>-->
      <!--<version>1.0.2.RELEASE</version>-->
      <!--</dependency>-->

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

      <!--引入zookeeper的客戶端工具-->
      <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
      <dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

web層pom

 <dependencies>
    <dependency>
      <groupId>com.itzmn</groupId>
      <artifactId>dubbo-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <!--引入zookeeper的客戶端工具-->
    <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

service層

 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <!--引入zookeeper的客戶端工具-->
    <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
    </dependency>
  </dependencies>

2.接口設(shè)計(jì)

springboot2.0整合dubbo的示例代碼

在service模塊新建接口,

springboot2.0整合dubbo的示例代碼

接口實(shí)現(xiàn)類的注解,service一定是dubbo的注解

3.配置文件

# Spring boot application
spring.application.name = /springboot-dubbo
server.port = 9099
management.port = 9091

# Service version
demo.service.version = 1.0.0

# Base packages to scan Dubbo Components (e.g @Service , @Reference)
dubbo.scan.basePackages = com.itzmn.dubbo.service.impl

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = springboot-dubbo
dubbo.application.name = springboot-dubbo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
dubbo.registry.id = my-registry1
dubbo.registry.address = zookeeper://47.106.64.158:2181

在web層的配置文件中,配置,即可,前提,要先安裝zookeeper,才能進(jìn)行服務(wù)的注冊(cè),然后啟動(dòng)即可

4.消費(fèi)者

配置

# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080
management.port = 8081

# Service Version
demo.service.version = 1.0.0

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

只需將服務(wù)提供者的接口jar包引入,然后注入服務(wù)即可

注意,springboot2.0和springboot1.x與dubbo整合不一樣,

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI