springcloud中使用dubbo開發rpc服務及調用


spring cloud中基於springboot開發的微服務,是基於http的rest接口,也可以開發基於dubbo的rpc接口。

一,創建goodsService模塊

  1, 在創建的goodsService模塊中再創建goodsServiceApi和goodsServiceServer模塊

  2,在oodsServiceApi模塊中定義接口 ,goodsServiceServer用於接口實現

  3,goodsServiceServer模塊中pom文件引入相關依賴

<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編寫接口實現

@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服務會自動注冊到nacos服務發現中心

二,創建調用dubbo服務的模塊

  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調用dubbo服務

@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服務也會自動注冊到nacos(因為nacos.register.enable默認為true,即代表自動注冊,可以只訂閱,不注冊),對應接口返回了dubbo服務返回的信息!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM