开发步骤:
(1)创建Maven工程(WAR)dubBoxdemo-web ,在pom.xml引入依赖 ,同“dubBoxdemo-service”工程。区别就是把tomcat插件的运行端口改为8082 。
(2)在webapps目录下创建WEB-INF 目录,并创建web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 解决post乱码 --> <filter> <filter-name>CharacterEnCodingFilter</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> <init-param> <param-name>forceEnCoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapPing> <filter-name>CharacterEnCodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapPing> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class> <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> |
(3)拷贝业务接口
将“dubBoxdemo-service”工程的cn.itcast.dubBoxdemo.service 包以及下面的接口拷贝至此工程。
(4)编写Controller
package cn.itcast.dubBoxdemo.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapPing; import org.springframework.web.bind.annotation.ResponseBody; import cn.itcast.dubbodemo.service.UserService; @Controller @RequestMapPing("/user") public class UserController { @Reference private UserService userService; @RequestMapPing("/showname") @ResponseBody public String showname(){ return userService.getname(); } } |
(5)编写spring配置文件
在src/main/resources下创建applicationContext-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven > <mvc:message-converters register-defaults="false"> <bean > <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 引用dubbo 服务 --> <dubbo:application name="dubBoxdemo-web" /> <dubbo:registry address="zookeeper://192.168.25.132:2181"/> <dubbo:annotation package="cn.itcast.dubboxdemo.controller" /> </beans> |
(6)测试运行
tomcat7:run
在浏览器输入http://localhost:8082/user/showname.do,查看浏览器输出结果
总结以上是内存溢出为你收集整理的Dubbox服务的消费方开发全部内容,希望文章能够帮你解决Dubbox服务的消费方开发所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)