Apache Dubbo 是一款高性能、轻量级的开源 Java RPC 框架,它提供了三大核心能力:html
Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案、 服务治理方案。java
官网:http://dubbo.apache.org/zh-cn/web
特性:redis
面向接口代理:
算法
调用接口的方法,在 A 服务器调用 B 服务器的方法,由 dubbo 实现对 B 的调用,无需关心实现的细节,就像 MyBatis 访问 Dao 的接口,能够操做数据库同样。不用关心 Dao 接口方法的实现spring
服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注册本身提供的服务。
服务消费者(Consumer ): 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅本身所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,若是调用失败,再选另外一台调用。
注册中心(Registry) :注册中心返回服务提供者地址列表给消费者,若是有变动,注册中心将基于长链接推送变动数据给消费者
监控中心(Monitor) :服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心数据库
调用关系说明
apache
支持多种协议:dubbo , hessian , rmi , http, webservice , thrift , memcached , redis。dubbo 官方推荐使用 dubbo 协议。dubbo 协议默认端口 20880
使用 dubbo 协议,spring 配置文件加入:
<dubbo:protocol name="dubbo" port="20880" />
spring-mvc
点对点的直连项目:消费者直接访问服务提供者,没有注册中心。消费者必须指定服务提供者的访问地址(url)tomcat
消费者直接经过 url 地址访问固定的服务提供者。这个 url 地址是不变的。
并添加对应的目录
添加了dubbo依赖
<?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.md</groupId> <artifactId>01-link-userservice-provider</artifactId> <version>1.0.0</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </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> <!--dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory><!--所在的目录--> <includes><!--包括目录下的.properties,.xml 文件都会扫描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
package com.md.dubbo.model; import java.io.Serializable; /** * @author MD * @create 2020-08-16 21:30 */ // model实现序列化 public class User implements Serializable { private Integer id; private String username; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package com.md.dubbo.service; import com.md.dubbo.model.User; /** * @author MD * @create 2020-08-16 21:35 */ public interface UserService { /** * 根据用户标识获取用户信息 * @param id * @return */ User queryUserById(Integer id); } //--------------------------------------- package com.md.dubbo.service.impl; import com.md.dubbo.model.User; import com.md.dubbo.service.UserService; /** * @author MD * @create 2020-08-16 21:38 */ public class UserServiceImpl implements UserService { @Override public User queryUserById(Integer id) { // 模拟 User user = new User(); user.setId(id); user.setUsername("pony"); user.setAge(20); return user; } }
在resources目录下创建dubbo-userservice-provider.xml文件
这里注意:选择apache的这个
<?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:application name="01-link-userservice-provider"/> <!--访问服务协议的名称及端口号 name:指定协议名称 port:指定协议端口号(默认20880) --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 暴露服务接口 interface:暴露服务接口的全限定类名 ref:接口引用的实现类在spring容器中的标识 registry:若是不使用注册中心,则值为N/A,直连 --> <dubbo:service interface="com.md.dubbo.service.UserService" ref="userService" registry="N/A"/> <!--将接口的实现类加载到Spring容器中--> <bean id="userService" class="com.md.dubbo.service.impl.UserServiceImpl"/> </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-userservice-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
端口号注意修改一下:
HTTP port :8081
JMX port:1098
服务接口中的方法要给消费者使用,消费者项目须要知道接口名称和接口中的方法名称、参数等。这些信息服务提供者才知道。须要把接口的 class 文件打包为 jar .
服务接口项目的类文件打包为 jar, 安装到 maven 仓库,仓库中的提供者 jar 能够被消费者使用。
使用 idea 的 maven 窗口执行 install
基本和服务提供者同样,这里就说一些主要的
仍是maven web项目
注意:添加了服务提供者的依赖
<?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.md</groupId> <artifactId>02-link-consumer</artifactId> <version>1.0.0</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </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> <!--dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!--依赖服务提供者--> <dependency> <groupId>com.md</groupId> <artifactId>01-link-userservice-provider</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory><!--所在的目录--> <includes><!--包括目录下的.properties,.xml 文件都会扫描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
在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="02-link-consumer"/> <!-- 引用远程服务接口: id:远程服务接口对象名称 interface:调用远程接口的全限定类名 url:访问服务接口的地址 registry:不使用注册中心,值为:N/A --> <dubbo:reference id="userService" interface="com.md.dubbo.service.UserService" url="dubbo://localhost:20880" registry="N/A"/> </beans>
package com.md.dubbo.web; import com.md.dubbo.model.User; import com.md.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; /** * @author MD * @create 2020-08-19 9:00 */ @Controller public class UserController { // 自动注入 @Autowired private UserService userService; @RequestMapping(value = "/user") public String userDetail(Model model , Integer id){ User user = userService.queryUserById(id); model.addAttribute("user",user); return "userDetail"; } }
在resources目录下建立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.md.dubbo.web"/> <!--配置注解驱动--> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.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>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>
和上面同样,只不过不用修改端口号了
在webapp下 创建userDetail.jsp
<%-- Created by IntelliJ IDEA. User: MD Date: 2020/8/19 Time: 9:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户详情</title> </head> <body> <h1>用户详情</h1> <div>用户标识:${user.id}</div> <div>用户名称:${user.username}</div> <div>用户年龄:${user.age}</div> </body> </html>