高性能RPC框架--Apache Dubbo

Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现

1 Dubbo 处理流程

在这里插入图片描述
节点说明:

节点角色名称
Provider暴露服务的服务提供方
Consumer调用远程服务的服务消费方
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心
Container服务运行容器 负责启动 加载 运行服务提供者

调用关系说明:

虚线 :代表异步调用 实线代表同步访问
蓝色虚线 :是在启动时完成的功能
红色虚线: 是程序运行中执行的功能

调用流程:

1)服务提供者在服务容器启动时 向注册中心 注册自己提供的服务
2)服务消费者在启动时 向注册中心订阅自己所需的服务
3)注册中心返回服务提供者地址列表给消费者 如果有变更 注册中心会基于长连接推送变更数据给消费者
4)服务消费者 从提供者地址列表中 基于软负载均衡算法 选一台提供者进行调用 如果调用失败 则重新选择一台
5)服务提供者和消费者 在内存中的调用次数 和 调用时间 定时每分钟发送给监控中心

2 Dubbo开发实战

在Dubbo中所有的的服务调用都是基于接口去进行双方交互的。双方协定好Dubbo调用中的接口,提供者来提供实现类并且注册到注册中心上。
调用方则只需要引入该接口,并且同样注册到相同的注册中心上(消费者)。即可利用注册中心来实现集群感知功能,之后消费者即可对提供者进行调用。我们所有的项目都是基于Maven去进行创建,这样相互在引用的时候只需要以依赖的形式进行展现就可以了。并且这里我们会通过maven的父工程来统一依赖的版本。

程序实现分为以下几步骤:

  1. 建立maven工程 并且 创建API模块: 用于规范双方接口协定
  2. 提供provider模块,引入API模块,并且对其中的服务进行实现。将其注册到注册中心上,对外来统一提供服务。
  3. 提供consumer模块,引入API模块,并且引入与提供者相同的注册中心。再进行服务调用。

开发过程

  1. 定义maven。
<groupId>com.lagou</groupId> 
<artifactId>service-api</artifactId> 
<version>1.0-SNAPSHOT</version>
  1. 定义接口,这里为了方便,只是写一个基本的方法。
public interface HelloService { String sayHello(String name); }

创建接口提供者
3. 引入API模块。

<dependency> 
<groupId>com.lagou</groupId> 
<artifactId>service-api</artifactId> 
<version>${project.version}</version> 
</dependency>
  1. 引入Dubbo相关依赖,这里为了方便,使用注解方式。
<dependency> 
<groupId>org.apache.dubbo</groupId>
 <artifactId>dubbo</artifactId>
</dependency> 
<dependency> 
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId> 
</dependency> 
<dependency> 
<groupId>org.apache.dubbo</groupId> 
<artifactId>dubbo-rpc-dubbo</artifactId> 
</dependency> 
<dependency> 
<groupId>org.apache.dubbo</groupId> 
<artifactId>dubbo-remoting-netty4</artifactId> 
</dependency> 
<dependency> 
<groupId>org.apache.dubbo</groupId> 
<artifactId>dubbo-serialization-hessian2</artifactId> 
</dependency>
  1. 编写实现类。注意这里也使用了Dubbo中的 @Service 注解来声明他是一个服务的提供者。
@Service public class HelloServiceImpl implements HelloService { 
@Override public String sayHello(String name) { return "hello: " + name; } }
  1. 编写配置文件,用于配置dubbo。比如这里我就叫 dubbo-provider.properties ,放入到resources 目录下。
dubbo.application.name=dubbo-demo-annotation-provider 
dubbo.protocol.name=dubbo 
dubbo.protocol.port=20880
  1. 编写启动的 main 函数。这里面做的比较简单,主要要注意注解方式中的注册中心这里是使用的本机2181端口来作为注册中心。
public class DubboPureMain { 
	public static void main(String[] args) throws Exception { 
		AnnotationConfigApplicationContext context = new
		AnnotationConfigApplicationContext(ProviderConfiguration.class); 
		context.start(); 
		System.in.read(); 
	}
	@Configuration @EnableDubbo(scanBasePackages = "com.lagou.service.impl")
	@PropertySource("classpath:/dubbo-provider.properties") 
	static class ProviderConfiguration { 
		@Bean public RegistryConfig registryConfig() { 
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setAddress("zookeeper://127.0.0.1:2181");
		return registryConfig; 
		} 
	} 
}

创建消费者

  1. 引入API模块。
<dependency> 
	<groupId>com.lagou</groupId> 
	<artifactId>service-api</artifactId> 
	<version>${project.version}</version> 
</dependency>
  1. 引入Dubbo依赖 ,同服务提供者。
  2. 编写服务,用于真实的引用dubbo接口并使用。因为这里是示例,所以比较简单一些。这里面@Reference 中所指向的就是真实的第三方服务接口。
@Component 
public class ConsumerComponent { 
	@Reference 
	private HelloService helloService; 
	public String sayHello(String name) {
	 	return helloService.sayHello(name); 
	} 
}
  1. 编写消费者的配置文件。这里比较简单,主要就是指定了当前消费者的名称和注册中心的位置。通过这个注册中心地址,消费者就会注册到这里并且也可以根据这个注册中心找到真正的提供者列表
dubbo.application.name=service-consumer 
dubbo.registry.address=zookeeper://127.0.0.1:2181
  1. 编写启动类,这其中就会当用户在控制台输入了一次换行后,则会发起一次请求。
public class AnnotationConsumerMain { 
	public static void main(String[] args) throws IOException, InterruptedException {
		 AnnotationConfigApplicationContext context = new
		 AnnotationConfigApplicationContext(ConsumerConfiguration.class);
		 context.start(); 
		 ConsumerComponent service = context.getBean(ConsumerComponent.class); 
		 while (true) { 
			 System.in.read(); 
			 try {
				 String hello = service.sayHello("world"); 
				 System.out.println("result :" + hello); 
			 } catch (Exception e) { 
				 e.printStackTrace(); 
			 }
		} 
	}
	@Configuration 
	@EnableDubbo(scanBasePackages = "com.lagou.service") 
	@PropertySource("classpath:/dubbo-consumer.properties") 
	@ComponentScan(value = {"com.lagou.bean.consumer"}) 
	static class ConsumerConfiguration { 
	} 
}

配置介绍

provider模块编写 dubbo-provider.xml 文件,用于对dubbo进行文件统一配置。并且对刚才的配置进行引入。

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
<dubbo:application name="dubbo-demo-annotation-provider"/> 
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> 
<dubbo:protocol name="dubbo"/> 
<bean id="helloService" class="com.lagou.service.impl.HelloServiceImpl"/>
	<dubbo:service interface="com.lagou.service.HelloService" ref="helloService"/> 
</beans>

编写模块启动类

public class ProviderApplication { 
	public static void main(String[] args) throws Exception { 
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-*.xml");
		context.start(); 
		System.in.read(); 
	} 
}

consumer模块定义spring的配置xml。

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
<dubbo:application name="demo-consumer"/> 
<dubbo:registry address="zookeeper://127.0.0.1:2181"/> 
<dubbo:reference id="helloService" interface="com.lagou.service.HelloService"> </dubbo:reference> 
</beans>

引入启动模块。因为引用了Spring框架,所以再上一步的helloService会被当做一个bean注入到真实的环境中。在我们生产级别使用的时候,我们可以通过Spring中的包扫描机制,通过@Autowired 这种机制来进行依赖注入。

public class XMLConsumerMain { 
	public static void main(String[] args) throws Exception {
		 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
		 context.start(); 
		 HelloService demoService = context.getBean(HelloService.class);
		 System.out.println(demoService.sayHello("world")); 
	 } 
}

3 Dubbo配置项说明

dubbo:application

对应 org.apache.dubbo.config.ApplicationConfig, 代表当前应用的信息

  1. name: 当前应用程序的名称,在dubbo-admin中我们也可以看到,这个代表这个应用名称。我们在真正时是时也会根据这个参数来进行聚合应用请求。
  2. owner: 当前应用程序的负责人,可以通过这个负责人找到其相关的应用列表,用于快速定位到责任人。
  3. qosEnable : 是否启动QoS 默认true
  4. qosPort : 启动QoS绑定的端口 默认22222
  5. qosAcceptForeignIp: 是否允许远程访问 默认是false

dubbo:registry
org.apache.dubbo.config.RegistryConfig, 代表该模块所使用的注册中心。一个模块中的服务可以将其注册到多个注册中心上,也可以注册到一个上。后面再service和reference也会引入这个注册中心。

  1. id : 当当前服务中provider或者consumer中存在多个注册中心时,则使用需要增加该配置。在一些公司,会通过业务线的不同选择不同的注册中心,所以一般都会配置该值。
  2. address : 当前注册中心的访问地址。
  3. protocol : 当前注册中心所使用的协议是什么。也可以直接在 address 中写入,比如使用zookeeper,就可以写成 zookeeper://xx.xx.xx.xx:2181 4. timeout : 当与注册中心不再同一个机房时,大多会把该参数延长。

dubbo:protocol

org.apache.dubbo.config.ProtocolConfig, 指定服务在进行数据传输所使用的协议。

  1. id : 在大公司,可能因为各个部门技术栈不同,所以可能会选择使用不同的协议进行交互。这里在多个协议使用时,需要指定。
  2. name : 指定协议名称。默认使用 dubbo 。

dubbo:service
org.apache.dubbo.config.ServiceConfig, 用于指定当前需要对外暴露的服务信息,后面也会具体讲
解。和 dubbo:reference 大致相同。

  1. interface : 指定当前需要进行对外暴露的接口是什么。
  2. ref : 具体实现对象的引用,一般我们在生产级别都是使用Spring去进行Bean托管的,所以这里面一般也指的是Spring中的BeanId。
  3. version : 对外暴露的版本号。不同的版本号,消费者在消费的时候只会根据固定的版本号进行消费

dubbo:reference
org.apache.dubbo.config.ReferenceConfig, 消费者的配置,这里只做简单说明,后面会具体讲解。

  1. id : 指定该Bean在注册到Spring中的id。
  2. interface: 服务接口名
  3. version : 指定当前服务版本,与服务提供者的版本一致。
  4. registry : 指定所具体使用的注册中心地址。这里面也就是使用上面在 dubbo:registry 中所声明的id。

dubbo:method
org.apache.dubbo.config.MethodConfig, 用于在制定的 dubbo:service 或者 dubbo:reference 中的更具体一个层级,指定具体方法级别在进行RPC操作时候的配置,可以理解为对这上面层级中的配置针
对于具体方法的特殊处理。

  1. name : 指定方法名称,用于对这个方法名称的RPC调用进行特殊配置。
  2. async: 是否异步 默认false

其它配置 参考官网
schema 配置参考手册

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值