【dubbo】dubbo服务注册三种方式

dubbo官方提供了三种注册方式。分别是:
1、利用main方法进行服务注册
2、利用tomcat容器
3、利用dubbo官方提提供的com.alibaba.dubbo.container.Main方法
三种方式各有利弊。这篇文章以一个简单的例子来简单的介绍一下这三种注册方式,在看这篇博客前,相信已经熟悉dubbo+zk的架构和项目中成员的组成了。

Main方法

这种方式需要在项目中新建一个类来专门执行main方法,去加载配置文件。所以这种方式更加适合开发阶段的使用,方便开发人员进行单元测试。
public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        context.start();
        System.out.println("启动成功");
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

web容器

这种方式不需要添加任何多余的类或方法,只需要将provider注册服务的配置文件放在web.xml中加载到web容器中即可。
这种方式服务注册的配置文件是支持中文格式的。
但是这种方式增加了tomcat的耦合性,web容器是用来运行web程序的,还需要分出额外的精力去管理dubbo服务注册,
造成管理压力。同时对内存的占用也会加大,影响程序性能。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="appication" version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

利用maven生成jar包通过Java-jar命令注册服务

这种方式可以将需要注册的服务单独放在服务器上进行管理。方便,有利于分布式应用的管理。利于分布式应用服务的扩展。
1、pom文件的配置
<build>

   <resources>
    <resource>
<!--生成的classes文件的路径,此文件夹中包含运行时的所有文件,可以不配置-->        <targetPath>${project.build.directory}/classes</targetPath>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.properties</include>
        </includes>
    </resource>
    <resource>
        <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
        <directory>src/main/resources/spring</directory>
        <filtering>true</filtering>
        <includes>
            <include>applicationContext.xml</include>
        </includes>
    </resource>
    </resources>

    <!--下面的必须配置-->
    <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>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
2、创建服务注册时相应的文件夹
这个是必须要建的,可能是默认生成的路径,如果配置resource后,targetPath也是这个

这里写图片描述

3、利用maven进行打包
install到本地后,会在target中生成:lib和你要打的jar包

这里写图片描述

生成的target目录下的情况:

这里写图片描述

classes是在直接执行com.alibaba.dubbo.container.Main方法时运行时所需要的所有文件.class文件和xml.

4、错误
在target目录下,通过java -jar 命令执行时报错;

这里写图片描述

原因:在配置文件中存在中文导致的。注释中的中文也是不可以的。将中文注释删掉后,执行成功!

这里写图片描述
这里写图片描述

查看效果:

这里写图片描述

最后,利用这三种注册方式的就都完成了。
还想让大家看的一个效果;

这里写图片描述

发现同一个ip的同一个端口注册一次就不允许二次注册服务了。这个端口号也是我们自定义的来管理我们的分布式服务的!
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值