1、Dubbo概述
Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开
源Java RPC框架,可以和Spring框架无缝集成。
官网提供了用户文档与开发指南,基本对所有功能有大概的描述与使用方式
官方网址:https://dubbo.apache.org/zh/
2、Dubbo处理流程
以下处理流程图与节点说明均来自官方
节点说明:
节点 | 角色名称 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器,负责启动、加载、运行服务提供者 |
3、服务注册中心Zookeeper
通过前面的Dubbo架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo官方推荐使用Zookeeper作为服务注册中心。Zookeeper 是 Apache Hadoop 的子项目,作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。
Zookeeper安装及使用请参考:Zookeeper
4、Dubbo基本使用
4.1、基于注解模式
目录结构如下
- 接口协定
package com.ljfngu.service;
public interface HelloService {
String sayHello(String name);
}
- 创建服务提供者
引入Maven依赖(依赖版本在父工程中定义了,所以此处不再重新定义)
<dependency>
<groupId>com.ljfngu</groupId>
<artifactId>service-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 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>
编写服务实现类:
package com.ljfngu.service.impl;
import com.ljfngu.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
// @Service为dubbo提供的Service注解,非Spring提供的
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
编写dubbo-provider.properties配置文件
dubbo.application.name=service-provider-anno
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
编写服务提供者启动类:
package com.ljfngu;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class DubboPureMain {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
// 让程序等待,输入任何字符程序结束
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "com.ljfngu.service.impl")
@PropertySource("classpath:/dubbo-provider.properties")
static class ProviderConfiguration{}
}
- 创建服务消费者
首先引入与服务提供者相同依赖
编写Component调用服务
package com.ljfngu.component;
import com.ljfngu.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerComponent {
@Reference
private HelloService helloService;
public String sayHello(String name){
return helloService.sayHello(name);
}
}
编写dubbo-consumer.properties配置文件
dubbo.application.name=service-consumer-anno
dubbo.registry.address=zookeeper://localhost:2181
编写消费者启动类
package com.ljfngu;
import com.ljfngu.component.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class ConsumerPureMain {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
ConsumerComponent consumerComponent = context.getBean(ConsumerComponent.class);
while (true) {
System.in.read();
try {
System.out.println(consumerComponent.sayHello("dubbo"));
}catch (Exception e) {
e.printStackTrace();
}
}
}
@Configuration
@EnableDubbo(scanBasePackages = "com.ljfngu.service")
@ComponentScan(value = {"com.ljfngu.component"} )
@PropertySource("classpath:/dubbo-consumer.properties")
static class ConsumerConfiguration {}
}
- 测试
先启动服务提供者,然后启动服务消费者,测试结果如下图:
4.2、基于XML模式
- 引用上述api模块
- 创建服务提供者
依赖与基于注解的服务提供者一致,新增如下依赖:
....
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
</dependency>
编写与基于注解的服务实现类一致代码,去掉@Service注解即可
package com.ljfngu.service.impl;
import com.ljfngu.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
编写dubbo-provider.xml配置类
<?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="service-provider-xml"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo"/>
<bean id="helloService" class="com.ljfngu.service.impl.HelloServiceImpl"/>
<dubbo:service interface="com.ljfngu.service.HelloService" ref="helloService"/>
</beans>
编写服务提供者启动类
package com.ljfngu;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class DubboPureMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
// 让程序等待,输入任何字符程序结束
System.in.read();
}
}
- 创建服务消费者
依赖与服务创建者一致
编写dubbo-consumer.xml配置文件
<?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="service-consumer-xml"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference interface="com.ljfngu.service.HelloService" id="helloService"/>
</beans>
编写服务消费者
package com.ljfngu;
import com.ljfngu.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
HelloService helloService = context.getBean(HelloService.class);
System.out.println(helloService.sayHello("dubbo"));
}
}
- 测试
先后启动服务提供者和服务消费者