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

What is the additional supported RPC scheme Dubbo under Spring Cloud Alibaba

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the RPC solution Dubbo supported by Spring Cloud Alibaba? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

A long time ago, at the beginning of the basic course of Spring Cloud, I wrote an article: "the choice of basic framework for micro-service architecture: Spring Cloud or Dubbo?" Many readers may have read it. Since then, there has been a question about how to choose these two frameworks. In fact, I have clearly mentioned in the article that the comparison between Spring Cloud and Dubbo itself is unfair. The former is mainly a relatively complete architectural scheme, while Dubbo is only a service governance and RPC implementation.

Because Dubbo has a very large user group in China, but its surrounding facilities and components are not so perfect. Many developers and users want to enjoy the ecology of Spring Cloud, so there will be some cases and methods that Spring Cloud and Dubbo are used together, but most of the Spring Cloud integration Dubbo solutions have been awkward. This is mainly due to the fact that the registry of Dubbod uses ZooKeeper, but at the beginning, the registry of Spring Cloud does not support ZooKeeper, so there are two different registries in many schemes, and then even if Spring Cloud supports ZooKeeper, the granularity of service information is not consistent with storage. Therefore, for a long time, at the level of service governance, the two have been a perfect integration scheme.

It was not until the emergence of Spring Cloud Alibaba that such a problem was solved. In previous tutorials, we have introduced the use of Nacos in Spring Cloud Alibaba as a service registry, and under this we can use Ribbon or Feign to achieve service consumption just like traditional Spring Cloud applications. Let's move on to the additional RPC scheme supported by Spring Cloud Alibaba: Dubbo.

Entry-level case

We first through a simple example to intuitively feel under the Nacos service registry, the use of Dubbo to achieve service providers and service consumers. The installation and use of Nacos are omitted here. If you don't know anything about Nacos, you can check out the registration and discovery of services using Nacos in this series. Let's go directly to the steps of using Dubbo.

Build a service interface

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

Public interface HelloService {String hello (String name);} build the service interface provider

Step 1: create a Spring Boot project and introduce the API package built in the first step and Spring Cloud Alibaba's dependency on Nacos and Dubbo in pom.xml, such as:

Com.didispace alibaba-dubbo-api 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.cloud spring- Cloud-starter-dubbo org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery / /...

There are two points to note here:

The spring-boot-starter-actuator package must be included, or the startup will report an error.

The spring-cloud-starter-dubbo package needs to pay attention to groupId, which is determined according to the specific spring cloud alibaba version dependencies used.

During the incubation period, the groupId used is: org.springframework.cloud

After the project incubates, the groupId used is modified to com.alibaba.cloud, so users need to pay attention to whether it is used correctly. Avoid the problem of not loading the corresponding JAR package.

Step 2: implement the Dubbo interface

@ Servicepublic class HelloServiceImpl implements HelloService {@ Override public String hello (String name) {return "hello" + name;}}

Note: the @ Service annotation here is not Spring's, but org.apache.dubbo.config.annotation.Service 's.

Step 3: configure information related to the Dubbo service, such as:

Spring.application.name=alibaba-dubbo-serverserver.port=8001spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848# specifies the scan base package dubbo.scan.base-packages=com.didispace.alibaba.dubbo.serverdubbo.protocol.name=dubbodubbo.protocol.port=-1dubbo.registry.address=spring-cloud://localhost of the Dubbo service implementation class

The configuration instructions are as follows:

Dubbo.scan.base-packages: specifies the scanning benchmark package for the Dubbo service implementation class

Dubbo.protocol: the protocol configuration exposed by the Dubbo service, where the subattribute name is the protocol name and port is the protocol port (- 1 means self-increment port, starting from 20880)

Dubbo.registry: Dubbo service registry configuration, where the value of the child attribute address is "spring-cloud://localhost", indicating that it is mounted to the Spring Cloud registry

Note: if you use Spring Boot 2.1and later, you need to add the configuration spring.main.allow-bean-definition-overriding=true

Step 4: create the application main class, such as:

@ EnableDiscoveryClient@SpringBootApplicationpublic class DubboServerApplication {public static void main (String [] args) {SpringApplication.run (DubboServerApplication.class, args);}} build the service interface consumer

Step 1: create a Spring Boot project and introduce the API package built in the first step and Spring Cloud Alibaba's dependence on Nacos and Dubbo into pom.xml, which is consistent with the service provider:

Com.didispace alibaba-dubbo-api 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.cloud spring- Cloud-starter-dubbo org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery / /...

Step 2: configure information related to the Dubbo service, such as:

Spring.application.name=alibaba-dubbo-clientserver.port=8002spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848dubbo.protocol.name=dubbodubbo.protocol.port=-1dubbo.registry.address=spring-cloud://localhostdubbo.cloud.subscribed-services=alibaba-dubbo-server

Note:

The dubbo.cloud.subscribed-services parameter is added to indicate the name of the service to which you want to subscribe. The alibaba-dubbo-server configured here corresponds to the spring.application.name value of the service provider in the previous section, that is, the application name of the service provider.

If you use Spring Boot 2.1and later, you need to add the configuration spring.main.allow-bean-definition-overriding=true.

Step 3: create the application main class and implement an interface in which the Dubbo service is invoked, such as:

@ EnableDiscoveryClient@SpringBootApplicationpublic 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: the @ Reference annotation here is org.apache.dubbo.config.annotation.Reference

Test verification

After all the above development is completed, we can start the Nacos, the service provider, and the service consumer in turn. After completing the startup, we can see the two services defined above in the list of services in the Nacos console, such as:

Next, we can trigger the consumption of the dubbo service by calling the / test interface defined in the service consumer. If all goes well, you should be able to get the following results:

Summary of $curl localhost:8002/testhello didispace.com

Through the above example, if you have ever played Spring Cloud and Dubbo at the same time, you will be impressed. You no longer have to worry about the configuration of Eureka and Zookeeper at the same time, nor the health of these two middleware at the same time, you only need to pay attention to and maintain Nacos. For the configuration and use of Dubbo, the configuration is still quite simple, and the coding is not much different from the previous Dubbo. Under the integration of Spring Cloud Alibaba, Dubbo users can not only enjoy the performance advantages brought by the original RPC, but also better enjoy the various benefits of Spring Cloud; for Spring Cloud users, it is a good option at the service governance level. It can be said that the integration of this piece is really so that the two major user groups have been a good integration, played a role in mutual achievement. For more information about the Spring Cloud and Spring Cloud Alibaba tutorials, please click here.

After reading the above, have you mastered the method of Dubbo, which is the additional supported RPC solution under Spring Cloud Alibaba? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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