dubbo框架中Main方法运行spring容器

转自:http://blog.csdn.net/u013168445/article/details/71194666

一:运行spring容器的方式有三种

1:使用tomcat、jetty等servlet容器运行

2:自己写一个Main方法运行

3:使用dubbo框架提供的Main方法运行

前面两种有一定的局限性,很多缺点,具体就不一一列举,有兴趣的同学可以研究一下,今天就给大家讲如何实现第三种方式

二:Main配置详讲

优点:由框架本身提供,可实现优雅关机

spring路径存放路径

  按照dubbo官网解说,要使得Main能正确的启用Spring容器,要求spring配置文件存放至

classpath*:META-INF/spring/*.xml路径底下

dubbo官方网址 http://dubbo.io/User+Guide-zh.htm

项目结构

项目是maven项目,框架是spring+mybatis+dubbo

 

maven配置

[java]  view plain  copy
 
  1. <span style="font-size:18px;"><build>  
  2.     <!--jar包名称-->  
  3.     <finalName>asset-server</finalName>  
  4.     <resources>  
  5.         <!--recources文件夹下的所有文件都打进jar包-->  
  6.         <resource>  
  7.             <targetPath>${project.build.directory}/classes</targetPath>  
  8.             <directory>src/main/resources</directory>  
  9.             <filtering>true</filtering>  
  10.             <includes>  
  11.                 <include>**/*.xml</include> 
  12.                 <include>**/*.properties</include>  
  13.             </includes>  
  14.         </resource>  
  15.   
  16.         <!-- 上文有说过,由于我的applicationContext.xml文件不是在META-INF下,这一段代码的作用就是将 applicationContext.xml文件拷贝到META-INF目录下-->  
  17.         <resource>  
  18.             <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>  
  19.             <directory>src/main/resources/spring</directory>  
  20.             <filtering>true</filtering>  
  21.             <includes>  
  22.                 <include>applicationContext.xml</include>  
  23.             </includes>  
  24.         </resource>  
  25.     </resources>  
  26.   
  27.     <pluginManagement>  
  28.         <plugins>  
  29.             <!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->  
  30.             <plugin>  
  31.                 <groupId>org.eclipse.m2e</groupId>  
  32.                 <artifactId>lifecycle-mapping</artifactId>  
  33.                 <version>1.0.0</version>  
  34.                 <configuration>  
  35.                     <lifecycleMappingMetadata>  
  36.                         <pluginExecutions>  
  37.                             <pluginExecution>  
  38.                                 <pluginExecutionFilter>  
  39.                                     <groupId>org.apache.maven.plugins</groupId>  
  40.                                     <artifactId>maven-dependency-plugin</artifactId>  
  41.                                     <versionRange>[2.0,)</versionRange>  
  42.                                     <goals>  
  43.                                         <goal>copy-dependencies</goal>  
  44.                                     </goals>  
  45.                                 </pluginExecutionFilter>  
  46.                                 <action>  
  47.                                     <ignore />  
  48.                                 </action>  
  49.                             </pluginExecution>  
  50.                         </pluginExecutions>  
  51.                     </lifecycleMappingMetadata>  
  52.                 </configuration>  
  53.             </plugin>  
  54.         </plugins>  
  55.     </pluginManagement>  
  56.     <plugins>  
  57.         <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->  
  58.         <plugin>  
  59.             <groupId>org.apache.maven.plugins</groupId>  
  60.             <artifactId>maven-jar-plugin</artifactId>  
  61.             <configuration>  
  62.                 <classesDirectory>target/classes/</classesDirectory>  
  63.                 <archive>  
  64.                     <manifest>  
  65.                         <mainClass>com.alibaba.dubbo.container.Main</mainClass>  
  66.                         <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->  
  67.                         <useUniqueVersions>false</useUniqueVersions>  
  68.                         <addClasspath>true</addClasspath>  
  69.                         <classpathPrefix>lib/</classpathPrefix>  
  70.                     </manifest>  
  71.                     <manifestEntries>  
  72.                         <Class-Path>.</Class-Path>  
  73.                     </manifestEntries>  
  74.                 </archive>  
  75.             </configuration>  
  76.         </plugin>  
  77.         <plugin>  
  78.             <groupId>org.apache.maven.plugins</groupId>  
  79.             <artifactId>maven-dependency-plugin</artifactId>  
  80.             <executions>  
  81.                 <execution>  
  82.                     <id>copy-dependencies</id>  
  83.                     <phase>package</phase>  
  84.                     <goals>  
  85.                         <goal>copy-dependencies</goal>  
  86.                     </goals>  
  87.                     <configuration>  
  88.                         <type>jar</type>  
  89.                         <includeTypes>jar</includeTypes>  
  90.                         <useUniqueVersions>false</useUniqueVersions>  
  91.                         <outputDirectory>  
  92.                             ${project.build.directory}/lib  
  93.                         </outputDirectory>  
  94.                     </configuration>  
  95.                 </execution>  
  96.             </executions>  
  97.         </plugin>  
  98.     </plugins>  
  99. </build></span>  

 

 

运行测试

1).在c盘新建文件夹asset-service,将lib文件夹,asset-service.jar放置文件夹(打包完成后可能还有其他文件,我们只需要这两个)

2)打开运行窗口,进入asset-service,执行命令 java -jar asset-server.jar &

 

可以看到dubbo监控中心监测到接口已经正常部署

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值