Running Dubbo On Spring Boot

Dubbo( http://dubbo.io/ ) is an open source distributed service framework of Alibaba. Spring BootIt 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 dependencyManagementway 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>
 

Mavenpart 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 SpringContainerthat this class will read the relevant spring in the system. configuration.

In Spring Bootthe system, it is to write a startup class arbitrarily, and then annotate @SpringBootApplicationit on top of this class, so that spring boot can discover this class and start the main method of this class.

In Spring Bootthe design @ImportResourceof @ImportResource.

@ImportResource({"classpath:spring-context.xml"})
 

@ImportResourceIt 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 Websome classes in the framework, Maven has to import Spring Webthe project, and if it Spring Bootfinds a web-related framework, it will start the web container (for example Tomcat, Jettyetc.), 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 CountDownLatchto 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 BootRelatively speaking, the tool does not require any configuration. Under pom.xmlthe buildnode, 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


The two files generated

Copy to demo-0.0.1-SNAPSHOT.jarany project to start:


Start the provider service

service call

I simply wrote a consumertest class to consumercall the project from the project, and the service can also be called smoothly.


consumer test results

At this time, on the providerend, you can also consumersee the call:


consumer call of

Bingo ! Dubbo running over the Spring Boot

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326945097&siteId=291194637