dubbo搭建-客户端开发(三)

接着上一篇《dubbo搭建-服务端开发(二)》,这篇是dubbo客户端开发简单的例子。这个例子中,dubbo客户端是一个springMVC项目,所以要创建一个web工程。


代码结构

如图中的dubbo_common_front工程

pom.xml说明

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.plg.dubbo</groupId>
    <artifactId>dubbo_common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dubbo_common_front</artifactId>
  <packaging>war</packaging>
  <name>dubbo前置</name>
  <url>http://maven.apache.org</url>
  <dependencies>
		<dependency>
			<groupId>com.plg.dubbo</groupId>
			<artifactId>dubbo_common_skeleton</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>

		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-instrument</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
		</dependency>
		<!-- spring -->

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>

		<!-- json -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>
  </dependencies>
</project>

引用的包和dubbo服务的的区别主要在于支持springMVC的jar了。


web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Archetype Created Web Application</display-name>
  
	<!-- spring context listener -->
	<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>

	<!-- Spring MVC的核心是DispatcherServlet,这个servlet充当Spring MVC的前端控制器 <servlet-name>spring</servlet-name>一定要spring.servlet.xml配置文件 -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
  
</web-app>
web.xml中定义spring监听和springMVC的配置文件。

spring的配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

	<import resource="classpath:beans/beans-*.xml" />
	
	<context:component-scan base-package="com.plg.dubbo">
		<!-- 由于Spring MVC注脚方式service和controller的扫描顺序问题,在主容器中(applicationContext.xml),将Controller的注解排除掉 -->
		<context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<!-- 激活自动代理功能 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>

dubbo客户端配置文件beans-dubbo.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_common_front" owner="author" />
    <!-- 使用zookeeper注册中心,获取服务地址 -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://xxx.xxx.xxx.xxx:2181" check="false" />
	  
    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
  	<dubbo:reference interface="com.plg.dubbo.common.service.ISimple01Service" id="simple01Service" />
  	<dubbo:reference interface="com.plg.dubbo.common.service.ISimple02Service" id="simple02Service" />
</beans>

确保address配置的zookeeper地址和端口正确,并启动。

启动dubbo_common_front

首先启动dubbo服务,确保启动成功,如下如,服务已经注册


启动dubbo客户端dubbo_common_front:

看到服务列表右边的状态是【正常】状态,那就说明客户端已经正常启动,并且已经发现dubbo服务了。

整个工程的源码:https://git.oschina.net/plg17/plg-dubbo













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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值