dubbo 注解方式整合 springmvc 您所在的位置:网站首页 dubbo注解配置消费者 dubbo 注解方式整合 springmvc

dubbo 注解方式整合 springmvc

2024-05-08 22:50| 来源: 网络整理| 查看: 265

1. 项目结构

创建一个 maven 多模块项目,结构如下:

1234567dubbo-annotation-with-springmvc-sample(父模块)||__ user-module-api(服务接口模块)||__ user-module-provider(服务提供者)||__ user-module-consumer(服务消费者) 1.1 父模块项目

dubbo-annotation-with-springmvc-sample/pom.xml 配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 4.0.0 org.fanlychie dubbo-annotation-with-springmvc-sample 0.0.1-SNAPSHOT pom dubbo-annotation-with-springmvc-sample Sample project for Dubbo http://maven.apache.org user-module-api user-module-provider user-module-consumer 2.5.3 4.3.7.RELEASE org.fanlychie user-module-api ${project.version} com.alibaba dubbo org.springframework spring ${dubbo.version} org.springframework spring-context ${spring.version} org.springframework spring-webmvc ${spring.version} com.fasterxml.jackson.core jackson-databind 2.8.8 com.101tec zkclient 0.10 1.2 服务接口模块

user-module-api/pom.xml 配置如下:

12345678910111213 4.0.0 org.fanlychie dubbo-annotation-with-springmvc-sample 0.0.1-SNAPSHOT user-module-api jar user-module-api http://maven.apache.org

编写注册用户的示例服务接口:

1234567package org.fanlychie.service; public interface UserService { void register(String username, String password); } 1.3 服务提供者

user-module-provider/pom.xml 配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 4.0.0 org.fanlychie dubbo-annotation-with-springmvc-sample 0.0.1-SNAPSHOT user-module-provider jar user-module-provider http://maven.apache.org org.fanlychie user-module-api com.alibaba dubbo org.springframework spring-context com.101tec zkclient org.apache.maven.plugins maven-assembly-plugin 2.6 make-assembly package single com.alibaba.dubbo.container.Main jar-with-dependencies false

实现服务接口,使用com.alibaba.dubbo.config.annotation.@Service注解暴露服务:

12345678910111213141516package org.fanlychie.service; import com.alibaba.dubbo.config.annotation.Service; @Servicepublic class UserServiceImpl implements UserService { @Override public void register(String username, String password) { System.out.println("---------------------------------------------------------"); System.out.println(String.format("接收到注册用户请求 - {username:%s, password:%s}", username, password)); System.out.println("---------------------------------------------------------"); } }

user-module-provider/src/main/resources/dubbo.properties 配置如下:

1dubbo.spring.config=classpath:spring-dubbo-provider.xml

user-module-provider/src/main/resources/spring-dubbo-provider.xml 配置如下:

12345678910111213141516171819202122

user-module-provider/src/main/resources/log4j.properties 配置如下:

12345log4j.rootCategory = INFO, consolelog4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.console.target = System.outlog4j.appender.console.layout = org.apache.log4j.PatternLayoutlog4j.appender.console.layout.conversionPattern = %d{yyyy-MM-dd HH:mm:ss:SSS} [%t] %-5p [%c{1}:%L] - %m%n 1.4 服务消费者

user-module-consumer/pom.xml 配置如下:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 4.0.0 org.fanlychie dubbo-annotation-with-springmvc-sample 0.0.1-SNAPSHOT user-module-consumer war user-module-consumer http://maven.apache.org org.fanlychie user-module-api com.alibaba dubbo org.springframework spring-webmvc com.fasterxml.jackson.core jackson-databind com.101tec zkclient autosellrobot-wechat org.apache.tomcat.maven tomcat7-maven-plugin / 8080 UTF-8 http://localhost:8080/manager/html

服务消费者使用com.alibaba.dubbo.config.annotation.@Reference注解引用接口服务:

12345678910111213141516171819202122import com.alibaba.dubbo.config.annotation.Reference;import org.fanlychie.service.UserService;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class UserController { @Reference private UserService userService; @GetMapping("/user/register") public String register(String username, String password) { if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) { return "用户名或密码不能为空"; } userService.register(username, password); return "注册完成"; } }

user-module-consumer/src/main/webapp/WEB-INF/web.xml 配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 contextConfigLocation classpath:spring-context.xml org.springframework.web.util.IntrospectorCleanupListener org.springframework.web.context.request.RequestContextListener org.springframework.web.context.ContextLoaderListener encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /* dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 dispatcherServlet /

user-module-consumer/src/main/resources/log4j.properties 配置如下:

12345log4j.rootCategory = INFO, consolelog4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.console.target = System.outlog4j.appender.console.layout = org.apache.log4j.PatternLayoutlog4j.appender.console.layout.conversionPattern = %d{yyyy-MM-dd HH:mm:ss:SSS} [%t] %-5p [%c{1}:%L] - %m%n

user-module-consumer/src/main/resources/spring-context.xml 配置如下:

1234567891011121314

user-module-consumer/src/main/resources/spring-mvc.xml 配置如下:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 text/html;charset=utf-8 application/xml;charset=utf-8 application/json;charset=utf-8 text/html;charset=utf-8 application/xml;charset=utf-8 application/json;charset=utf-8

服务消费者方 dubbo 注解扫描配置的信息不能独立出 springmvc 配置文件,否则@Reference注解引用的接口实例会出现 Null 的状况。

示例项目开发环境:Java-8、Maven-3、IntelliJ IDEA-2017、Spring-4.7、Dubbo-2.5.3完整示例项目链接:dubbo-annotation-with-springmvc-sample参考文档文献链接:dubbo用户指南



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有