How spring cloud uses Dubbo to develop RPC services and calls

Time:2020-3-28

This article mainly introduces how spring cloud uses Dubbo to develop RPC services and calls. The example code is introduced in detail in this article, which has a certain reference learning value for everyone’s study or work. You can refer to the following for friends who need it

The microservices developed in spring cloud based on springboot are rest interfaces based on HTTP and RPC interfaces based on Dubbo.

1、 Create goodsservice module

1. In the created goodsservice module, create goodsserviceapi and goodsserviceserver modules

2. Define the interface in oodserviceapi module, and goodsserviceserver is used for interface implementation

3. The POM file in the goodsserviceserver module introduces related dependencies


<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. Add configuration to 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. Interface implementation of goodsserviceserver

@org.apache.dubbo.config.annotation.Service
public class GoodsImpl implements GoodsApi {
  public String getGoodsName() {
    Return "item 1";
  }
}

6. The goodsserviceserver writes the startup class


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

After startup, Dubbo service will automatically register to Nacos Service Discovery Center

2、 Create a module that calls the Dubbo service

1. New – > module – > fill in information – > finish

2. Add POM dependency


<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. Add configuration


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

4. Write controller to call Dubbo service


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

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

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

5. Write startup class


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

After startup, the demo Dubbo service will also automatically register to Nacos (because nacos.register.enable is true by default, that is, it represents automatic registration. You can only subscribe, but not register). The corresponding interface returns the information returned by Dubbo service!

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.