目录
(4)接口工程DubboInteface01中添加业务接口和实体类
(5)提供者DubboProvider01项目需要在pom.xml中引入DubboInteface01工程
(6)提供者DubboProvider01项目实现DubboInteface01中的接口方法
(7)提供者DubboProvider01项目,配置dubbo信息
(8)消费者DubboConsumer01项目需要在pom.xml中引入DubboInteface01工程
(9)消费者DubboConsumer01项目创建视图控制层
(10)消费者DubboConsumer01项目添加dubbo配置
(11)消费者DubboConsumer01项目添加视图jsp页面
(12)为提供者DubboProvider01项目、消费者DubboConsumer01配置tomcat
简述
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
1.Dubbo性能高的原因
高性能要从底层的原理说起,既然是一个RPC框架,主要干的就是远程过程(方法)调用,那么提升性能就要从最关键、最耗时的两个方面入手:序列化和网络通信。
序列化:我们学习java网络开发的时候知道,本地对象要在网络上传输,必须要实现Serializable接口,也就是必须序列化。我们序列化的方案很多:xml、json、二进制流,其中效率最高的就是二进制流(因为计算机就是二进制的)。然而Dubbo采用的就是效率最高的二进制。
网络通信:不同于HTTP需要进行7步走(三次握手和四次挥手),Dubbo采用Socket通信机制,一步到位,提升了通信效率,并且可以建立长连接,不用反复连接,直接传输数据。
2.Dubbo框架
服务容器(Container):服务容器负责启动、加载、运行服务提供者。
服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者(Consumer):调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选一台服务调用。
注册中心(Registry):注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将依据长连接推送变更数据给消费者。
监控中心(Monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心
3.Dubbo支持的协议
支持多种协议:dubbo、http、webservice、memcached、redis等,dubbo官方推荐使用dubbo协议,dubbo协议默认端口20880。
使用dubbo协议,spring配置文件中加入
<dubbo:protocol name="dubbo" port="20880"/>
4.Dubbo直连案例
只有提供者和消费者,不添加注册中心的方式。
4.1服务提供者provider项目:
(1)创建provider提供者maven项目
(2)pom.xml添加需要的jar包
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--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>
(3)开发对外暴露的接口
/**
* 对外暴露的一些接口
*/
public interface SomeService {
String sayHello(String msg);
}
(4)接口的实现类
/**
* 接口实现类
*/
public class SomeServiceImpl implements SomeService {
public String sayHello(String msg) {
return "Hello:"+msg;
}
}
(5)resources目录下添加dubbo的相关spring配置文件dubbo-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="dubbo-provider"/>
<!--指定dubbo服务的协议名称和端口号,指定dubbo,默认端口号20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务dubbo:service
interface:暴露服务的接口全限定类名
ref:引用接口在spring容器中的标识名称
register:注册方式,使用直连方式,不适用注册中心,配置为N/A
-->
<dubbo:service interface="com.qingyun.service.SomeService"
ref="someServiceImpl" registry="N/A"/>
<!--把接口实现类添加到spring容器中-->
<bean id="someServiceImpl" class="com.qingyun.service.impl.SomeServiceImpl"/>
</beans>
(6)在webapp/WEB-INF目录下的web.xml中配置文本加载监听,把配置的dubbo配置文件加载进去
<?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-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4.2服务消费者Consumer:
(1)创建maven项目
(2)pom.xml添加需要的jar包
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--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>
(3)注入provider提供者jar包
maven项目默认打出的是jar包,但是提供者项目DubboProvider中的pom.xml文件中的包方式为war包,需要注释包方式
打开项目的maven管理,在Lifecycle中先clean再install,打出jar包,并且放到本地maven私服中去
在消费者中添加我们打出来的DubboProvider项目jar包,pom.xml添加配置:
<!--注入提供者打出的jar包-->
<dependency>
<groupId>com.qingyun</groupId>
<artifactId>DubboProvider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(4)创建后台调用的controller控制层,注入SomeService接口并调用其方法
@Controller
public class SomeController {
@Autowired
private SomeService someService;
@RequestMapping(value = "/hello")
public String hello(Model model){
String hello = someService.sayHello("World");
model.addAttribute("hello",hello);
return "hello";
}
}
(5)在resources目录下,创建dubbo的配置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://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="dubbo-consumer"/>
<!-- 引用远程接口
id:远程接口服务的代理对象名称
interface:接口的全限定类名
url:调用远程接口服务的url地址,协议方式和端口都需要和提供者配置中的一致
registry:直连方式,不适用注册中心,N/A
-->
<dubbo:reference id="someService" interface="com.qingyun.service.SomeService"
url="dubbo://localhost:20880" registry="N/A"/>
</beans>
(6)在resources目录下,创建springmvc.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 自动扫描包,实现支持注解的IOC -->
<context:component-scan base-package="com.qingyun.controller" />
<!-- Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
<!-- 支持mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
(7)配置中央调试器,在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>springmvc</servlet-name>
<!-- Servlet类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml,classpath*:dubbo-consumer.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会被springmvc拦截 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(8)创建jsp页面,测试数据
<%--
Created by IntelliJ IDEA.
User: zhanglizeng
Date: 2021/7/3
Time: 16:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>
(9)idea添加tomcat服务器,需要添加两个tomcat,一个启动提供者项目,一个启动消费者项目
①创建tomcat为启动提供者项目
添加提供者war包依赖
②创建tomcat为消费者提供服务,注意端口冲突问题
添加上需要引入的war包
4.3测试直连项目
(1)启动提供者项目的tomcat,打印dubbo的映射地址,版本、主机地址等信息
(2)启动消费者consumer项目,通过http://localhost:8082/hello访问页面,说明已经调用到注入的提供者provider项目中的方法,是使用的接口调用的方式
5.dubbo直连方式-官方推荐方式
dubbo官方推荐使用的项目结构如下:
1)服务提供者工程:实现接口工程中的业务接口,web工程
2)服务消费者工程:消费服务提供者提供的业务接口,web工程
3)接口工程:业务接口和实体类,java工程
(1)创建服务提供者web工程
创建过程与4.1一致
(2)创建服务消费者web工程
创建过程与4.2一致
(3)创建java接口工程
不勾选create from archetype创建maven java工程
(4)接口工程DubboInteface01中添加业务接口和实体类
SomeService接口
/**
* 接口提供
*/
public interface SomeService {
String sayHello(String msg);
User getUserById(Integer id);
}
User实体
public class User {
private Integer id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
(5)提供者DubboProvider01项目需要在pom.xml中引入DubboInteface01工程
pom.xml配置
<?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.qingyun</groupId>
<artifactId>DubbboProvicer01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--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>
<!--引入java接口工程-->
<dependency>
<groupId>com.qingyun</groupId>
<artifactId>DubboInterface01</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
(6)提供者DubboProvider01项目实现DubboInteface01中的接口方法
SomeServiceImpl接口实现类
/**
* 接口实现类
*/
public class SomeServiceImpl implements SomeService {
public String sayHello(String msg) {
return "hello"+msg;
}
public User getUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三"+id);
return user;
}
}
(7)提供者DubboProvider01项目,配置dubbo信息
dubbo-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="dubbo-provider"/>
<!--指定dubbo服务的协议名称和端口号,指定dubbo,默认端口号20880-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--暴露服务dubbo:service
interface:暴露服务的接口全限定类名
ref:引用接口在spring容器中的标识名称
register:注册方式,使用直连方式,不适用注册中心,配置为N/A
-->
<dubbo:service interface="com.qingyun.service.SomeService"
ref="someServiceImpl" registry="N/A"/>
<!--把接口实现类添加到spring容器中-->
<bean id="someServiceImpl" class="com.qingyun.service.impl.SomeServiceImpl"/>
</beans>
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-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
(8)消费者DubboConsumer01项目需要在pom.xml中引入DubboInteface01工程
pom.xml配置
<?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.qingyun</groupId>
<artifactId>DubboConsumer01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--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>
<!--接口工程-->
<dependency>
<groupId>com.qingyun</groupId>
<artifactId>DubboInterface01</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
(9)消费者DubboConsumer01项目创建视图控制层
使用官方推荐的方式,此时注入的选项里面没有实现类impl类的选项了
SomeController类
/**
* 控制层
*/
@Controller
public class SomeController {
@Autowired
private SomeService someService;
@RequestMapping(value="hello")
public String hello(Model model){
String world = someService.sayHello("world");
model.addAttribute("hello",world);
return "hello";
}
@RequestMapping(value="userDetail")
public String userDetail(Model model,Integer id){
User user = someService.getUserById(id);
model.addAttribute("user",user);
return "userDetail";
}
}
(10)消费者DubboConsumer01项目添加dubbo配置
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://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="dubbo-consumer"/>
<!-- 引用远程接口
id:远程接口服务的代理对象名称
interface:接口的全限定类名
url:调用远程接口服务的url地址,协议方式和端口都需要和提供者配置中的一致
registry:直连方式,不适用注册中心,N/A
-->
<dubbo:reference id="someService" interface="com.qingyun.service.SomeService"
url="dubbo://localhost:20880" registry="N/A"/>
</beans>
springmvc.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 自动扫描包,实现支持注解的IOC -->
<context:component-scan base-package="com.qingyun.controller" />
<!-- Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
<!-- 支持mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
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>springmvc</servlet-name>
<!-- Servlet类 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml,classpath*:dubbo-consumer.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有请求都会被springmvc拦截 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(11)消费者DubboConsumer01项目添加视图jsp页面
(12)为提供者DubboProvider01项目、消费者DubboConsumer01配置tomcat
配置步骤见4.2(9)
(13)启动这两个tomcat访问系统
hello页面:
userDetail页面:
由于查询结果使用User实体返回,user实体需要序列化,否则会报错
java.lang.IllegalStateException: Serialized class
com.qingyun.model.User must implement java.io.Serializable
DubboInteface01项目中的User实体实现序列化
此时访问正常