【Dubbo 03: 直连式 + 接口工程】
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-link-userservice-provider.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
package com.example.dubbo.service.impl;import com.example.dubbo.model.User;import com.example.dubbo.service.UserService;public class userServiceImpl implements UserService {@Overridepublic User queryUserById(String id) {User user = new User();user.setId(id);user.setName("橘子");user.setAge("18");return user;}@Overridepublic int queryAllUserCount() {return 3;}}
- 结构
文章插图
- pom文件:要引入接口工程的依赖,知道可以申请哪些服务
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example.dubbo</groupId><artifactId>o5-link-consumer</artifactId><packaging>war</packaging><version>1.0.0</version><dependencies><!--Spring依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.16.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.16.RELEASE</version></dependency><!--dubbo依赖--><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.2</version></dependency><!-- 接口工程 --><dependency><groupId>com.example.dubbo</groupId><artifactId>o3-link-interface</artifactId><version>1.0.0</version></dependency></dependencies><build><plugins><!--JDK1.8编译插件--><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>
- dubbo里消费者的配置文件
<?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:dubbo="http://dubbo.apache.org/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"><!-- 消费者标识 --><dubbo:application name="o5-link-consumer"/><!-- 引用的远程服务 --><dubbo:reference id="userService" interface="com.example.dubbo.service.UserService" url="dubbo://127.0.0.1:20880" registry="N/A"/></beans>
- spring核心配置文件
<?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"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"><!-- 扫描组件 --><context:component-scan base-package="com.example.dubbo.web.controller"/><!-- 注解驱动 --><mvc:annotation-driven/><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="https://www.huyubaike.com/"/><property name="suffix" value="https://www.huyubaike.com/biancheng/.jsp"/></bean></beans>
- Controller层
package com.example.dubbo.web.controller;import com.example.dubbo.model.User;import com.example.dubbo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class UserController {@AutowiredUserService userService;/*** 响应前端请求,返回用用户详细信息以及总的用户个数*/@RequestMapping("/getUserDetail.do")public String getUserDetail(String id, Model model){//获取数据User user = userService.queryUserById(id);int userCount = userService.queryAllUserCount();//存放数据model.addAttribute("user", user);model.addAttribute("userCount", userCount);//跳转到用户详情页面return "userDetail";}}
推荐阅读
- 怎么在线预览.doc,.docx,.ofd,.pdf,.wps,.cad文件以及Office文档的在线解析方式。
- 含开启测试模式失败的解决办法 Windows开启关闭测试模式的方法
- 原神虚构的凯旋式成就怎么达成
- 如何实现通过Leaflet加载dwg格式的CAD图
- Dubbo2.7详解
- 细聊.Net Core中IServiceScope的工作方式
- 通过openlayers加载dwg格式的CAD图并与互联网地图叠加
- 阴阳师超鬼王活动式神怎么选择
- 阴阳师超鬼王活动式神选择怎么推荐
- 洛克王国星结豹获取方式是什么