使用maven构建dubbo服务jar包

服务器:192.168.1.121(edu-provider-01)


一、Dubbo服务的运行方式:


1、使用Servlet容器运行(Tomcat、Jetty等)----不可取 缺点:增加复杂性(端口、管理)

tomcat/jetty等占用端口,dubbo服务也需要端口

浪费资源(内存):单独启动tomcat,jetty占用内存大


2、自建Main方法类来运行(spring容器) ----不建议(本地调试可用) 

缺点: Dobbo本身提供的高级特性没用上

自已编写启动类可能会有缺陷

3、使用Dubbo框架提供的Main方法类来运行(Spring容器)----建议使用 优点:框架本身提供(com.alibaba.dubbo.container.Main)

可实现优雅关机(ShutdownHook)

注意点

spring-context.xml 

官方:服务容器的加载内容可以扩展,内置了spring, jetty, log4j等加载,可通过Container扩展点进行扩展
Dubbo是通过JDK的ShutdownHook来完成优雅停机的,所以如果用户使用"kill -9 PID"等强制关闭指令,是不会执行优雅停机的,只有通过"kill PID"时,才会执行。
原理:
服务提供方停止时,先标记为不接收新请求,新请求过来时直接报错,让客户端重试其它机器。 然后,检测线程池中的线程是否正在运行,如果有,等待所有线程执行完成,除非超时,则强制关闭。
服务消费方停止时,不再发起新的调用请求,所有新的调用在客户端即报错。然后,检测有没有请求的响应还没有返回,等待响应返回,除非超时,则强制关闭。

二、Maven构建Dubbo服务可执行Jar包的配置

[html]  view plain  copy
 
  1. <span style="font-size:14px;">  
  2.     <build>  
  3.         <finalName>edu-service-userfinalName>  
  4.   
  5.         <resources>  
  6.             <resource>  
  7.                 <targetPath>${project.build.directory}/classestargetPath>  
  8.                 <directory>src/main/resourcesdirectory>  
  9.                 <filtering>truefiltering>  
  10.                 <includes>  
  11.                     <include>**/*.xmlinclude>  
  12.                     <include>**/*.propertiesinclude>  
  13.                 includes>  
  14.             resource>  
  15.               
  16.             <resource>  
  17.                 <targetPath>${project.build.directory}/classes/META-INF/springtargetPath>  
  18.                 <directory>src/main/resources/springdirectory>  
  19.                 <filtering>truefiltering>  
  20.                 <includes>  
  21.                     <include>spring-context.xmlinclude>  
  22.                 includes>  
  23.             resource>  
  24.         resources>  
  25.           
  26.         <pluginManagement>  
  27.             <plugins>  
  28.                   
  29.                 <plugin>  
  30.                     <groupId>org.eclipse.m2egroupId>  
  31.                     <artifactId>lifecycle-mappingartifactId>  
  32.                     <version>1.0.0version>  
  33.                     <configuration>  
  34.                         <lifecycleMappingMetadata>  
  35.                             <pluginExecutions>  
  36.                                 <pluginExecution>  
  37.                                     <pluginExecutionFilter>  
  38.                                         <groupId>org.apache.maven.pluginsgroupId>  
  39.                                         <artifactId>maven-dependency-pluginartifactId>  
  40.                                           
  41.                                         <goals>  
  42.                                             <goal>copy-dependenciesgoal>  
  43.                                         goals>  
  44.                                     pluginExecutionFilter>  
  45.                                     <action>  
  46.                                         <ignore />  
  47.                                     action>  
  48.                                 pluginExecution>  
  49.                             pluginExecutions>  
  50.                         lifecycleMappingMetadata>  
  51.                     configuration>  
  52.                 plugin>  
  53.             plugins>  
  54.         pluginManagement>  
  55.         <plugins>  
  56.               
  57.             <plugin>  
  58.                 <groupId>org.apache.maven.pluginsgroupId>  
  59.                 <artifactId>maven-jar-pluginartifactId>  
  60.                 <configuration>  
  61.                     <classesDirectory>target/classes/classesDirectory>  
  62.                     <archive>  
  63.                         <manifest>  
  64.                             <mainClass>com.alibaba.dubbo.container.MainmainClass>  
  65.                               
  66.                             <useUniqueVersions>falseuseUniqueVersions>  
  67.                             <addClasspath>trueaddClasspath>  
  68.                             <classpathPrefix>lib/classpathPrefix>  
  69.                         manifest>  
  70.                         <manifestEntries>  
  71.                             <Class-Path>.Class-Path>  
  72.                         manifestEntries>  
  73.                     archive>  
  74.                 configuration>  
  75.             plugin>  
  76.             <plugin>  
  77.                 <groupId>org.apache.maven.pluginsgroupId>  
  78.                 <artifactId>maven-dependency-pluginartifactId>  
  79.                 <executions>  
  80.                     <execution>  
  81.                         <id>copy-dependenciesid>  
  82.                         <phase>packagephase>  
  83.                         <goals>  
  84.                             <goal>copy-dependenciesgoal>  
  85.                         goals>  
  86.                         <configuration>  
  87.                             <type>jartype>  
  88.                             <includeTypes>jarincludeTypes>  
  89.                             <useUniqueVersions>falseuseUniqueVersions>  
  90.                             <outputDirectory>  
  91.                                 ${project.build.directory}/lib  
  92.                             outputDirectory>  
  93.                         configuration>  
  94.                     execution>  
  95.                 executions>  
  96.             plugin>  
  97.         plugins>  
  98.   
  99. build>  
  100. span>  
三、可执行Dubbo服务Jar在linux上的操作

1.获取数据库密码密文,使用druid

上传druid-0.2.23.jar包到provider-01服务器192.168.1.121上,使用 Java -cp druid-0.2.23.jar com.alibaba.druid.filter.config.ConfigTools root获取加密的密码

[root@provider-01 ~]# java -cp druid-0.2.23.jar com.alibaba.druid.filter.config.ConfigTools root
bNVOqb7WKLX5Bjnw+LMv92taj25KOxDimXxILPQjw42wgv+1lHzOH8kr97xDwWdhpY67

QuYCS7sWN4W46YbkFA==
[root@provider-01 ~]# 


2.打包provider提供者服务jar



3.把edu-service-user.jar与其lib上传到provider-01服务器上

[root@provider-01 home]# mkdir /home/yxq/edu/service
[root@provider-01 home]# mkdir /home/yxq/edu/service/user


4.手动执行jar

启动zookeeper注册中心192.168.1.121

[root@provider-01 user]# ls
edu-service-user.jar  lib
[root@provider-01 user]# java -jar edu-service-user.jar 


dubbo管理控制台


[root@provider-01 user]# ps -ef | grep edu-service
root      12391  11678  1 06:46 pts/0    00:00:08 java -jar edu-service-user.jar
root      12423  11678  0 06:59 pts/0    00:00:00 grep edu-service
[root@provider-01 user]# kill 12391

手工维护Dubbo服务 

java -jar edu-service-xxx.jar & 

kill PID 

kill -9 PID

你可能感兴趣的:(dubbo)