【dubbo】使用Dubbo框架提供的Main方法类来运行dubbo服务——服务容器

我们在dubbo的框架使用中,不能不提的就是如何运行我们的dubbo服务的jar包。比如provider的jar等。
根据现有经验,dubbo服务的运行方式有三种。

1.Dubbo服务的运行方式:

1、使用Servlet容器运行(Tomcat、Jetty等)----不可取
缺点:
增加复杂性(端口、管理)
浪费资源(内存)
2、自建Main方法类来运行(Spring容器) ----不建议(本地调试可用)
缺点:
Dobbo本身提供的高级特性没用上 自已编写启动类可能会有缺陷
3、使用Dubbo框架提供的Main方法类来运行(Spring容器)----建议使用
优点:
框架本身提供(com.alibaba.dubbo.container.Main)
可实现优雅关机(ShutdownHook) 详见(http://dubbo.apache.org/zh-cn/docs/user/demos/graceful-shutdown.html)

2.自建Main方法类来运行

对于这样的方式,我们主要在调试过程中使用。只需要写个测试类就可以了。

package dubbo.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @描述: 启动Dubbo服务用的MainClass.
 * @作者: 程金鹏
 * @创建时间: 2019年8月1日21:16:25
 * @版本: 1.0 .
 */
public class DubboProvider {
	
	private static final Log log = LogFactory.getLog(DubboProvider.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("== DubboProvider context start error:",e);
		}
		synchronized (DubboProvider.class) {
			while (true) {
				try {
					DubboProvider.class.wait();
				} catch (InterruptedException e) {
					log.error("== synchronized error:",e);
				}
			}
		}
	}
    
}

3. 使用Dubbo框架提供的Main方法类来运行

今天我们主要来说这种方式。

a 什么是服务容器

详见:http://dubbo.apache.org/zh-cn/docs/user/demos/service-container.html
服务容器是一个 standalone 的启动程序,因为后台服务不需要 Tomcat 或 JBoss 等 Web 容器的功能,如果硬要用 Web 容器去加载服务提供方,增加复杂性,也浪费资源。

服务容器只是一个简单的 Main 方法,并加载一个简单的 Spring 容器,用于暴露服务。

服务容器的加载内容可以扩展,内置了 spring, jetty, log4j 等加载,可通过容器扩展点进行扩展。配置配在 java 命令的 -D 参数或者 dubbo.properties 中。

b 具体配置方式:

在需要配置的服务模块中的pom中增加如下代码:

 <build>
        <finalName>edu-service-userfinalName>

        <resources>
            <resource>
                <targetPath>${project.build.directory}/classestargetPath>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
            
            <resource>
                <targetPath>${project.build.directory}/classes/META-INF/springtargetPath>
                <directory>src/main/resources/springdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>spring-context.xmlinclude>
                includes>
            resource>
        resources>

        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.eclipse.m2egroupId>
                    <artifactId>lifecycle-mappingartifactId>
                    <version>1.0.0version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.pluginsgroupId>
                                        <artifactId>maven-dependency-pluginartifactId>
                                        <versionRange>[2.0,)versionRange>
                                        <goals>
                                            <goal>copy-dependenciesgoal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                            pluginExecutions>
                        lifecycleMappingMetadata>
                    configuration>
                plugin>
            plugins>
        pluginManagement>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    <classesDirectory>target/classes/classesDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.alibaba.dubbo.container.MainmainClass>
                            
                            <useUniqueVersions>falseuseUniqueVersions>
                            <addClasspath>trueaddClasspath>
                            <classpathPrefix>lib/classpathPrefix>
                        manifest>
                        <manifestEntries>
                            <Class-Path>.Class-Path>
                        manifestEntries>
                    archive>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-dependenciesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-dependenciesgoal>
                        goals>
                        <configuration>
                            <type>jartype>
                            <includeTypes>jarincludeTypes>
                            <useUniqueVersions>falseuseUniqueVersions>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            outputDirectory>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>

    build>

这样构建出来的jar 便可以直接使用命令来执行了。

java -jar xxxx.jar

你可能感兴趣的:(Dubbo,Dubbo)