溫馨提示×

溫馨提示×

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

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

springcloud如何使用dubbo開發(fā)rpc服務(wù)及調(diào)用

發(fā)布時間:2020-10-21 15:54:03 來源:腳本之家 閱讀:157 作者:王東波 欄目:編程語言

這篇文章主要介紹了springcloud如何使用dubbo開發(fā)rpc服務(wù)及調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

spring cloud中基于springboot開發(fā)的微服務(wù),是基于http的rest接口,也可以開發(fā)基于dubbo的rpc接口。

一,創(chuàng)建goodsService模塊

  1, 在創(chuàng)建的goodsService模塊中再創(chuàng)建goodsServiceApi和goodsServiceServer模塊

  2,在oodsServiceApi模塊中定義接口 ,goodsServiceServer用于接口實現(xiàn)

  3,goodsServiceServer模塊中pom文件引入相關(guān)依賴

<dependencies>
    <dependency>
      <groupId>net.biui</groupId>
      <artifactId>goods-service-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
  </dependencies>

4,goodsServiceServer中添加配置

spring:
 application:
  name: goods-service
 cloud:
  nacos:
   discovery:
    server-addr: 127.0.0.1:8848
    namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501

dubbo:
 registry:
  address: nacos://127.0.0.1:8848
 scan:
  base-packages: net.biui.impl
 protocol:
  port: 20881
  name: dubbo

5,goodsServiceServer編寫接口實現(xiàn)

@org.apache.dubbo.config.annotation.Service
public class GoodsImpl implements GoodsApi {
  public String getGoodsName() {
    return "商品一";
  }
}

6,goodsServiceServer編寫啟動類

@SpringBootApplication
@EnableDiscoveryClient
public class GoodsServiceServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(GoodsServiceServerApplication.class, args);
  }
}

啟動后,dubbo服務(wù)會自動注冊到nacos服務(wù)發(fā)現(xiàn)中心

二,創(chuàng)建調(diào)用dubbo服務(wù)的模塊

  1,new -> module -> 填寫信息 -> finish

  2,添加pom依賴

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
    <dependency>
      <groupId>net.biui</groupId>
      <artifactId>goods-service-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

3,添加配置

spring:
 application:
  name: demo-dubbo
 cloud:
  nacos:
   discovery:
    server-addr: 127.0.0.1:8848
    namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501

4,編寫controller調(diào)用dubbo服務(wù)

@RestController
@RequestMapping("/demo")
public class demoController {

  @org.apache.dubbo.config.annotation.Reference
  GoodsApi goodsApi;

  @GetMapping("/test")
  public String test(){
    return "test " + goodsApi.getGoodsName();
  }
}

5,編寫啟動類

@SpringBootApplication
@EnableDiscoveryClient
public class demoDubboApplication {
  public static void main(String[] args) {
    SpringApplication.run(demoDubboApplication.class, args);
  }
}

啟動后,demo-dubbo服務(wù)也會自動注冊到nacos(因為nacos.register.enable默認為true,即代表自動注冊,可以只訂閱,不注冊),對應(yīng)接口返回了dubbo服務(wù)返回的信息!

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

向AI問一下細節(jié)

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

AI