Dubbo服务化最优实现(改造直连方式)
Dubbo服务化最优实现
由于上篇dubbo直连方式文章直接提供了服务提供者的jar包,导致在服务消费者的工程中可以直接使用服务接口实现类.为了避免这样的情况发生,dubbo官方推荐远程调用RPC服务以接口为粒度,为开发者屏蔽远程调用底层细节.必须有一个接口工程.它是一个maven java工程,对外提供服务的接口.要求接口工程里存放的内容如下:
1.对外暴露的服务接口(service接口)
2.实体bean对象
提示:由于是改造,大部分代码可以复制之前写好的。当然我在这里又写了一边,可以参考下面的步骤
1、创建接口工程
1.1、创建maven Java 工程
1.2、创建实体类和服务接口
实体类
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
setter and getter方法;
服务接口
public interface StudentService {
//通过id查询学生
Student QueryById(Integer id);
}
2、创建服务提供者工程
2.1创建maven web工程
2.2、整理pom文件,添加依赖(spring,dubbo,接口工程)和jdk1.8编译插件
<?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.why</groupId>
<artifactId>ch03-service-provide</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.why</groupId>
<artifactId>ch05-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>
2.3、创建服务接口实现类
/*
这里只是为了模拟dubbo的直连方式,
为了方便就省略了dao层
用student对象来模拟dao层返回的数据
@Autowired
StudentDao studentDao;
*/
@Override
public Student QueryById(Integer id) {
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
return student;
}
2.4、创建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服务提供者名称:保证唯一-->
<!--选择dubbo.apache.org的那个-->
<dubbo:application name="StudentService-provide"/>
<!--声明dubbo使用的协议和端口,dubbo官方推荐使用的协议为dubbo,端口号默认为20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务(直连方式)-->
<!--
暴露服务标签<dubbo:service>
interface:暴露的服务接口的全限定类名
ref:接口引用的实现类在spring中的标识
registry:如果不使用注册中心(使用直连方式),则值为N/A
-->
<dubbo:service interface="com.why.service.StudentService" ref="StudentService" registry="N/A"/>
<bean id="StudentService" class="com.why.service.Imp.StudentServiceImp"/>
</beans>
2.5、web.xml中创建监听器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-service-provide.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.6、配置Tomcat修改端口
3、创建服务消费者工程
3.1创建maven web工程
3.2整理pom文件,添加依赖(spring,dubbo,接口工程)和jdk1.8编译插件(此时就不需要服务提供者的jar包了)
<?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.why</groupId>
<artifactId>ch05-service-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.why</groupId>
<artifactId>ch05-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>
3.3、创建dubbo核心配置文件和springmvc配置文件(spring的xml配置文件)
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服务提供者名称:保证唯一-->
<!--选择dubbo.apache.org的那个-->
<dubbo:application name="ch05-StudentService-consumer"/>
<!--
引用远程服务接口<dubbo:reference>标签
id;远程服务接口对象名称
interface:调用远程服务接口的全限定名称
url:访问服务接口的地址 dubbo规定用dubbo://+ip地址+端口号
registey:直连N/A
-->
<dubbo:reference id="userService" interface="com.why.service.StudentService" url="dubbo://localhost: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置组件扫描器-->
<context:component-scan base-package="com.why.controller"/>
<!--配置注解驱动-->
<!--选mvc结尾的-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.4、编写controller
@Controller
public class MyController {
@Resource
StudentService studentService;
@RequestMapping("/student")
public String studnetQuery(Model model, Integer id){
Student student = studentService.QueryById(id);
model.addAttribute("student",student);
return "showStudent";
}
}
3.5、配置中央调度器
<display-name>Archetype Created Web Application</display-name>
<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:application.xml,classpath:dubbo-service-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.6、创建jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
学生id=${student.id}<br>
学生name=${student.name}<br>
学生age=${student.age}
</body>
</html>