Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Spring Cloud Service Governance by Dubbo

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how Dubbo implements Spring Cloud service governance". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Version support

As Spring officially announced that Spring Cloud Edgware (hereinafter referred to as "E" version) will stop maintenance 13 after August 1, 2019, the current Dubbo Spring Cloud release version does not support "E" version, but is only developed for "F" version and "G" version. Spring Cloud users are also recommended and encouraged to update to "F" version or "G" version.

At the same time, Dubbo Spring Cloud is developed based on Apache Dubbo Spring Boot 2.7.x (the minimum Java version is 1.8), which provides a complete Dubbo annotation driver, external configuration and Production-Ready features. Click here for details.

The following table describes the Dubbo Spring Cloud version relationship mapping relationship:

Spring Cloud

Spring Cloud Alibaba

Spring Boot

Dubbo Spring Boot

Finchley

0.2.2.RELEASE

2.0.x

2.7.1

Greenwich

2.2.1.RELEASE

2.1.x

2.7.1

Edgware

0.1.2.RELEASE

1.5.x

This version is not supported by x: Dubbo Spring Cloud

Functional characteristics

Since Dubbo Spring Cloud is built on the native Spring Cloud, its service governance capability can be considered as Spring Cloud Plus, which not only completely covers the native features of Spring Cloud, but also provides a more stable and mature implementation. The feature comparison is shown in the following table:

Functional component

Spring Cloud

Dubbo Spring Cloud

Distributed configuration (Distributed configuration)

Git 、 Zookeeper 、 Consul 、 JDBC

Spring Cloud distributed configuration + Dubbo configuration Center (Dubbo 2.7supports configuration Center, which can be customized)

Service Registration and Discovery (Service registration and discovery)

Eureka 、 Zookeeper 、 Consul

Spring Cloud native registry (Spring Cloud native registry, including Nacos in Spring Cloud Alibaba in addition to Eureka, Zookeeper, Consul) + Dubbo native registry

Load balancing (Load balancing)

Ribbon (random, polling, etc.)

Dubbo built-in implementation (random, polling, algorithm + weight, etc.)

Service breaker (Circuit Breakers)

Spring Cloud Hystrix

Spring Cloud Hystrix + Alibaba Sentinel, etc. (Sentinel has been accepted as a candidate implementation of Circuit Breaker by the Spring Cloud project)

Service call (Service-to-service calls)

Open Feign 、 RestTemplate

Spring Cloud service call + Dubbo @ Reference

Link tracking (Tracing)

Spring Cloud Sleuth + Zipkin

Zipkin, opentracing, etc.

Highlight feature Dubbo registers and discovers using Spring Cloud services

Dubbo Spring Cloud implements Dubbo service registration and discovery based on Spring Cloud Commons abstraction. Applications can easily bridge to all native Spring Cloud registries, including:-Nacos-Eureka-Zookeeper-Consul by adding the externalized configuration attribute "dubbo.registry.address = spring-cloud://localhost".

Note: Dubbo Spring Cloud will support the coexistence of Spring Cloud registry and Dubbo registry in the next version, providing a dual registration mechanism to achieve seamless migration

Dubbo is invoked as a Spring Cloud service

By default, Spring Cloud Open Feign and @ LoadBalancedRestTemplate are two ways of invoking services for Spring Cloud. Dubbo Spring Cloud provides it with a third choice, that is, Dubbo service will appear as the equivalent citizen of Spring Cloud service invocation. Applications can expose and reference Dubbo service through Apache Dubbo annotations @ Service and @ Reference, and realize the communication of multiple protocols between services. At the same time, the service gateway can be easily implemented by using the Dubbo generalization interface.

Dubbo service introspection

Dubbo Spring Cloud introduces a new service governance feature-Service introspection (Service Introspection), which is designed to maximize the load on the registry and de-Dubbo registration meta-information center. Suppose a Spring Cloud application introduces Dubbo Spring Boot Starter and exposes N Dubbo services. Take the Dubbo Nacos registry as an example, the current application will register one Nacos application. Except before the Spring Cloud application itself, the other N applications come from Dubbo services. The greater the N, the heavier the registry load.

Therefore, the load of the Dubbo Spring Cloud application on the registry is equal to 1 / N of the traditional Dubbo, which theoretically increases the size of the cluster by N times without increasing the infrastructure investment. Of course, the future Dubbo will also provide the ability to serve introspection.

Dubbo Migration Spring Cloud Service invocation

Although Dubbo Spring Cloud completely retains the native Spring Cloud service invocation features, Dubbo services governance capabilities are beyond the capabilities of Spring Cloud Open Feign, such as high performance, high availability, and load balancing stability. Therefore, it is recommended that developers migrate Spring Cloud Open Feign or @ LoadBalancedRestTemplate to Dubbo services.

Considering that the migration process does not happen overnight, Dubbo Spring Cloud provides a solution, the @ DubboTransported annotation. This annotation can help the Spring Cloud Open Feign interface of the service consumer and the underlying layer of @ LoadBalanced RestTemplate Bean to call Dubbo (which can switch the protocols supported by Dubbo), while the service provider only needs to append the Dubbo @ Servce annotation to the original @ RestController class (need to extract the interface). In other words, seamless migration can be achieved without adjusting the Feign interface and RestTemplate URL. If the migration time is sufficient, it is recommended that you use Dubbo services to ReFactor the definition of native Spring Cloud services in the system.

Simple exampl

The method of developing Dubbo Spring Cloud applications is similar to traditional Dubbo or Spring Cloud applications. Follow these steps to fully implement the applications of Dubbo service providers and consumers. For a complete sample code, please visit the resources:

Dubbo service provider application

Dubbo service consumer application

Define Dubbo service interface

The Dubbo service interface is a remote communication contract between the service provider and the consumer, which is usually declared by a normal Java interface (interface), such as the EchoService interface:

Public interface EchoService {String echo (String message);}

To ensure contract consistency, it is recommended that the Dubbo service interface be packaged in a second-party or third-party artifact (jar), such as the above interface stored in artifact spring-cloud-dubbo-sample-api.

For service providers, they not only introduce the Dubbo service interface in the form of relying on artifact, but also need to implement it. The corresponding service consumer also needs to rely on the artifact and execute the remote method in the way of interface invocation. Next, we will further discuss how to implement Dubbo service providers and consumers.

Implement the Dubbo service provider to initialize the spring-cloud-dubbo-server-sample Maven project

First, create a Maven project named spring-cloud-dubbo-server-sample for artifactId and add the necessary dependencies of Dubbo Spring Cloud to its pom.xml file:

Org.springframework.cloud spring-cloud-dubbo-sample-api ${project.version} org.springframework.boot spring-boot-actuator org.springframework.cloud spring-cloud-starter-dubbo org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery this value is considered dubbo.application.name under Dubbo Spring Cloud blessing, so Configure dubbo.application.name without the need for display

Spring.cloud.nacos.discovery: Nacos service discovery and registration configuration, where the subattribute server-addr specifies the Nacos server host and port

For the above complete YAML configuration file, please refer to the spring-cloud-dubbo-server-samplebootstrap.yaml file

After completing the above steps, you also need to write a Dubbo Spring Cloud bootstrap class.

Guide the application of Dubbo Spring Cloud service provider

The Dubbo Spring Cloud bootstrap class is no different from a normal Spring Cloud application, as follows:

@ EnableDiscoveryClient @ EnableAutoConfiguration public class DubboSpringCloudServerBootstrap {public static void main (String [] args) {SpringApplication.run (DubboSpringCloudServerBootstrap.class);}}

Before booting DubboSpringCloudServerBootstrap, start the Nacos server ahead of time. When DubboSpringCloudServerBootstrap starts, the spring-cloud-dubbo-server-sample that will be applied appears in the Nacos console interface.

When the Dubbo service provider starts, the next step is to implement a Dubbo service consumer.

Implement the Dubbo service consumer

Because the Java service is ready for EchoService, service provider application spring-cloud-dubbo-server-sample, and Nacos server. The Dubbo service consumer only needs to initialize the service consumer Maven project spring-cloud-dubbo-client-sample and consume the Dubbo service.

Initialize the spring-cloud-dubbo-client-sample Maven project

With the service provider Maven engineering class, you need to add relevant Maven dependencies:

Org.springframework.cloud spring-cloud-alibaba-dependencies 2.2.1.RELEASE pom import org.springframework.cloud spring-cloud-dubbo-sample-api ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot Spring-boot-actuator org.springframework.cloud spring-cloud-starter-dubbo org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery > Current application will subscribe all services (size:x) in registry A lot of memory and CPU cycles may be used

> > thus it's strongly recommend you using the externalized property 'dubbo.cloud.subscribed-services' to specify the services

Since the current application is a Web application, it uses 8080 as the Web service port by default. If you need to customize it, you can adjust it through the property server.port.

Note: for the above complete YAML configuration file, please refer to the spring-cloud-dubbo-client-samplebootstrap.yaml file

Guide the application of Dubbo Spring Cloud service consumers

To reduce the implementation steps, the following bootstrap classes combine Dubbo service consumption and bootstrapping functions into one:

@ EnableDiscoveryClient@EnableAutoConfiguration@RestControllerpublic class DubboSpringCloudClientBootstrap {@ Reference private EchoService echoService; @ GetMapping ("/ echo") public String echo (String message) {return echoService.echo (message);} public static void main (String [] args) {SpringApplication.run (DubboSpringCloudClientBootstrap.class);}

Not only that, DubboSpringCloudClientBootstrap also acts as a REST Endpoint, consuming Dubbo EchoService services through exposure / echo Web services. Therefore, the HTTP GET method can be executed through the curl command:

$curl http://127.0.0.1:8080/echo?message=%E5%B0%8F%E9%A9%AC%E5%93%A5%EF%BC%88mercyblitz%EF%BC%89

The HTTP response is:

[echo] Hello, mercyblitz

The above results show that the application spring-cloud-dubbo-client-sample returns the spring-cloud-dubbo-server-sample calculated content of the service provider by consuming the Dubbo service.

This is the end of the content of "how Dubbo implements Spring Cloud service governance". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report