Dubbo with the perfect combination of Spring Cloud

Dubbo with the perfect combination of Spring Cloud

1 Overview

Might sound Dubbo, many people are not familiar with, after all, is one from 2012 began to open-source Java RPC framework, among a variety of reasons to stop updating 4 and a half years, with only a hair over a small version repair a small bug, even we all thought that this project is dead, actually resumed updated in September 2017, is nothing less than miraculous.

A lot of people get on the network and Spring Cloud do Dubbo contrast, probably in everyone's mind, these two frameworks can equate it, then there is a very popular form on the Web, a more detailed comparison of the Spring Cloud and Dubbo, the following table:

Dubbo SpringCloud
Service registry Zookeeper  Spring Cloud Netfix Eureka
Service invocation RPC  REST API
Service Monitoring Dubbo-monitor Spring Boot Admin
Fuse imperfect Spring Cloud Netflix Hystrix
Services Gateway no Spring Cloud Netflix Zuul
Distributed Configuration no Spring Cloud Config
Service Tracking no Spring Cloud Sleuth
data flow no Spring Cloud Stream
Batch jobs no Spring Cloud Task
Information Bus no Spring Cloud Bus

A number of core components listed above, of course, need to affirm that here, for the Dubbo summarized in the table is "no" does not mean that the components can not be achieved, but only Dubbo frame itself is not provided, an additional function corresponding to achieve integration, so it looks Dubbo is really more like a subset of Spring Cloud.

Dubbo in the country has a huge user base, we want to enjoy eco-Spring Cloud while using Dubbo, the emergence of a wide range of integrated solutions, but because of the different service centers, a variety of integrated solutions is not so natural until Spring Cloud Alibaba project appeared to provide service registry Nacos by the official only after the perfect solution to this problem. And provides Dubbo and Spring Cloud integration solution named: Dubbo Spring Cloud.

1.2 Dubbo Spring Cloud Overview

Dubbo Spring Cloud Spring Cloud built on top of native, its ability to govern aspects of services can be considered Spring Cloud Plus, not only fully cover the Spring Cloud native features, and provide a more stable and mature implementation, characteristics than in the following table :

Functional components Spring Cloud Dubbo Spring Cloud
Distributed configuration (Distributed configuration) Git、Zookeeper、Consul、JDBC Spring Cloud + Dubbo distributed configuration Configuration Center
Service registration and discovery (Service registration and discovery) Eureka、Zookeeper、Consul Spring Cloud native registry + Dubbo native registry
Load balancing (Load balancing) The Ribbon (random polling algorithms) Built achieve Dubbo (+ algorithms like random weights, and other characteristics polling)
Services fuse (Circuit Breakers) Spring Cloud Hystrix Spring Cloud Hystrix + Alibaba Sentinel 等
Service call (Service-to-service calls) Open Feign、RestTemplate Spring Cloud service call + Dubbo @Reference
Link Tracking (Tracing) Spring Cloud Sleuth + Zipkin Zipkin、opentracing 等

Comparison table above is taken from Dubbo Spring Cloud official documents.

And Dubbo Spring Cloud based Dubbo Spring Boot 2.7.1 and Spring Cloud 2.x development, regardless of the user or developer is Dubbo Spring Cloud users can easily manage, and cost close to the price of "zero" make up application migration. Dubbo Spring Cloud is committed to simplifying cloud native development costs, enhance research and development in order to achieve the performance and improve application performance objectives.

1.3 Dubbo Spring Cloud Key Features

  • High performance RPC interface proxy for calling: remote call providing high performance based on the ability of the agent, the service interface to a particle size, the shield remote calls to the underlying details.
  • Intelligent load balancing: Built-in a variety of load balancing strategy, IntelliSense health of downstream node, significantly reduce call latency, improve system throughput.
  • Auto service registration and discovery: support a variety of registry services, offline real-time perception on the service instance.
  • Highly scalable: + follow microkernel design principles plug-in, all core competencies such as Protocol, Transport, Serialization is designed as an extension point, equal treatment and built to achieve third-party implementation.
  • Run-time traffic scheduling: built-in condition, scripts, and other routing policies by configuring different routing rules, easily publish gray, with features such as priority room.
  • Visualization of service management and operation and maintenance: provide rich service management, operation and maintenance tools: always check the service metadata, services, health status and call statistics, real-time routing policy issued, adjust the configuration parameters.

1.4 Spring Cloud Why RPC

Spring Cloud service in the micro-system built in, most developers are using Feign official component provided by internal service communication, HTTP clients such declarative use very simple, easy, elegant, but one thing , when using Feign consumer services, such comparison Dubbo RPC framework, performance is worrisome.

Although the micro-service architecture, speaks independent business segments in accordance with the deployment of micro-services, and run their processes. Communication between micro-services more inclined to use HTTP communication mechanism such short answer is, in most cases will use the REST API. This form of communication is very simple and efficient, and the development platform and language-independent, but under normal circumstances, HTTP and not turn KeepAlive function, that is currently connected to a short connection, short connection drawback is that each request needs to establish a TCP connection this makes its efficiency is rather low.

To provide external REST API service is a very good thing, but if the internal call is invoked using HTTP mode, it will seem seem poor performance, Spring Cloud default Feign components used for HTTP protocol used by an internal service call is invoked, At this point, if we use RPC calls internal services, external use REST API, it will be a very good choice, as it happens, Dubbo Spring Cloud gives us this choice of implementation.

2. combat

This summary will be a simple case of entry, tell us about the case of the use of Nacos as a service center, using Dubbo to implement the service provider and the service consumer.

Nacos installation, deployment, configuration and use have been described in previous chapters too, and will not repeat them here, if there is unclear readers, please refer to the previous Nacos series:

"Spring Cloud Alibaba | Preliminary Nacos service center"

"Spring Cloud Alibaba | Nacos service registration and discovery."

"Spring Cloud Alibaba | Nacos cluster deployment."

"Spring Cloud Alibaba | Nacos Configuration Management"

2.1 Creating a parent project dubbo-spring-cloud-demo

Dependent on the parent project pom.xml as follows:

Listing: Alibaba / Dubbo-the Spring-Cloud-Demo / pom.xml
***

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <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>
    <!-- Dubbo Spring Cloud Starter -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
    <!-- Spring Cloud Nacos Service Discovery -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

note:

  1. It must contain a spring-boot-starter-actuatorpackage, or start error.
  2. spring-cloud-starter-dubboNote packet groupId, determined according to the specific use of spring cloud alibaba version dependency.

    • If you use the hatch version, use groupId as:org.springframework.cloud
    • If you use the graduating version, groupId used are:com.alibaba.cloud
  3. The above-cited statement not specify a version, to be displayed<dependencyManagement>

2.2 create a sub-project dubbo_api

API module, storage Dubbo service interfaces and model definition, non-essential, only create better code reuse, and interface control management model specifications here.

Defines an abstract interface to HelloService.java:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_api/src/main/java/com/springcloud/dubbo_api/service/HelloService.java
***

public interface HelloService {
    String hello(String name);
}

2.3 create a sub-project dubbo_provider, Dubbo service provider

Project relies pom.xml as follows:

Listing: Alibaba / Dubbo-the Spring-Cloud-Demo / dubbo_provider / pom.xml
***

<!-- API -->
<dependency>
    <groupId>com.springcloud.book</groupId>
    <artifactId>ch13_1_dubbo_api</artifactId>
    <version>${project.version}</version>
</dependency>

Here the introduction of a common API module.

Dubbo implement interfaces, HelloServiceI.java as follows:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_provider/src/main/java/com/springcloud/dubbo_provider/service/HelloServiceI.java
***

@Service
public class HelloServiceI implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello " + name;
    }
}

Note: This @Servicecomment is not from the Spring org.springframework.stereotype.Service, but in Dubbo org.apache.dubbo.config.annotation.Service, do not misquoted. Here @Serviceannotation declares only the Java service (local) Dubbo implemented as a service.

Profile application.yml requires Java Services (local) configured to Dubbo service (remote) as follows:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_provider/src/main/resources/application.yml
***

server:
port: 8000
dubbo:
    scan:
        base-packages: com.springcloud.book.ch13_1_dubbo_provider.service
protocol:
    name: dubbo
    port: -1
registry:
    address: spring-cloud://192.168.44.129
spring:
application:
    name: dubbo-spring-cloud-provider
cloud:
    nacos:
    discovery:
        server-addr: 192.168.44.129:8848
main:
    allow-bean-definition-overriding: true

Note: In terms of exposure to Dubbo service, recommended use of external configuration, which specifies Java service implementation class scanning benchmark kit.

Dubbo Spring Cloud inherited characteristic configuration of the external Dubbo Spring Boot, the package may also be achieved by scanning the reference label @DubboComponentScan.

  • 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 in which the sub-address attribute values ​​"spring-cloud: //192.168.44.129", explained to mount Spring Cloud registry
  • spring.application.name: Spring application name, for Spring Cloud service registration and discovery. This value is considered at Dubbo Spring Cloud blessing dubbo.application.name, therefore, no longer displays arranged dubbo.application.name.
  • spring.main.allow-bean-definition-overriding: Increasing this setting in Spring Boot 2.1 and higher versions, since Spring Boot adjust the default behavior covered by the definition of Bean.
  • spring.cloud.nacos.discovery: Nacos service discovery and registration configuration, in which the sub-properties server-addr specified Nacos server host and port.

Create a Main Class Ch131DubboProviderApplication.java:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_provider/src/main/java/com/springcloud/dubbo_provider/DubboProviderApplication.java
***

@SpringBootApplication
@EnableDiscoveryClient
public class DubboProviderApplication {

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

}

2.4 create a sub-project dubbo_consumer, service caller:

Project relies pom.xml as follows:

Listing: Alibaba / Dubbo-the Spring-Cloud-Demo / dubbo_consumer / pom.xml
***

<!-- API -->
<dependency>
    <groupId>com.springcloud.book</groupId>
    <artifactId>ch13_1_dubbo_api</artifactId>
    <version>${project.version}</version>
</dependency>

Application.yml project configuration as follows:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/src/main/resources/application.yml
***

server:
port: 8080
dubbo:
protocol:
    name: dubbo
    port: -1
registry:
    address: spring-cloud://192.168.44.129
cloud:
    subscribed-services: dubbo-spring-cloud-provider
spring:
application:
    name: dubbo-spring-cloud-consumer
cloud:
    nacos:
    discovery:
        server-addr: 192.168.44.129:8848
main:
    allow-bean-definition-overriding: true
  • dubbo.cloud.subscribed-services: To subscribe to the service represents the service name, you can configure '*', on behalf of all subscription services, it is not recommended. For subscription and more applications, use "," split.

Test Interface HelloController.java as follows:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/src/main/java/com/springcloud/dubbo_consumer/controller/HelloController.java
***

@RestController
public class HelloController {
    @Reference
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello("Dubbo!");
    }
}

Note: Here's @Referenceannotations are org.apache.dubbo.config.annotation.Reference.

Start main class Ch131DubboConsumerApplication.java follows:

代码清单:Alibaba/dubbo-spring-cloud-demo/dubbo_consumer/src/main/java/com/springcloud/dubbo_consumer/DubboConsumerApplication.java
***

@SpringBootApplication
@EnableDiscoveryClient
public class DubboConsumerApplication {

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

}

2.5 Test

Dubbo_provider project promoters and sub-projects dubbo_consumer, after seeing two services on startup is complete, we can access the console Nacos list of services, as:

We open the browser to access: http: // localhost: 8080 / hello, you can see displayed correctly Hello Dubbo!, the test is successful, as shown:

3. Sample Code

Sample Code -Github

Sample Code -Gitee

4. Reference

Dubbo Spring Cloud official documents

Guess you like

Origin www.cnblogs.com/babycomeon/p/11546737.html