Spring Cloud and Dubbo perfect fusion of hand "Spring Cloud Alibaba"

A long time ago, at the beginning of Spring Cloud engage in basic tutorial, wrote this article: "basic framework for micro-services architecture choices: Spring Cloud or Dubbo? " , Many readers may have seen. After it has been about how these two frameworks election issue out of the fact I have a clear text mentioned, and compare Spring Cloud Dubbo itself is not fair, mainly the former is a more complete framework program, while Dubbo just RPC service governance and implementation.

Due to Dubbo in the country has a very large user base, but the surrounding facilities and components is relatively not so perfect. Many developers want to enjoy very user ecological Spring Cloud, so there will be the case with some of the methods used with the Spring Cloud Dubbo appear, but most has been the integration of Dubbo Spring Cloud usage scenarios are more awkward. This is mainly due Dubbod the registry using ZooKeeper, and Spring Cloud in the system registry does not support ZooKeeper at the beginning, so many programs is that there are two different registration centers, even after Spring Cloud support ZooKeeper, but because of service particle size is also inconsistent with the stored information. So, for a long time, in the service levels of governance, both of which it has been a perfect integration program.

Until Spring Cloud Alibaba, we were able to solve this problem. In the previous tutorial, we have introduced the use of Spring Cloud Alibaba in Nacos as a service registry, and can be as traditional Spring Cloud applications use the same or Feign Ribbon Under this service consumption is achieved. This, we have to continue to talk about next Spring Cloud Alibaba additional support RPC program: Dubbo.

Getting Case

We first through a simple example to intuitively feel under Nacos service registry, service providers and consumer use Dubbo to implement the service. Here the installation and use Nacos omitted, if Nacos do not know, you can view this series use Nacos realize service registration and discovery , the following steps directly into the Dubbo use.

Build Service Interface

Create a simple Java project, and an abstract interface defined below, for example:

public interface HelloService {

    String hello(String name);

}
复制代码

Building service provider interfaces

The first step : create a Spring Boot project, in pom.xmlintroducing the first step in building the API packages and Spring Cloud Alibaba's reliance on Nacos and Dubbo, such as:

    <dependencies>
        <!-- 第一步中构建的API包 -->
        <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 things to note here:

  1. It must contain a spring-boot-starter-actuatorpackage, or start error.
  2. spring-cloud-starter-dubboNote that the packet groupIdis determined according to the specific use of spring cloud alibaba version dependency.
    • During the project incubation, used groupIdto:org.springframework.cloud ;
    • After incubation project, using the groupIdmodified order com.alibaba.cloud, so users need to pay attention to whether correct. Avoid loading less than the corresponding JAR package of issues.

Step Two : Implement Interface Dubbo

@Service
public class HelloServiceImpl implements HelloService {

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

}
复制代码

Note: This @Servicecomment is not Spring, but the org.apache.dubbo.config.annotation.Servicenotes.

Step Three : 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

# 指定 Dubbo 服务实现类的扫描基准包
dubbo.scan.base-packages=com.didispace.alibaba.dubbo.server
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
复制代码

Configuration as follows:

  • dubbo.scan.base-packages: Dubbo designated service implementation class scanning benchmark kit
  • dubbo.protocol: Dubbo exposed service protocol configuration, wherein the sub-attribute name is the name of the protocol, port is the port protocol (-1 means increment the port, from the beginning 20880)
  • dubbo.registry: Dubbo service registry configuration, where the value of sub-attributes address the "spring-cloud: // localhost", explained to mount Spring Cloud registry

Note: If you use Spring Boot 2.1 and later when the need to increase the allocationspring.main.allow-bean-definition-overriding=true

Step Four : Create Main Class, for example:

@EnableDiscoveryClient
@SpringBootApplication
public class DubboServerApplication {

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

}
复制代码

Build consumer service interface

The first step : create a Spring Boot project, in pom.xmlintroducing the first step in building the API packages and Spring Cloud Alibaba's dependence on Nacos and Dubbo, the specific content and service providers is consistent:

    <dependencies>
        <!-- 第一步中构建的API包 -->
        <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>
复制代码

Step Two : 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
复制代码

note:

  1. Here more than the increase of dubbo.cloud.subscribed-servicesparameters, pledged to subscribe to the service name of the service, where the configuration of the alibaba-dubbo-servercorresponding service is provided on one side of the spring.application.namevalue, which is the name of the service provider application side.
  2. If you are using Spring Boot 2.1 and later, the need to increase the allocation spring.main.allow-bean-definition-overriding=true.

Step Three : Create Main Class, and implements an interface, call the service in Dubbo this interface, 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");
        }
    }

}
复制代码

Note: This @Referencecomment isorg.apache.dubbo.config.annotation.Reference

Testing and certification

After completion of the development of all the above, we can Nacos, service providers, service consumers in order to start up. After completing the start, we can see the two services defined above in the list of services Nacos console, such as:

file

Next, we can call customer service defined /testto trigger the consumer dubbo service interface. If all goes smoothly, you should be able to obtain the following results:

$ curl localhost:8002/test
hello didispace.com
复制代码

summary

By the above example, if you've ever played at the same time and Spring Cloud Dubbo, certainly emotionally. At the same time you do not have to worry about Eureka and Zookeeper configuration, while also concerned about the health of these two different middleware, only need to focus and maintain a good Nacos can. As for the configuration and use of Dubbo, the configuration is fairly simple, but the writing code is not much different from the previous Dubbo. Under the integration of Spring Cloud Alibaba, Dubbo user can either enjoy the original RPC brings performance advantages, but also can better enjoy various benefits Spring Cloud; for Spring Cloud users, service level management, but also on a good option. It can be said this is a real integration of these two user groups so that was a very good fusion, played a role in each other's achievements. More about Spring Cloud Spring Cloud Alibaba and tutorial content can click to see .

References : official document

The sample code

This article describes the contents of the client code, readers can view the following examples warehouse alibaba-dubbo-api, alibaba-dubbo-server, alibaba-dubbo-clientProject:

If you are interested in these, welcomed the star, follow, bookmarking, forwarding support! > Welcome to my public concern number: Program ape DD, get exclusive organize learning resources and push dry goods daily. If you are interested in my topic content, you can focus on my blog: didispace.com

Guess you like

Origin juejin.im/post/5d5a0657f265da03f564e2c1