Dubbo
( http://dubbo.io/ ) is an open source distributed service framework of Alibaba. Spring Boot
It is a microservice framework that the Spring community has been working on for the past two years to simplify Java configuration .
Using their respective advantages and configuring them together can help us build very good microservices.
Configure Maven
The Dubbo used is generally large-scale projects, and the maven project construction will also use the parent node. Spring Boot considers this situation and provides a dependencyManagement
way to introduce Spring Boot packages.
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.3.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
In the specific starter selection, use spring-boot-starter
, so that spring-boot will only start the spring configuration, and will not automatically enable any software or network server.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
Maven
part is configured
Configure startup class
Dubbo provides a startup class com.alibaba.dubbo.container.Main
. In Dubbo's projects, most of them use this class to start the project, and this class will load a lot of containers. The most important thing is SpringContainer
that this class will read the relevant spring in the system. configuration.
In Spring Boot
the system, it is to write a startup class arbitrarily, and then annotate @SpringBootApplication
it on top of this class, so that spring boot can discover this class and start the main method of this class.
In Spring Boot
the design @ImportResource
of @ImportResource
.
@ImportResource({"classpath:spring-context.xml"})
@ImportResource
It will automatically find the files in the resource directory and import them Spring Boot
.
After that, we need to solve main()
the problem of the method in the startup class. Under normal circumstances, the startup class we write will use:
SpringApplication.run(DemoApplication.class, args);
However, because the Dubbo project references Spring Web
some classes in the framework, Maven has to import Spring Web
the project, and if it Spring Boot
finds a web-related framework, it will start the web container (for example Tomcat
, Jetty
etc.), and our configuration does not add a container, So the main()
method needs to be written as:
ApplicationContext ctx = new SpringApplicationBuilder() .sources(DemoApplication.class) .web(false) // Yes, set the project to a non-web environment .run(args);
It should also be noted that since a very pure starter is used, and Dubbo's network framework is also non-blocking, we still need to use some methods to keep the main thread blocked. For example I use CountDownLatch
to do this. Eventually the complete code for the following startup class is formed:
@SpringBootApplication @ImportResource({"classpath:spring/spring-context.xml"}) public class DemoApplication { private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); @Bean public CountDownLatch closeLatch() { return new CountDownLatch(1); } public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new SpringApplicationBuilder() .sources(DemoApplication.class) .web(false) .run(args); logger.info("Project started!"); CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class); closeLatch.await(); } }
Well, after the construction of this class is completed, we configure the service according to the Dubbo method, and we can package it.
Package deployment
Dubbo itself has a troublesome problem in packaging and deployment. From the official demo point of view, Dubbo is packaged with the help of the maven plug-in assembly
, and a lot of configuration needs to be written. Spring Boot
Relatively speaking, the tool does not require any configuration. Under pom.xml
the build
node, configure the spring-boot-maven-plugin plugin
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.5.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins>
After the command line switches to the directory of the project, execute the command:
mvn package
Under the target directory, two files will be generated
Copy to demo-0.0.1-SNAPSHOT.jar
any project to start:
service call
I simply wrote a consumer
test class to consumer
call the project from the project, and the service can also be called smoothly.
At this time, on the provider
end, you can also consumer
see the call:
Bingo ! Dubbo running over the Spring Boot