Java之品优购课程讲义_day01(5)

3.2.1 服务消费者开发
开发步骤:
(1)创建 Maven 工程(WAR)dubboxdemo-web ,在 pom.xml 引入依赖 ,同“dubboxdemo-
service”工程。区别就是把 tomcat 插件的运行端口改为 8082 。
(2)在 webapps 目录下创建 WEB-INF 目录,并创建 web.xml

Java之品优购课程讲义_day01(5)
Java之品优购课程讲义_day01(5)
(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 [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]

[url]http://www.springframework.org/schema/mvc[/url] [url]http://www.springframework.org/schema/mvc/spring-mvc.xsd[/url]

[url]http://code.alibabatech.com/schema/dubbo[/url] [url]http://code.alibabatech.com/schema/dubbo/dubbo.xsd[/url]

[url]http://www.springframework.org/schema/context[/url] [url]http://www.springframework.org/schema/context/spring-context.xsd[/url]">

<mvc:annotation-driven  >

<mvc:message-converters  register-defaults="false">

<bean class="org.springframework.http.converter.StringHttpMessageConverter">

<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,查看浏览器输出结果

猜你喜欢

转载自blog.51cto.com/13517854/2149948