阿里dubbo工程的搭建

由于对spring不是很熟悉,在搭建dubbo时折腾了一段时间。故在此将搭建过程分享一下,互相学习。

一下主要是在搭建过程中遇到的问题。

首先构建provider的maven工程。在pom.xml中,添加以下内容

1.dependencies


		<dependency><!-- spring依赖包 -->
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.5.RELEASE</version>
		</dependency>

		<dependency><!-- 阿里的dubbo的依赖包 -->
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.4.9</version>
		</dependency>

		<dependency><!-- zookeeper依赖包 -->
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.3</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency><!-- zookeeper客户端调用的包 -->
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

		<dependency>
			<groupId>com.netflix.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>1.1.16</version>
		</dependency>

2.maven打包的内容

<build><!-- maven打包 -->
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>zju.cs.lzm.main.Main</mainClass><!-- 外接调用主类的路径 -->
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
					</archive>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.5.1</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>
								${project.build.directory}/lib/
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>



将pom.xml构建完毕后,需要在工程的resources目录下创建provider.xml文件,作为spring的配置文件。

具体内容在其他的很多教程页面里都有,这里不再赘述。下面是几个注意事项:

1.在provider.xml文件中,会出现阿里的schema无法获得的错误。如:

Multiple annotations found at this line:
	- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'.
	- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) 
	 could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
这时,可以在Eclipse中,打开window->preferences->validation,将其中的与XML相关的项设置为未选中。


2.在进行打包提交时,最好使用maven进行打包,上面已经提供了maven打包的代码内容。然后将lib目录也要拷贝到和执行的jar相同的目录下。如果使用fatjar进行打包的话,可能会出现xml不匹配的异常。


3.如果在这个过程中,工程内部是没有错误显示的,但是工程名称上却又红叉,可以在下面的problems栏中,右击每个error,进行quick fix。


4.provider和consumer使用了同一个接口。注意,不仅要这个接口的名称和内容相同,接口的路径也要相同。因为在provider.xml和consumer.xml中,要进行接口暴露的,首先他们俩直接要配对上;同时路径还要符合自己的目录,以允许spring进行依赖注入。


5。两者的application name不要相同,他们均可以在dubbo服务器端的监控中心进行查看。


6.在provider端,若需要调用本地已经实例化了的一个单例对象,则可以这样做

首先,在xml中配置这个单例bean

<!-- 对于singleton模式,则加载spring配置文件的时候就进行了bean的初始化。而对于prototype模式,则是在进行getBean()方法时才进行实例化 -->
	<bean id="IPTransformer" class="zju.cs.lzm.avltree.IPTransformer" scope="singleton"/>
然后,
在共同接口的实现类里,创建一个属性,如

private IPTransformer ipt = null;

	public IPTransformer getIpt() {
		return ipt;
	}

	public void setIpt(IPTransformer ipt) {
		this.ipt = ipt;
	}

需要同时设置getset方法。

然后,在xml中进行配置

<!-- 和本地bean一样实现服务。使用实现类,但是id是接口的名称 -->
	<bean id="IPLibrary" class="zju.cs.lzm.main.IPLibraryImpl" >
		<property name="ipt" ref="IPTransformer"></property><!-- 为这个类依赖注入一个IPTransformer类型的名称为ipt的对象 -->
	</bean>

则,这样会在spring对象创建的时候,自动创建IPTransformer的一个单例对象,然后将这个单例对象以ipt为名称被注入到类IPLibrary中。就可以在IPLibrary中进行调用了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值