search
HomeJavajavaTutorialIntroduction to integrating Dubbo distributed services in SpringBoot development

This article brings you an introduction to the integration of Dubbo distributed services in SpringBoot development. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

When SpringBoot was very popular, Alibaba's distributed framework Dubbo was finally maintained after being suspended for N years for unknown reasons. In the previous microservices, the Dangdang-maintained version of Dubbox was used, and the integration method also used the xml configuration method.

Before transformation

This is how Dubbox was used in SpringBoot before. First briefly record the versions, Dubbox-2.8.4, zkclient-0.6, zookeeper-3.4.6.

The spring-context-dubbo.xml configuration file introduced into the project is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
    <!-- 记录监控信息 -->
    <dubbo:monitor protocol="registry"/>
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="spring-boot-pay" />
    <!-- 使用zookeeper注册中心暴露服务地址 subscribe 默认:true 是否向此注册中心订阅服务,如果设为false,将只注册,不订阅 check 默认:true 注册中心不存在时,是否报错    -->
    <dubbo:registry protocol="zookeeper" address="192.168.1.180:2181" check="false"/>
    <!-- 
               生产者配置 生产者  远程默认调用3次 参数 retries="2" async="true" 异步返回结果 默认是同步 timeout="10000" 毫秒
               用dubbo协议在20882端口暴露服务  固定线程池 10 启动时建立线程,不关闭,一直持有  负载均衡策略 轮询
     -->
    <dubbo:provider  timeout="10000"  threads="10" threadpool="fixed" loadbalance="roundrobin"/>
    <!-- name="dubbo" 协议名称   为防止被大量连接撑挂,可在服务提供方限制大接收连接数,以实现服务提供方自我保护。 host 部署外网设置为内网通信地址-->
    <dubbo:protocol name="dubbo" port="-1" dispatcher="all"  accepts="1000"   />
    
    <!-- 使用注解方式-->     
    <dubbo:annotation package="com.itstyle"/>
</beans>

The startup class introduces the following annotations:

@SpringBootApplication
@ImportResource({"classpath:spring-context-dubbo.xml"})
public class Application{
    private static final Logger logger = Logger.getLogger(Application.class);

    public static void main(String[] args) throws InterruptedException,
            IOException {
        logger.info("支付项目启动 ");
    }

}

After transformation

However, SpringBoot introduces the new concept Spring Boot Starter, which effectively reduces the complexity of the project development process and has a very good effect on simplifying development operations.

The concept of starter

starter will include all the dependencies used, avoiding the trouble caused by developers themselves introducing dependencies.

It should be noted that different starters are designed to solve different dependencies, so their internal implementations may be very different. For example, the starter of jpa and the starter of Redis may have different implementations. This is because The essence of starter lies in synthesisize, which is a layer of abstraction at the logical level. Maybe this concept is somewhat similar to Docker, because they are both doing a "packaging" operation. If you know what problem Docker is to solve, maybe You can make an analogy with Docker and starter.

Implementation of starter

Although different starters have different implementations, they basically use the same two contents: ConfigurationProperties and AutoConfiguration.

Because Spring Boot firmly believes in the concept of "convention over configuration", we use ConfigurationProperties to save our configurations, and these configurations can have a default value, that is, when we do not actively overwrite the original configuration , the default value will take effect, which is very useful in many situations.

In addition, starter's ConfigurationProperties also allows all configuration properties to be gathered into one file (usually application.properties in the resources directory), so we say goodbye to the XML hell in the Spring project.

The overall logic of starterIntroduction to integrating Dubbo distributed services in SpringBoot development is as strong as Dubbo. Of course, it will also create its own starter to cater to the popularity of Spring Boot.

Here we use the newer version of Dubbo. The pom.xml introduces the following:

<!-- dubbo 替换  dubbox-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba.spring.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
<!-- curator-recipes 替换  zkclient-->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.0.1</version>
</dependency>

application.properties configuration:

## dubbo springboot 配置
spring.dubbo.application.id=springboot_pay
spring.dubbo.application.name=springboot_pay
spring.dubbo.registry.address=zookeeper://192.168.1.127:2181
spring.dubbo.provider.threads=10
spring.dubbo.provider.threadpool=fixed
spring.dubbo.provider.loadbalance=roundrobin
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo

Add the following annotations to the startup class:

@EnableDubboConfiguration
@SpringBootApplication
public class Application{
    private static final Logger logger = Logger.getLogger(Application.class);

    public static void main(String[] args) throws InterruptedException,
            IOException {
        logger.info("支付项目启动 ");
    }

}

Relevant exposed interface implementation configuration:

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Service
@Component
public class AliPayServiceImpl implements IAliPayService {
      //省略代码
}

Finally start the service. If it is started successfully and registered in the registration center, the transformation is successful.

Supplement

Dubbo 2.6.1 is the first version released after changing the structure. Dubbo 2.6.0 has merged the Dubbox branch provided by Dangdang.com.

Dubbo’s version strategy: two major versions are developed in parallel, 2.5.x is a stable version, and 2.6.x is an experimental version with new features. After the experiments on 2.6 are stable, it will be migrated to 2.5.

Summary

  • ##The original Dangdang Dubbox 2.8.4 was replaced by Dubbo 2.6.2

  • Original The spring-context-dubbo.xml configuration is replaced with dubbo-spring-boot-starter 2.0.0

  • The original zkclient 0.6 is replaced with curator-recipes 4.0.1

  • Original zookeeper 3.4.6 upgraded to zookeeper 3.5.3

The above is the detailed content of Introduction to integrating Dubbo distributed services in SpringBoot development. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Apr 19, 2025 pm 07:57 PM

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Apr 19, 2025 pm 07:54 PM

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

In Java programming, how to stop subsequent code execution when student ID is repeated?In Java programming, how to stop subsequent code execution when student ID is repeated?Apr 19, 2025 pm 07:51 PM

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Apr 19, 2025 pm 07:48 PM

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

After the Spring Boot service is running for a period of time, how to troubleshoot?After the Spring Boot service is running for a period of time, how to troubleshoot?Apr 19, 2025 pm 07:45 PM

The troubleshooting idea of ​​SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...

How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?Apr 19, 2025 pm 07:42 PM

How to push video streams from Hikvision camera SDK to front-end Vue project? During the development process, you often encounter videos that need to be captured by the camera to be circulated...

How to limit access to access_token via OAuth2.0 scope parameter?How to limit access to access_token via OAuth2.0 scope parameter?Apr 19, 2025 pm 07:39 PM

How to use access_token of OAuth2.0 to restrict interface access permissions How to ensure access_token when authorizing using OAuth2.0...

In Spring Boot Redis, how to solve the problem of returning garbled codes?In Spring Boot Redis, how to solve the problem of returning garbled codes?Apr 19, 2025 pm 07:36 PM

SpringBootRedis gets the key garbled problem analysis using Spring...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.