首页 > 学院 > 开发设计 > 正文

web项目调用dubbo的provider

2019-11-06 09:55:39
字体:
来源:转载
供稿:网友

上一篇文章中消费者是在Maven项目中调用,接下来,我将使用web项目来调用提供者的接口

首先新建一个web工程(如何创建Maven的web项目自行上网百度),项目结构如下:

首先来看下pom.xml(可以拷贝上一篇文章中消费者的)

<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/maven-v4_0_0.xsd">	<modelVersion>4.0.0</modelVersion>	<groupId>com.wys</groupId>	<artifactId>test</artifactId>	<version>0.0.1-SNAPSHOT</version>	<name>test Maven Webapp</name>	<url>http://maven.apache.org</url>	<properties>		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>		<springversion>3.1.1.RELEASE</springversion>		<junitversion>3.8.1</junitversion>	</properties>	<dependencies>		<!-- 项目依赖 -->		<dependency>			<groupId>test-web</groupId>			<artifactId>test-pubilc-interface</artifactId>			<version>0.0.1-SNAPSHOT</version>		</dependency>		<dependency>			<groupId>junit</groupId>			<artifactId>junit</artifactId>			<version>${junitversion}</version>			<scope>test</scope>		</dependency>		<dependency>			<groupId>com.alibaba</groupId>			<artifactId>dubbo</artifactId>			<version>2.5.3</version>			<exclusions>				<exclusion>					<groupId>org.springframework</groupId>					<artifactId>spring</artifactId>				</exclusion>			</exclusions>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-aop</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-asm</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-aspects</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-beans</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-context</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-context-support</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-core</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-expression</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-jdbc</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-jms</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-orm</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-oxm</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-tx</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-web</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-webmvc</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>org.springframework</groupId>			<artifactId>spring-test</artifactId>			<version>${springversion}</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>javax.servlet</groupId>			<artifactId>jstl</artifactId>			<version>1.2</version>			<type>jar</type>			<scope>compile</scope>		</dependency>		<dependency>			<groupId>commons-collections</groupId>			<artifactId>commons-collections</artifactId>			<version>3.1</version>		</dependency>		<dependency>			<groupId>commons-logging</groupId>			<artifactId>commons-logging</artifactId>			<version>1.1</version>		</dependency>		<dependency>			<groupId>org.apache.zookeeper</groupId>			<artifactId>zookeeper</artifactId>			<version>3.3.3</version>		</dependency>		<dependency>			<groupId>com.github.sgroschupf</groupId>			<artifactId>zkclient</artifactId>			<version>0.1</version>		</dependency>	</dependencies>	<build>		<finalName>tan-springmvc-book</finalName>	</build></project>applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"	xsi:schemaLocation="http://www.springframework.org/schema/aop 		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 		http://www.springframework.org/schema/beans 		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 		http://www.springframework.org/schema/context 		http://www.springframework.org/schema/context/spring-context-3.0.xsd 		http://www.springframework.org/schema/mvc 		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 		http://www.springframework.org/schema/tx 		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd		http://code.alibabatech.com/schema/dubbo		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">				<mvc:annotation-driven />		<context:annotation-config />		<dubbo:annotation package="com.cl.user.*"/>		<context:component-scan base-package="com.tan.*" />		<dubbo:application name="hehe_consumer" />		<dubbo:registry address="zookeeper://172.30.9.173:2181" />		<dubbo:reference id="userService" interface="com.cl.user.servicei.UserService" />				<bean			class="org.springframework.web.servlet.view.InternalResourceViewResolver">			<property name="prefix" value="/" />			<property name="suffix" value=".jsp" />		</bean></beans>log4j.properties

log4j.rootLogger=DEBUG,CONSOLE,Alog4j.addivity.org.apache=falselog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Threshold=DEBUGlog4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH/:mm/:ss} -%-4r [%t] %-5p  %x - %m%nlog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.Encoding=UTF-8log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.A=org.apache.log4j.DailyRollingFileAppender  log4j.appender.A.File=${catalina.home}/logs/FH_log/PurePro_log4j.appender.A.DatePattern=yyyy-MM-dd'.log'log4j.appender.A.layout=org.apache.log4j.PatternLayout  log4j.appender.A.layout.ConversionPattern=[FH_sys]  %d{yyyy-MM-dd HH/:mm/:ss} %5p %c{1}/:%L /: %m%nlog4j.logger.com.ibatis=debuglog4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debuglog4j.logger.com.ibatis.common.jdbc.ScriptRunner=debuglog4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debuglog4j.logger.java.sql.Connection=debuglog4j.logger.java.sql.Statement=debuglog4j.logger.java.sql.PreparedStatement=debug,stdoutweb.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">	<display-name></display-name>	<welcome-file-list>		<welcome-file>index.jsp</welcome-file>	</welcome-file-list>			<!-- Spring的log4j监听器 -->	<context-param>	    <param-name>log4jConfigLocation</param-name>	    <param-value>/WEB-INF/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>	<!-- 核心控制器 -->	<servlet>		<servlet-name>book</servlet-name>		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>		<init-param>			<param-name>contextConfigLocation</param-name>			<param-value>/WEB-INF/applicationContext.xml</param-value>		</init-param>		<load-on-startup>1</load-on-startup>	</servlet>		<servlet-mapping>		<servlet-name>book</servlet-name>		<url-pattern>/</url-pattern>	</servlet-mapping>	<servlet-mapping>		<servlet-name>book</servlet-name>		<url-pattern>*.do</url-pattern>	</servlet-mapping></web-app>MyController.java

package com.tan.controller;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.cl.user.servicei.UserService;@Controllerpublic class MyController {	@Resource(name="userService")	private UserService userService;	@RequestMapping(method=RequestMethod.GET, value="/test/{id}",produces="text/plain;charset=UTF-8")	@ResponseBody	public String say(@PathVariable String id){		return userService.sayHello("4");	}			@RequestMapping(method=RequestMethod.GET, value="/test1/{id}",produces="text/plain;charset=UTF-8")	@ResponseBody	public String test(@PathVariable String id){		return userService.test(2,3);	}		@RequestMapping(method=RequestMethod.GET, value="/test2",produces="text/plain;charset=UTF-8")	@ResponseBody	public String test2(){		return userService.test2(); 	}		@RequestMapping("login.do")	@ResponseBody        public ModelAndView login(String username,String passWord){		String result=userService.login(username, password);		System.out.println("result===>"+result);                return new ModelAndView(result,"username",username);    }    }接下去在将项目部署在Tomcat下,启动,打开浏览器输入网址:http://localhost:8080/tan-springmvc-book/test/1   就可以看到provider返回的数据:

provider接口实现:

这里输入的网址还可以根据MyController中不同方法进行不同调用,如还可以访问:http://localhost:8080/tan-springmvc-book/test1/1  

好了 ,本次就讲到这里,这篇是基于上一篇的改造,比较简单,所以就直接贴出代码,具体可以下载源码在研究研究

源码下载:http://pan.baidu.com/s/1pLg4ivx  密码:a05p


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表