Basic course of spring cloud Alibaba: perfect integration with Dubbo
Program description DD 2021-06-25 02:34:28

Long ago , In the beginning Spring Cloud In the basic tutorial , I wrote such an article :《 The choice of infrastructure for microservice Architecture :Spring Cloud still Dubbo?》, Maybe many readers have seen it . Since then, there have been questions about how to choose these two frameworks , In fact, I explicitly mentioned ,Spring Cloud And Dubbo The comparison itself is unfair , Mainly, the former is a relatively complete architecture scheme , and Dubbo Just service governance and RPC Implementation scheme .

because Dubbo There is a very large user group in China , But its surrounding facilities and components are not so perfect . Many developers want to enjoy Spring Cloud The ecology of , So there will be some Spring Cloud And Dubbo Cases and methods used together appear , But most of the time Spring Cloud Integrate Dubbo It's not easy to use . This is mainly due to Dubbod The registry of ZooKeeper, At the beginning Spring Cloud The registry in the system does not support ZooKeeper, So a lot of schemes have two different registries , After that, even if Spring Cloud Support ZooKeeper, But because the granularity and storage of service information are not consistent . therefore , For a long time , On the level of service governance , Both of them have always been a perfect combination .

until Spring Cloud Alibaba Appearance , To solve such a problem . In the previous tutorial , We have introduced the use of Spring Cloud Alibaba Medium Nacos As a service registry , And under this can be as traditional Spring Cloud The application uses the same Ribbon or Feign To achieve service consumption . This article , Let's continue Spring Cloud Alibaba Under additional support RPC programme :Dubbo.

Introductory cases

Let's start with a simple example , To feel it intuitively Nacos Under service registry , utilize Dubbo To achieve service provider and service consumer . It is omitted here Nacos Installation and use of , If the Nacos I don't know yet , You can view the Use Nacos Realize service registration and discovery , Let's go straight down Dubbo How to use .

Build service interface

Create a simple Java project , And define an abstract interface below , such as :

public interface HelloService {

String hello(String name);

}

Build service interface provider

First step : Create a Spring Boot project , stay pom.xml Introduced in step 1 API Packages and Spring Cloud Alibaba Yes Nacos and Dubbo Dependence , such as :

<dependencies>
<!-- Built in step 1 API package -->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>alibaba-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--<groupId>com.alibaba.cloud</groupId>-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

//...
</dependencies>

Two points need to be noted here :

  1. Must contain spring-boot-starter-actuator package , Otherwise, the startup will report an error .
  2. spring-cloud-starter-dubbo Package needs attention groupId, According to the specific use spring cloud alibaba Version dependency to determine .
    • Project incubation period , The use of groupId by :org.springframework.cloud;
    • After project incubation , The use of groupId Revised to com.alibaba.cloud, So users need to pay attention to whether they use it correctly . Avoid no corresponding loading JAR Package problem .

The second step : Realization Dubbo Interface

@Service
public class HelloServiceImpl implements HelloService {

@Override
public String hello(String name) {
return "hello " + name;
}

}

Be careful : there @Service Annotations are not Spring Of , It is org.apache.dubbo.config.annotation.Service annotation .

The third step : To configure Dubbo Service related information , such as :

spring.application.name=alibaba-dubbo-server
server.port=8001

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

# Appoint Dubbo Scan benchmark package of service implementation class
dubbo.scan.base-packages=com.didispace.alibaba.dubbo.server
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost

The configuration description is as follows :

  • dubbo.scan.base-packages: Appoint Dubbo Scan benchmark package of service implementation class
  • dubbo.protocol: Dubbo Protocol configuration of service exposure , Sub attributes name Is the name of the agreement ,port Is the protocol port ( -1 Indicates auto increment port , from 20880 Start )
  • dubbo.registry: Dubbo Service registry configuration , Sub attributes address Value “spring-cloud://localhost”, Note mount to Spring Cloud Registry Center

Be careful : If you use Spring Boot 2.1 And later , Configuration needs to be added spring.main.allow-bean-definition-overriding=true

Step four : Create the application main class , such as :

@EnableDiscoveryClient
@SpringBootApplication
public class DubboServerApplication {

public static void main(String[] args) {
SpringApplication.run(DubboServerApplication.class, args);
}

}

Build service interface consumer

First step : Create a Spring Boot project , stay pom.xml Introduced in step 1 API Packages and Spring Cloud Alibaba Yes Nacos and Dubbo Dependence , The specific content is consistent with that of service provider :

<dependencies>
<!-- Built in step 1 API package -->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>alibaba-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--<groupId>com.alibaba.cloud</groupId>-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

//...
</dependencies>

The second step : To configure Dubbo Service related information , such as :

spring.application.name=alibaba-dubbo-client
server.port=8002

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
dubbo.cloud.subscribed-services=alibaba-dubbo-server

Be careful :

  1. It's increased here dubbo.cloud.subscribed-services Parameters , Indicates the name of the service to subscribe to , It's configured here alibaba-dubbo-server Corresponding to the service provider's in the previous section spring.application.name Value , This is the application name of the service provider .
  2. If you use Spring Boot 2.1 And later , Configuration needs to be added spring.main.allow-bean-definition-overriding=true.

The third step : Create the application main class , And implement an interface , Invoke in this interface Dubbo service , such as :

@EnableDiscoveryClient
@SpringBootApplication
public class DubboClientApplication {

public static void main(String[] args) {
SpringApplication.run(DubboClientApplication.class, args);
}

@Slf4j
@RestController
static class TestController {

@Reference
HelloService helloService;

@GetMapping("/test")
public String test() {
return helloService.hello("didispace.com");
}
}

}

Be careful : there @Reference The note is org.apache.dubbo.config.annotation.Reference

Test verification

After all the above development , We can Nacos、 Service providers 、 Service consumers start in turn . After startup , We can do it in Nacos In the service list of the console, you can see the two services defined above , such as :

Next , We can call the... Defined in the service consumer /test Interface to trigger dubbo Service consumption . If everything goes well , The following results should be obtained :

$ curl localhost:8002/test
hello didispace.com

Summary

Through the example above , If you've ever played at the same time Spring Cloud and Dubbo, It's going to be very emotional . You don't have to worry about it at the same time Eureka and Zookeeper Configuration of , It is also different to pay attention to the health of these two middleware at the same time , Just pay attention and maintain Nacos Just one . And for Dubbo In terms of configuration and use , The configuration is quite simple , And the code is written in the same way as before Dubbo No big difference . stay Spring Cloud Alibaba Under the integration of ,Dubbo Users can enjoy the original RPC Performance advantage , You can enjoy it better Spring Cloud All kinds of benefits ; And for Spring Cloud Users , At the service governance level , Another good option . It can be said that this integration is really a good integration of the two user groups , It's a mutual achievement .

Reference material Official documents

Code example

The client code of this article , The sample reader can view the alibaba-dubbo-apialibaba-dubbo-serveralibaba-dubbo-client project :

If you are interested in these , welcome star、follow、 Collection 、 Forward support !

Please bring the original link to reprint ,thank
Similar articles