Dubbo(五) 聚合工程之服务消费者Demo



消费者项目结构

POM依赖


<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>
 <parent>
	  <groupId>com.common.parent</groupId>
	  <artifactId>common-parent</artifactId>
	  <version>1.0.0-SNAPSHOT</version>
  </parent>
  <groupId>com.lsq.manage.web</groupId>
  <artifactId>lsq-manage-web</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <name>lsq-manage-web</name>
  <url>http://maven.apache.org</url>
 <dependencies>
 	<!-- 导入dubbo接口依赖 -->
  	<dependency>
  		<groupId>com.lsq.manage.api</groupId>
	    <artifactId>lsq-manage-api</artifactId>
	    <version>1.0.0-SNAPSHOT</version>
  	</dependency>
  	<!-- jackSon pringMVC 使用responseBody注解 -->
	<!-- Jackson Json处理工具包 -->
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
	</dependency>
  	
  	<!-- jsp相关 -->
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<scope>provided</scope>
	</dependency>
	<!-- cglib -->
	<dependency>
		<groupId>org.glassfish.hk2.external</groupId>
		<artifactId>cglib</artifactId>
	</dependency>
	
	<!-- dubbo 需要的jar start -->
	<dependency>
		<groupId>org.jboss.netty</groupId>
		<artifactId>netty</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.springframework</groupId>
				<artifactId>spring</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

	<!-- Zookeeper 用于分布式服务管理 -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
	</dependency>
	<dependency>
		<groupId>com.101tec</groupId>
		<artifactId>zkclient</artifactId>
	</dependency>
	<!-- Zookeeper 用于分布式服务管理 end -->
	<!-- dubbo 需要的jar end -->
  </dependencies>

	<build>
		<finalName>lsq-manage-web</finalName>
		<resources>
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources>
	</build>
</project>

dubbo-consumer.xml


<?xml version="1.0" encoding="GBK"?>
<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="manage-web"/>

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.159.132:2181"/>
</beans>  


spring-mvc.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
						http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd	
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://code.alibabatech.com/schema/dubbo  
        				http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	  <!--   
        自动扫描controller包下的所有类,如果@Controller注入为bean  
        并将bean依赖注入,即dubbo服务端暴露的Service注入到Controller中  
    --> 
	<!-- dubbo消费者的扫描包,多个包使用逗号隔开 -->
	<dubbo:annotation package="com.lsq.manage.controller" />
	<!-- 注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven> 
	
	<!--cglib-->
	<aop:aspectj-autoproxy proxy-target-class="true"/>
	
	<!-- 扫描 -->
	<context:component-scan base-package="com.lsq.manage.controller" />
	
	<!-- 配置SpringMVC的视图解析器 -->
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="WEB-INF/jsp"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 自定义异常处理-->
	<bean id="exceptionResolver" class="com.lsq.manage.exception.reslover.MyExceptionResolver"></bean> 
	
	
</beans>


spring-context.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
						http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd	
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.2.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<import resource="classpath:spring/dubbo-consumer.xml"/> 
	
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	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_2_5.xsd">
	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/spring-context.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 解决乱码问题 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置springMVC -->
	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<!-- 伪静态 ,SEO搜索引擎优化-->
		<!-- 所有以html结尾的请求都进入springMVC容器 -->
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!-- 设置session失效时间 -->
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

UserController

package com.lsq.manage.controller;

import org.springframework.stereotype.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lsq.manage.controller.base.BaseController;
import com.lsq.manage.service.UserService;

/**
 * User控制器
 * 
 * @author LSQ
 *
 */
@Controller
public class UserController extends BaseController{

	@Reference
	private UserService userService;
}

启动消费者(部署在tomcat中)

执行效果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值