上一篇是用的xml方式,https://www.cnblogs.com/luoa/articles/9844417.html,这篇改用注解方式。
更改的文件如下:
提供者provider部分
1,springmvc.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:util="http://www.springframework.org/schema/util" 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-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName"> <!--先让spring扫描,再让dubbo扫描 --> <!-- 加入spring注解扫描 --> <context:component-scan base-package="com.*"/> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo_provider" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <!-- <dubbo:service interface="com.luo.DemoService" ref="demoService" /> --> <!-- 使用注解方式暴露接口 --> <dubbo:annotation package="com.luo" /> <aop:aspectj-autoproxy /> <!-- <context:component-scan base-package="com.luo.*" /> --> <!-- <import resource="classpath:dubbo-provider.xml" /> --> </beans>
2,接口实现类DemoServiceImpl:
package com.luo; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; import com.luo.domain.User; //@Service("demoService") @Service //@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0") public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return name; } public List<User> getUser() { List<User> list = new ArrayList<User>(); for (int i = 0; i < 3; i++) { User user = new User(i, "name" + i, i); list.add(user); } return list; } }
消费者部分
1,test-web-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!--先让dubbo扫描controller层,可以避免出现NullPoint空指针问题 --> <dubbo:annotation package="com.aluo.controller" /> <!-- 自动扫描controller包下的所有类,使其认为是spring mvc的控制器 路径即为类路径 --> <context:component-scan base-package="com.aluo.controller"></context:component-scan> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="dubbo_consumer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" /> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 根据控制器返回的字符串拼接成jsp路径:/WEB-INF/jsp/xx.jsp --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- <import resource="classpath:/dubbo-consumer.xml" /> --> </beans>
2,Controller类,@Reference引用服务
package com.aluo.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.alibaba.dubbo.config.annotation.Reference; import com.luo.DemoService; import com.luo.domain.User; @Controller public class HelloController { /* * @Resource(name="demoService") private DemoService demoService; */ @Reference private DemoService demoService; @RequestMapping("/welcome") public String welcome(ModelMap model) { System.out.println("begin"); System.out.println(demoService.getUser()); List<User> list = demoService.getUser(); for (User user : list) { System.out.println(user.getId()); } model.addAttribute("message", demoService.sayHello("hello dubbo")); return "welcome"; } }
运行zookeeper,服务者,tomcat,访问:http://localhost:8080/test-web/welcome,成功,如图:
参考:https://blog.csdn.net/cslucifer/article/details/78608246