文章目录
通过本示例程序来熟悉Dubbo框架。应用程序可以通过spring XML、spring 注解和java API这3种方式来编写
另外:
- spring,zookeepermaven等知识已经了解
- zookeeper作为注册中心,需要安装zookeeper,如何安装自行百度
1 抽取一个API模块
该模块主要是定义dubbo的接口,接口的实现将交给服务提供者实现
- 定义一个接口EchoService
public interface EchoService {
String echo(String message);
}
2 spring xml版
2.1 服务提供者
- maven依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.3</version>
</dependency>
<!--zk的客户端-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
<exclusions>
<!--使用dubbo依赖的netty-->
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
</dependencies>
- 实现EchoService接口
package study.wyy.dubbo.quick.provider.xml.service;
import study.wyy.dubbo.quick.api.service.EchoService;
public class EchoServiceImpl implements EchoService {
public String echo(String message) {
// 原样返回
return message;
}
}
- spring 配置文件
<?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.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务提供方应用名称, 方便用于依赖跟踪 -->
<dubbo:application name="quick-start-provider-xml"/>
<!-- 设置注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--指定dubbo协议, 并监听20880端口-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--通过XML方式把实现配置为Bean,让Spring托管和实例化-->
<bean name="echoService" class="study.wyy.dubbo.quick.provider.xml.service.EchoServiceImpl"></bean>
<!--声明要暴露的接口-->
<dubbo:service interface="study.wyy.dubbo.quick.api.service.EchoService" ref="echoService"/>
</beans>
- 主方法
package study.wyy.dubbo.quick.provider.xml;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("spring/applicationcontext.xml");
context.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}finally {
context.close();
}
}
}
- 启动服务提供者,使用telnet 测试
- 连接20880端口:
telnet localhost 20880
- invoke指令会发起Dubbo服务调用,该指令采用[接口 .方法]格式调用,采用JSON 格式传递参数
invoke study.wyy.dubbo.quick.api.service.EchoService.echo("hello dubbo")
- 连接20880端口:
2.2 服务消费者
- 依赖
<dependencies>
<dependency>
<groupId>study.wyy</groupId>
<artifactId>dubbo-simple-quickstart-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<exclusions>
<!--使用dubbo依赖的netty-->
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
- spring配置文件
<?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.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务消费者应用名称, 方便用于依赖跟踪 -->
<dubbo:application name="quick-start-consumer-xml"/>
<!-- 设置注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--指定dubbo协议, 并监听20880端口-->
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:reference id="echoService"
interface="study.wyy.dubbo.quick.api.service.EchoService"
check="false"
/>
</beans>
- 测试
package study.wyy.dubbo.quick.consumerxml;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import study.wyy.dubbo.quick.api.service.EchoService;
@Slf4j
public class ConsumerApplication {
public static void main(String[] args) {
// 构建 spring上下文
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("spring/applicationcontext.xml");
EchoService echoService = context.getBean(EchoService.class);
// 调用 echoService接口
String res = echoService.echo("Hello Dubbo");
log.info("echoService invoke result is {}",res);
}
}
- 控制台日志输出:
16:08:24.912 [main] INFO study.wyy.dubbo.quick.consumerxml.ConsumerApplication - echoService invoke result is Hello Dubbo
3 spring 注解版
3.1 服务提供者
- 依赖和xml开发没有区别
- 实现接口
package study.wyy.dubbo.quick.provider.annotation.service;
import com.alibaba.dubbo.config.annotation.Service;
import study.wyy.dubbo.quick.api.service.EchoService;
// 注意:这个注解是dubbo提供的,不是spring的Service注解
@Service
public class EchoServiceImpl implements EchoService {
public String echo(String message) {
// 原样返回
return message;
}
}
使用Service注解后,由Dubbo服务将这个实现类提升为Spring容器的Bean,并且负责 配置初始化和服务暴露
- 基于注解来生成Dubbo的配置信息
package study.wyy.dubbo.quick.provider.annotation.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author wyaoyao
* @description
* @date 2021/1/29 16:18
*/
@Configuration
// 启用dubbo,配置dubbo的扫描包
@EnableDubbo(scanBasePackages = "study.wyy.dubbo.quick.provider.annotation.service")
public class DubboConfig {
@Bean
public ProviderConfig providerConfig(){
ProviderConfig providerConfig = new ProviderConfig();
return providerConfig;
}
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
// 设置服务名字,对应xml的 <dubbo:application name="quick-start-provider-annotation"/>
applicationConfig.setName("quick-start-provider-annotation");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
// 配置注册中心的地址, 对应xml : <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
registryConfig.setAddress("localhost");
registryConfig.setProtocol("zookeeper");
registryConfig.setPort(2181);
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
// 对应xml :<dubbo:protocol name="dubbo" port="20880"/>
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
return protocolConfig;
}
}
- 启动类
package study.wyy.dubbo.quick.provider.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import study.wyy.dubbo.quick.provider.annotation.config.DubboConfig;
import java.io.IOException;
public class ProviderApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DubboConfig.class);
context.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
} finally {
context.close();
}
}
}
3.2 服务消费者
- 依赖和xml没有区别
- 通过注解消费服务时,只需要Reference注解标注,该注解适用于对象字段和方法中。因 此需要做一下特殊的包装。接下来,我们在EchoConsumer类中定义echoService字段,该字段 用^Reference注解标注
package study.wyy.dubbo.quick.consumer.annotation.service;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import study.wyy.dubbo.quick.api.service.EchoService;
@Component
@Slf4j
public class EchoConsumer {
@Reference
EchoService echoService;
public void invokeEcho(){
String res = echoService.echo("hello dubbo");
log.info("echoService invoke result is {}",res);
}
}
- 消费者配置
package study.wyy.dubbo.quick.consumer.annotation.config;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
// 配置dubbo的扫描包,扫描Reference注解
@EnableDubbo(scanBasePackages = "study.wyy.dubbo.quick.consumer.annotation.service")
// 这个是为了将EchoConsumer 注入到容器中
@ComponentScan("study.wyy.dubbo.quick.consumer.annotation.service")
public class DubboConsumerConfig {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
// 设置服务名字,对应xml的 <dubbo:application name="quick-start-provider-annotation"/>
applicationConfig.setName("quick-start-consumer-annotation");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
// 配置注册中心的地址, 对应xml : <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
registryConfig.setAddress("localhost");
registryConfig.setProtocol("zookeeper");
registryConfig.setPort(2181);
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
// 对应xml :<dubbo:protocol name="dubbo" port="20880"/>
protocolConfig.setPort(20880);
protocolConfig.setName("dubbo");
return protocolConfig;
}
}
- 启动类
package study.wyy.dubbo.quick.consumer.annotation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import study.wyy.dubbo.quick.api.service.EchoService;
import study.wyy.dubbo.quick.consumer.annotation.config.DubboConsumerConfig;
import study.wyy.dubbo.quick.consumer.annotation.service.EchoConsumer;
@Slf4j
public class ConsumerApplication {
public static void main(String[] args) {
// 构建 spring上下文
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext (DubboConsumerConfig.class);
EchoConsumer echoConsumer = context.getBean(EchoConsumer.class);
// 调用 echoService接口
echoConsumer.invokeEcho();
}
}
- 运行并观察日志输出
4 基于API实现
Dubbo框架大部分场景都会在Spring中使用,但是不局限于这种场景。除了基于XML和 注解的方式,Dubbo框架还支持API的方式。虽然大部分场景不会直接使用API的方式暴露和 消费服务,但是在某些场景下API非常有用。比如开发网关类的应用,需要动态消费不同版本 的服务,通过API方式,可以根据前端请求参数动态构造不同版本的服务实例等。
4.1 基于API编写Echo服务器
基于配置方式启动Dubbo,框架内部必须做很多转换,比如把标签<dubbo:protocol .. ./>
转换成等价的配置对象,但这些转换对业务方都是透明的,不管是XML配置还是注解方式, 最终都会转换成Java API对应的配置对象(ServiceConfig)
package study.wyy.dubbo.quick.provider.java;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import study.wyy.dubbo.quick.api.service.EchoService;
import study.wyy.dubbo.quick.provider.java.service.EchoServiceImpl;
import java.io.IOException;
public class ProviderApplication {
public static void main(String[] args) throws IOException {
// 将EchoService 暴露
registryDubbo();
System.out.println("java-echo-provider is running.");
System.in.read();
}
private static void registryDubbo() {
ServiceConfig<EchoService> service = new ServiceConfig<>();
service.setApplication(new ApplicationConfig("java-echo-provider"));
// 设置注册中心地址
service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
service.setInterface(EchoService.class);
service.setRef(new EchoServiceImpl());
service.export();
}
}
4.2 基于API编写Echo客户端
采用Java API是最灵活的方式,可以与第三方框架集成,特别适合动态消费场景。在Dubbo 框架中典型的使用场景就是泛化调用,可以指定一个本地不存在的接口发起RPC调用。通过 API的方式是最简洁的
package study.wyy.dubbo.quick.consumer.java;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import lombok.extern.slf4j.Slf4j;
import study.wyy.dubbo.quick.api.service.EchoService;
/**
* @author wyaoyao
* @description
* @date 2021/1/29 17:24
*/
@Slf4j
public class EchoConsumer {
public static void main(String[] args) {
ReferenceConfig<EchoService> referenceConfig = new ReferenceConfig();
referenceConfig.setApplication(new ApplicationConfig("java-echo-consumer"));
// 配置注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setPort(2181);
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("localhost");
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(EchoService.class);
EchoService echoService = referenceConfig.get();
String res = echoService.echo("hello dubbo");
log.info("echoService invoke result is {}",res);
}
}