preface
When SpringBoot was hot, Alibaba’s distributed framework, Dubbo, was finally being maintained after a long hiatus, for whatever reason. In the previous microservices, Dubbox, a version maintained by Dangdang, was used, and the integration method was also used in XML configuration.
Before modification
The previous use of Dubbox in SpringBoot looked like this. Note the versions dubbox-2.8.4, ZKclient-0.6, and Zookeeper-3.4.6.
The spring-context-dubo. XML configuration file is introduced 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= <dubbo:application name="spring-boot-pay"/ > <! Subscribe by default:trueWhether to subscribe to this registry, if set tofalse, will only register, will not subscribe check default:true--> <dubbo: Registry protocol="zookeeper" address="192.168.1.180:2181" check="false"/ > <! -- Producer configuration The producer remote calls the parameter retries= three times by default"2" async="true"Asynchronous return result The default is synchronous timeout="10000"<dubbo:provider timeout= <dubbo:provider timeout= <dubbo:provider timeout="10000" threads="10" threadpool="fixed" loadbalance="roundrobin"/ > <! -- name="dubbo"To prevent a large number of connections from being suspended, the number of received connections can be limited on the service provider to protect the service provider. Host Deployment Internet Set to Intranet communication address --> <dubbo:protocol name="dubbo" port="1" dispatcher="all" accepts="1000"/ > <! <dubbo: Annotation package="com.itstyle"/>
</beans>Copy the code
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("Payment project initiated"); }}Copy the code
After transforming
However, SpringBoot introduces a new concept, SpringBoot Starter, which effectively reduces the complexity of the project development process and has a very good effect on simplifying the development operation.
The concept of the starter
The starter will include all of the dependencies used, avoiding the hassle of having to introduce them themselves.
It is important to note that different starter is to solve different dependencies, so their internal implementation may be very different, for example, jPA starter and Redis starter may be implemented differently. That’s because the essence of the starter synthesize is that it’s a logical abstraction, and maybe the idea is a little bit like a Docker, because they’re all doing a wrapper, and if you know what the problem is that Docker solves, Maybe you can draw an analogy between Docker and starter.
The realization of the starter
Although different starter implementations vary, they generally use the same two things: ConfigurationProperties and AutoConfiguration.
Because Spring Boot believes in convention over configuration, we used ConfigurationProperties to save our configuration, which can have a default value that takes effect without actively overwriting the original configuration. This can be very useful in many situations.
In addition, the starter ConfigurationProperties enables all ConfigurationProperties to be gathered into a single file (typically application.properties in the resources directory), thus ending the XML hell of Spring projects.
The overall logic of starter
Better than Dubbo, he will certainly create his own starter to cater to the Spring Boot craze.
Here we use the newer version of Dubbo, pom.xml, to introduce the following:
<! Alibaba </groupId> <artifactId>dubbo</artifactId> < version > 2.6.2 < / version > < / dependency > < the dependency > < groupId > com. Alibaba. Spring. The boot < / groupId > < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 2.0.0 < / version > < / dependency > <! Zkclient --> <dependency> <groupId>org.apache. groupId> < artifactId > curator - recipes < / artifactId > < version > 4.0.1 < / version > < / dependency >Copy the code
Application. The properties configuration:
# dubbo Springboot configurationspring.dubbo.application.id=springboot_pay spring.dubbo.application.name=springboot_pay Spring. Dubbo. Registry. Address = zookeeper: / / 192.168.1.127:2181 spring. Dubbo. The provider. The threads = 10 spring.dubbo.provider.threadpool=fixed spring.dubbo.provider.loadbalance=roundrobin spring.dubbo.server=true
spring.dubbo.protocol.name=dubboCopy the code
Add the following comments 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("Payment project initiated"); }}Copy the code
Related exposed interface implementation configuration:
import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; @component public class AliPayServiceImpl implements IAliPayService {// implements IAliPayService}Copy the code
Finally, start the service. If the service is successfully started and registered in the registry, the transformation is successful.
supplement
Dubbo 2.6.1 is the first release after the change of structure. Dubbo 2.6.0 has merged the Dubbox branch provided by Dangdang.
Dubbo’s release strategy: Two major releases are developed in parallel, with 2.5.x being the stable release and 2.6.x being the experimental release of new features. When all experiments on 2.6 are stable, we will move to 2.5.
conclusion
- Replace Dubbox 2.8.4 with Dubbo 2.6.2
- The original spring-context-dubo. XML configuration is replaced with dubo-spring-boot-starter 2.0.0
- Zkclient 0.6 is replaced with co-curator recipes 4.0.1
- Zookeeper 3.4.6 was upgraded to ZooKeeper 3.5.3
case
Alipay, wechat, UnionPay detailed code cases: gitee.com/52itstyle/s…