一、概述
dubbo
是阿里巴巴开源的基于 Java
的高性能RPC
(一种远程调用) 分布式服务框架,致力于提供高性能和透明化的RPC
远程服务调用方案,以及SOA
服务治理方案。
其核心部分包含:
1、远程通讯:提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2、集群容错:提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3、自动发现:基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
二、dubbo的主要用途
1、透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2、软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3.、服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载dubbo的配置即可,dubbo基于Spring的Schema扩展进行加载。
三、dubbo 和 Spring Cloud区别
1、通信方式不同:dubbo 使用的是 RPC 通信,而Spring Cloud 使用的是HTTP RESTFul方式。
2、组成不一样:dubbo的服务注册中心为Zookeerper,服务监控中心为dubbo-monitor,无消息总线、服务跟踪、批量任务等组件;
Spring Cloud的服务注册中心为spring-cloud netflix enruka,服务监控中心为spring-boot admin,有消息总线、数据流、服务跟踪、批量任务等组件;
四、dubbo
入门案例
1.服务端
首先,我们先把服务端的接口写好,因为其实dubbo
的作用简单来说就是给消费端提供接口。
/**
* xml方式服务提供者接口
*/
public interface ProviderService {
String SayHello(String word);
}
接着,定义它的实现类
/**
* xml方式服务提供者实现类
*/
public class ProviderServiceImpl implements ProviderService{
public String SayHello(String word) {
return word;
}
}
导入 maven 依赖
<?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.ouyangsihai</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
这里使用的dubbo的版本是2.6.6,需要注意的是,如果你只导入dubbo的包的时候是会报错的,找不到netty和curator的依赖,所以,在这里我们需要把这两个的依赖加上,就不会报错了。
另外,这里我们使用 zookeeper作为注册中心。
到目前为止,dubbo 需要的环境就已经可以了,下面,我们就把上面刚刚定义的接口暴露出去。
暴露接口(xml 配置方法)
首先,我们在我们项目的resource目录下创建 meta-inf.spring 包,然后再创建 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:application name="provider" owner="sihai">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="55555"/>
</dubbo:application>
<dubbo:monitor protocol="registry"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--<dubbo:registry address="N/A"/>-->
<dubbo:registry address="N/A" />
<!--当前服务发布所依赖的协议;webservice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service
interface="com.sihai.dubbo.provider.service.ProviderService"
ref="providerService"/>
<!--Bean bean定义-->
<bean id="providerService" class="com.sihai.dubbo.provider.service.ProviderServiceImpl"/>
</beans>
说明:
1、上面的文件其实就是类似 spring的配置文件,而且,dubbo 底层就是 spring。
2、节点:dubbo:application
就是整个项目在分布式架构中的唯一名称,可以在name属性中配置,另外还可以配置owner字段,表示属于谁。
下面的参数是可以不配置的,这里配置是因为出现了端口的冲突,所以配置。
3、节点:dubbo:monitor
监控中心配置, 用于配置连接监控中心相关信息,可以不配置,不是必须的参数。
4、节点:dubbo:registry
配置注册中心的信息,比如,这里我们可以配置 zookeeper 作为我们的注册中心。address 是注册中心的地址,这里我们配置的是 N/A 表示由 dubbo自动分配地址。或者说是一种直连的方式,不通过注册中心。
5、节点:dubbo:protocol
服务发布的时候 dubbo 依赖什么协议,可以配置dubbo、webservice、http等协议。
6、节点:dubbo:service
这个节点就是我们的重点了,当我们服务发布的时候,我们就是通过这个配置将我们的服务发布出去的。interface是接口的包路径,ref是第 ⑦ 点配置的接口的bean。
7、最后,我们需要像配置spring的接口一样,配置接口的 bean。
到这一步,关于服务端的配置就完成了,下面我们通过 main 方法将接口发布出去。
package com.sihai.dubbo.provider;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.alibaba.dubbo.container.Main;
import com.sihai.dubbo.provider.service.ProviderService;
import com.sihai.dubbo.provider.service.ProviderServiceImpl;
import org.springframework.context.support.ClasspathXmlApplicationContext;
import java.io.IOException;
/**
* xml方式启动
*
*/
public class App
{
public static void main( String[] args ) throws IOException {
//加载xml配置文件启动
ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("meta-inf/spring/provider.xml");
context.start();
system.in.read(); // 按任意键退出
}
}
dubbo暴露的url:
dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService?anyhost=true&application=provider&bean.name=com.sihai.dubbo.provider.service.ProviderService&bind.ip=192.168.234.1&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.sihai.dubbo.provider.service.ProviderService&methods=SayHello&owner=sihai&pid=8412&qos.accept.foreign.ip=false&qos.enable=true&qos.port=55555&side=provider×tamp=1562077289380
2.消费端
上面提到,我们在服务端提供的只是点对点的方式提供服务,并没有使用注册中心,所以,下面的配置也是会有一些不一样的。
消费端环境配置
首先,我们在消费端的 resource
下建立配置文件 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:application name="consumer" owner="sihai"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<dubbo:registry address="N/A" />
<!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->
<!--生成一个远程服务的调用代理-->
<!--点对点方式-->
<dubbo:reference id="providerService"
interface="com.sihai.dubbo.provider.service.ProviderService"
url="dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService"/>
<!--<dubbo:reference id="providerService"
interface="com.sihai.dubbo.provider.service.ProviderService"/>-->
</beans>
分析如下所示:
1、发现这里的 dubbo:application
和 dubbo:registry
是一致的
2、dubbo:reference
:我们这里采用点对点的方式,所以,需要配置在服务端暴露的 url
maven 依赖
和服务端一样:
<?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.ouyangsihai</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.ouyangsihai</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
调用服务:
package com.sihai.dubbo.consumer;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.sihai.dubbo.provider.service.ProviderService;
import org.springframework.context.support.ClasspathXmlApplicationContext;
import java.io.IOException;
/**
* xml的方式调用
*
*/
public class App
{
public static void main( String[] args ) throws IOException {
ClasspathXmlApplicationContext context=new ClasspathXmlApplicationContext("consumer.xml");
context.start();
ProviderService providerService = (ProviderService) context.getBean("providerService");
String str = providerService.SayHello("hello");
System.out.println(str);
system.in.read();
}
}
3.加入zookeeper
作为注册中心
在前面的案例中,我们没有使用任何的注册中心,而是用一种直连的方式进行的。但是,实际上很多时候,我们都是使用dubbo + zookeeper的方式,使用 zookeeper 作为注册中心,这里,我们就介绍一下 zookeeper作为注册中心的使用方法。
这里,我们在前面的入门实例中进行改造。
服务端
在服务端中,我们只需要修改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:application name="provider" owner="sihai">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="55555"/>
</dubbo:application>
<dubbo:monitor protocol="registry"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--<dubbo:registry address="N/A"/>-->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!--当前服务发布所依赖的协议;webservice、Thrift、Hessain、http-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--服务发布的配置,需要暴露的服务接口-->
<dubbo:service
interface="com.sihai.dubbo.provider.service.ProviderService"
ref="providerService"/>
<!--Bean bean定义-->
<bean id="providerService" class="com.sihai.dubbo.provider.service.ProviderServiceImpl"/>
</beans>
消费端
跟服务端一样,在消费端,我们也只需要修改 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:application name="consumer" owner="sihai"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<!--点对点的方式-->
<!--<dubbo:registry address="N/A" />-->
<dubbo:registry address="zookeeper://localhost:2181" check="false"/>
<!--生成一个远程服务的调用代理-->
<!--点对点方式-->
<!--<dubbo:reference id="providerService"
interface="com.sihai.dubbo.provider.service.ProviderService"
url="dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService"/>-->
<dubbo:reference id="providerService"
interface="com.sihai.dubbo.provider.service.ProviderService"/>
</beans>
好了,消费端也配置好了,这样就可以使用修改的入门案例,重新启动运行了。