天天看點

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

使用maven建構dubbo服務可執行jar包

  • 一、dubbo服務啟動方式分析
    • 1、使用Servlet容器運作(Tomcat、Jetty等)----不可取
    • 2、自建Main方法運作(Spring容器) ----不建議(本地調試可用)
    • 3、Dubbo架構提供Main方法類運作(Spring容器)----推薦使用
      • 可實作優雅關機 dubbo.shutdown.hook
  • 二、修改spring-context.xml
  • 三、修改服務pom.xml檔案
  • 四、打包
    • 4.1 右擊edu-common-parent 項目的pom.xml run as-->maven install
    • 4.2 右擊edu-facade-user 項目的pom.xml run as-->maven install
    • 4.3 右擊edu-service-user 項目的pom.xml run as-->maven install
  • 五、運作
      • 輸入指令 java -jar edu-service-user.jar & (啟動之前不要忘記啟動zookeper)
  • 六、檢視結果
  • 備注

一、dubbo服務啟動方式分析

1、使用Servlet容器運作(Tomcat、Jetty等)----不可取

缺點:增加複雜性(端口、管理)

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

浪費資源(記憶體):單獨啟動tomcat,jetty占用記憶體大

2、自建Main方法運作(Spring容器) ----不建議(本地調試可用)

缺點: Dobbo本身提供的進階特性沒用上,自已編寫啟動類可能會有缺陷

3、Dubbo架構提供Main方法類運作(Spring容器)----推薦使用

優點:架構本身提供(com.alibaba.dubbo.container.Main)

可實作優雅關機 dubbo.shutdown.hook

消費者:停止時,不再發送新的請求,所有新的調用在用戶端發生報錯。然後檢查有沒有請求響應沒有傳回,若有等待傳回。除非逾時,則強制關閉。

生産者:停止時,先标記為不接收新請求,新請求過來時報錯,讓消費者嘗試其他機器。然後檢測線程池中,是否有其他線程正在運作,如果有等待線程執行完畢。除非逾時,則強制關閉。

二、修改spring-context.xml

<import resource="spring-mybatis.xml" />
<import resource="dubbo-provider.xml" />
           

改為

<import resource="classpath:spring/spring-mybatis.xml" />
<import resource="classpath:spring/dubbo-provider.xml" />
           

三、修改服務pom.xml檔案

在pom.xml 中加入build

<build>
		<finalName>edu-service-user</finalName>
 
		<resources>
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
			<!-- 結合com.alibaba.dubbo.container.Main -->
			<resource>
				<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
				<directory>src/main/resources/spring</directory>
				<filtering>true</filtering>
				<includes>
					<include>spring-context.xml</include>
				</includes>
			</resource>
		</resources>
		
		<!-- <pluginManagement>
			<plugins>
				解決Maven插件在Eclipse内執行了一系列的生命周期引起沖突
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.apache.maven.plugins</groupId>
										<artifactId>maven-dependency-plugin</artifactId>
										
										<goals>
											<goal>copy-dependencies</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement> -->
		<plugins>
		<!-- 	打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<classesDirectory>target/classes/</classesDirectory>
					<archive>
						<manifest>
							<mainClass>com.alibaba.dubbo.container.Main</mainClass>
							<!-- 打包時 MANIFEST.MF檔案不記錄的時間戳版本 -->
							<useUniqueVersions>false</useUniqueVersions>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<manifestEntries>
							<Class-Path>.</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<type>jar</type>
							<includeTypes>jar</includeTypes>
							<useUniqueVersions>false</useUniqueVersions>
							<outputDirectory>
								${project.build.directory}/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
 
</build>

           

四、打包

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

打包的時候,要根據項目依賴,逐層打包。

最終提供服務的項目為edu-service-user

他所依賴edu-facade-user、edu-common-parent

是以要先将edu-common-parent和edu-facade-user 打包到本地資源庫

~~windows–>preferences–>maven–>uesrsettings

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

4.1 右擊edu-common-parent 項目的pom.xml run as–>maven install

4.2 右擊edu-facade-user 項目的pom.xml run as–>maven install

4.3 右擊edu-service-user 項目的pom.xml run as–>maven install

打包完成 檢視target包,生成lib和edu-service-user.jar 成功

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

五、運作

找到工作空間edu-service-user 項目的 target 将lib和edu-service-user.jar 複制到C:\usertest下,cmd進入。

輸入指令 java -jar edu-service-user.jar & (啟動之前不要忘記啟動zookeper)

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

六、檢視結果

dubbo —— 使用maven建構dubbo服務可執行jar包一、dubbo服務啟動方式分析二、修改spring-context.xml三、修改服務pom.xml檔案四、打包五、運作六、檢視結果備注

備注

安裝dubbo管控台連結:https://blog.csdn.net/han_xiaoxue/article/details/87921126

安裝zookeper連結:https://blog.csdn.net/han_xiaoxue/article/details/86747509

此次應用項目連結:https://download.csdn.net/download/han_xiaoxue/10957427

項目介紹:https://blog.csdn.net/han_xiaoxue/article/details/87281795