您的位置:首页 > 编程语言 > Java开发

Spring Boot整合Dubbo运行

2017-06-20 13:42 246 查看
原文地址:http://www.cnblogs.com/whthomas/p/running-dubbo-on-spring-boot.html

Dubbo
(http://dubbo.io/)
是阿里的开源的一款分布式服务框架。而
Spring
Boot
则是Spring社区这两年致力于打造的简化Java配置的微服务框架。

利用他们各自优势,配置到一起,可以帮助我们构建出非常优秀的微服务。


配置Maven

使用的Dubbo的一般都是大型项目,maven项目构建也会使用parent节点,Spring Boot考虑到了这种情况,提供了
dependencyManagement
的方式,引入Spring Boot的包。
<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>


在具体的starter选择上,使用
spring-boot-starter
,这样spring-boot会只启动spring的配置,而不会自动启用什么软件或者是网络服务器。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>


Maven
的部分就配置完了


配置启动类

Dubbo提供了一个启动类
com.alibaba.dubbo.container.Main
,在Dubbo的项目中,大多数都是使用这个类启动项目的,而这个类中会加载很多的容器,其中最重要的就是
SpringContainer
,这个类会读取系统中相关的spring配置。

而在
Spring
Boot
的体系中,则是任意写一个启动类,然后加注解
@SpringBootApplication
在这个类之上,让spring
boot能够发现这个类,并启动这个类的main方法。

Spring
Boot
的设计中,推荐大家使用无XML配置的方式启动项目,但大多数项目由于各种原因很难摆脱XML的束缚,这种情况下,使用
@ImportResource
注解引入xml配置,在
@ImportResource
中填写需要引入的配置:
@ImportResource({"classpath:spring-context.xml"})


@ImportResource
会自动去查找resource目录下的文件并引入
Spring
Boot


经接着,我们需要解决启动类中
main()
方法的问题,一般情况下,我们写的启动类会使用:

SpringApplication.run(DemoApplication.class, args);

但由于Dubbo项目引用了
Spring
Web
框架中的一些类,使得Maven也不得不导入
Spring
Web
项目,了而
Spring
Boot
发现跟web相关的框架就会去启动web容器(比如
Tomcat
Jetty
等),并且我们的配置是没有添加容器的,所以
main()
方法需要写成:
ApplicationContext ctx = new SpringApplicationBuilder()
.sources(DemoApplication.class)
.web(false) // 没错,把项目设置成非web环境
.run(args);


还需要注意的是,由于使用了非常纯粹的starter,而且Dubbo的网络框架也是非阻塞的,所以我们还需使用一些方法,保持主线程的阻塞状态。比如我使用
CountDownLatch
来做这件事。最终形成了以下启动类的完整代码:
@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("项目启动!");

CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
closeLatch.await();
}
}


好了这个类构建完成之后,我们按照Dubbo的方式配置好service,就可以打包了。


打包部署

Dubbo本身有一个比较麻烦的问题就是在打包部署,从官方的demo上来看,Dubbo打包借助了maven插件
assembly
,而且要写不少的配置,而
Spring
Boot
的工具相对来说,几乎不需要任何的配置。在
pom.xml
build
节点下面,配置spring-boot-maven-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>


命令行切换到项目的目录之后,执行命令:

mvn package

在target目录下面,会产生两个文件



demo-0.0.1-SNAPSHOT.jar
拷贝到任意一个项目下启动:




服务调用

我简单地写了一个
consumer
的测试类,从
consumer
项目调用项目,也是可以顺利调用到这个服务的。



这个时候在
provider
端,也是可以
consumer
看到调用的情况
:



Bingo ! Dubbo running over the Spring Boot!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: