接上文
--------------------------------------------------------------------------------------------------------------------------------
步骤:
1、创建maven工程:
pom.xml文件内容同上文,只不过将tomcat的端口号改下;
2、编写web.xml文件:
同上文
3、将上文创建的interface,连同他的包一同复制在我们的src/main/java下
4、创建class作为我们的handler
说明:1、@RestController注解相当于@Controller和@ResponseBody的合体
2、@Reference注解为alibaba的,是远程调用标签
package com.aynu.dubbo.handler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aynu.dubbo.service.IDubboTest;
@RestController
@RequestMapping("/test")
public class DubboHanlder {
@Reference
private IDubboTest service;
@RequestMapping("/dubbo.do")
public void dubbo() {
String DUBBO = service.sayHello("DUBBO");
System.out.println(DUBBO);
}
}
5、创建spring-handler.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:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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">
<!-- 注册mvc解析器 -->
<mvc:annotation-driven/>
<!-- 为当前应用取名字,可以随意,建议为项目名 -->
<dubbo:application name="dubbo_sonmer"/>
<!-- 连接远程zookeeper -->
<dubbo:registry address="zookeeper://192.168.146.128:2181"/>
<!-- dubbo包扫描器 -->
<dubbo:annotation package="com.aynu.dubbo.handler"/>
</beans>
6、运行(前提是提供方已经运行):
打开管控台,在消费者中可见内容: