将 dubbo provider 打包成 jar 包

来源(黑客派)

From: https://hacpai.com/article/1490662913058

将 dubbo provider 打包成 jar 包

本贴最后更新于 1225 天前,其中的信息可能已经时移世改

在使用dubbo框架搭建集群时,如果我们将dubbo打包成可运行的jar包,就能很方便的构架起微服务集群 本文将示例使用maven-shade-plugin插件,将dubbo provider项目打包成一个整体的可运行jar包

1.使用maven创建一个新dubbo provider项目

Eclipse中,点File->New Project...菜单,选择Maven Project,创建artifactId为dubbo-provider-to-jar的项目,修改pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>asm32.net.dubbo.provider</groupId>
	<artifactId>dubbo-provider-to-jar-demo-1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties> <!-- spring版本号 -->
		<spring.version>4.3.7.RELEASE</spring.version>
	</properties>
	<dependencies> <!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency> <!-- dubbo依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.10</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>${artifactId}-${version}</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin> <!-- 打成jar包 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.4.1</version>
				<configuration> <!-- 是否生成缩减的pom文件,默认不配置是true -->
					<createDependencyReducedPom>false</createDependencyReducedPom>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 启动类 -->
									<mainClass>asm32.net.dubbo.provider.Main</mainClass>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project> 


2.新建启动入口程序mytest.dubbo.Main,内容如下:

package asm32.net.dubbo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	private static boolean running = true;

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:dubbo-provider.xml" });
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				context.stop();
				System.out.println("dubbo service has stop");
				synchronized (Main.class) {
					running = false;
					Main.class.notify();
				}
			}
		});
		context.start();
		System.out.println("dubbo service has start");
		synchronized (Main.class) {
			while (running) {
				try {
					Main.class.wait();
				} catch (Throwable e) {
				}
			}
		}
	}
}


3.src/main/resources下新建dubbo-provider.xml配置,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="dubbo-service" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" /> <!-- <dubbo:protocol name="rmi" codec="spring"/> --> <!-- 声明需要暴露的服务接口 -->
	<dubbo:service
		interface="asm32.net.dubbo.provider.service.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 -->
	<bean id="demoService"
		class="asm32.net.dubbo.provider.service.impl.DemoServiceImpl" />
</beans>


4.创建对外提供服务接口及实现类,内容如下:

package asm32.net.dubbo.provider.service;

public interface DemoService {
	String SayHello(String name);
}



package asm32.net.dubbo.provider.service.impl;

import asm32.net.dubbo.provider.service.DemoService;

public class DemoServiceImpl implements DemoService {
	public String SayHello(String name) {
		return "Hello " + name;
	}
}


5.使用maven打包项目:

Eclipse项目上点Run As -> Maven build ,Goals栏填package,编译完成后,将在target目录中看到两个jar包及lib目录: dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar 为包含了第三方类库的完整jar包,可单独部署 original-dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar 为不包含第三方类库的jar包,结合lib目录可部署。

cd到target目录,执行java -jar dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar,就能看到已经能成功运行了。

Link: http://www.asm32.net/article_details.aspx?id=7379


浏览次数 0 发布时间 2020-08-04 12:00:18 从属分类 Java 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1