Dubbo服务化最佳实战
存放实体bean和业务接口2.2 实现接口中的方法UserserviceImpl.java2.3 服务提供者的核心配置文件声明dubbo服务提供者的名称:保证唯一- 性设置dubbo使用的协议和端口号暴露服务接口加载业务接口的实现类到spring容器中2.4 监听器3. 服务消费者(maven web)3.2 服务消费者的核心配置文件声明服务消费者名称:保证唯一性引用远程接口服务3.3 sprin
·
Dubbo
Dubbo服务化最佳实战
- dubbo官方推荐使用的一个模式,将实体bean和业务接口存放到接口工程中
- 服务的提供者
- 服务的消费者
1. 接口工程(maven jave)
存放实体bean和业务接口
2. 服务提供者 (maven web)
2.1 引入接口工程相关依赖
pom
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guo</groupId>
<artifactId>002-link-userservice-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--spring 依赖 context,webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--引用接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>001-link-interface</artifactId>
<version>1.0-SNAPSHOT</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>
2.2 实现接口中的方法
UserserviceImpl.java
package com.guo.service.impl;
import com.guo.dubbo.model.User;
import com.guo.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
public class Userserviceimpl implements UserService{
@Autowired
private UserService userService;
//根据用户标识获取用户信息
@Override
public User queryUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("zhangsan");
return user;
}
//查询用户总人数
@Autowired
public Integer queryAllUserCount(){
return 23;
}
}
2.3 服务提供者的核心配置文件
- 声明dubbo服务提供者的名称:保证唯一- 性
- 设置dubbo使用的协议和端口号
- 暴露服务接口
- 加载业务接口的实现类到spring容器中
resources/dubbo-userservice-provider.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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务提供者的名称:保证唯一性-->
<dubbo:application name="002-link-userservice-provider"/>
<!--设置dubbo使用的协议和端口号
name:dubbo使用协议的名称
port:dubbo服务的端口号
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务接口-->
<dubbo:service interface="com.guo.dubbo.service.UserService" ref="userservice" registry="N/A"/>
<!--加载业务接口的实现类到spring容器中-->
<bean id="userservice" class="com.guo.service.impl.Userserviceimpl"/>
</beans>
2.4 监听器
webapp/WEB-INF/web.xml
<?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-userservice-provider.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3. 服务消费者(maven web)
3.1 引入接口工程相关依赖
pom
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guo</groupId>
<artifactId>003-llink-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--spring 依赖 context,webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!--dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>001-link-interface</artifactId>
<version>1.0-SNAPSHOT</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>
3.2 服务消费者的核心配置文件
- 声明服务消费者名称:保证唯一性
- 引用远程接口服务
resources/dubbo-consumer.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: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="003-link-consumer"/>
<!--引用远程接口服务-->
<dubbo:reference id="userService"
interface="com.guo.dubbo.service.UserService"
url="dubbo://localhost:20880"
registry="N/A"/>
</beans>
3.3 spring核心配置文件
- 扫描组件
- 配置注解驱动(命名空间容易出错)
- 视图解析器
resources/applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.guo.dubbo.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.4 控制层
UserController.xml
package com.guo.dubbo.controller;
import com.guo.dubbo.model.User;
import com.guo.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.GetMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user")
public String userQueryById(Model model,Integer id){
User user = userService.queryUserById(id);
Integer count = userService.queryAllUserCount();
model.addAttribute("user",user);
model.addAttribute("count",count);
return "index";
}
}
3.5 web.xml中配置中央调度器servlet
webapp/WEB-INF/web.xml
<?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">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:dubbo-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.6 创建页面
webapp/WEB-INF/index.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<title>用户详情</title>
</head>
<body>
<h1>用户详情</h1>
<div>用户标识:${user.id}</div>
<div>用户名称:${user.name}</div>
<div>用户数量:${count}</div>
</body>
</html>
4. 结果预览
面向接口编程,创建一个接口工程把我们公共的实体bean和业务接口都放在工程中,让我们服务的提供者和消费者都去依赖它
更多推荐
已为社区贡献11条内容
所有评论(0)