Dubbo基本用法-Dubbo Provider配置

Dubbo基本用法

本章节主要讲述如何配置dubbo,按照配置方式上分,可以分为:XML配置,properties方式配置,注解方式配置,API调用方式配置。

按照功能角度进行划分,可以分为Dubbo Provider和Dubbo Consumer。接下来章节中,分别对dubbo provider和Dubbo consumer进行讲解。

 

Dubbo Provider配置

 

Provider 配置详解

配置Dubbo Provider有4种方式:XML配置,properties方式配置,API调用方式配置,注解方式配置。

XML配置

最简单的配置的样例:


 
 
 
 
 
 

 

上面样例中,注意下dubbo schema的写法:


 

支持的配置标签

Dubbo基本用法-Dubbo Provider配置_第1张图片

Dubbo基本用法-Dubbo Provider配置_第2张图片

配置之间关系图

配置项详解

用于指定应用名,这里需要保证应用名唯一,这个应用名在后续的console admin中可以在列表中显示,方便管理。

注册中心配置,和服务发现的具体机制有关系。可以是zookeeper地质,也可以eureka地质。上面这个是广播地址,在本地服务调用的测试过程中非常方便。

这里是传输的协议和默认端口,一般不需要更改。

接下来重点讲解下的配置。


支持的主要属性列表:

| 属性名 | 说明 |

| ——– | —– |

| version | 版本号 |

| scope | 服务可见性, 值为:local 或者 remote,默认为remote |

| actives | 最大的激活的请求数 |

| async | 方法调用是否异步,默认为false |

| cache | 服务缓存,可选值:lru/threadlocal/jcache |

| callbacks | callback实例的限制 |

| generic | 泛化调用,可以绕过 |

| class | Service的实现的类名 |

| connections | 这个服务里的连接数 |

| delay | 发布服务延迟的毫秒数 |

| executes | 服务执行的请求上限 |

| retries | 超时重试次数 |

| timeout | 调用超时时间 |

其他配置属性请参考xsd:http://dubbo.apache.org/schema/dubbo/dubbo.xsd

作为的子元素,它可以针对方法进行配置。比较常用的属性有:

Dubbo基本用法-Dubbo Provider配置_第3张图片

其他属性,可以参考上面的xsd。


配置的覆盖关系

Dubbo基本用法-Dubbo Provider配置_第4张图片

配置的覆盖关系图

这里的覆盖关系包含了Provider和Consumer两端的配置,如果对consumer有疑问,可以参考后一章节的consumer章节之后再来理解。


dubbo.properties方式配置

如果公共配置很简单,没有多注册中心,多协议等情况,或者想多个 Spring 容器想共享配置,可以使用 dubbo.properties 作为缺省配置。

Dubbo 将自动加载 classpath 根目录下的 dubbo.properties,可以通过JVM启动参数 -Ddubbo.properties.file=xxx.properties 改变缺省配置位置。

dubbo.properties配置样例

# 应用名
dubbo.application.name=dubbodemo-provider
# 注册中心地址
dubbo.registry.address=zookeeper://localhost:2181
# 广播的注册中心样例
#dubbo.registry.address=multicast://224.5.6.7:1234
# 调用协议地址
dubbo.protocol.name=dubbo
dubbo.protocol.port=28080

 


 

映射规则

将 XML 配置的标签名,加属性名,用点分隔,多个属性拆成多行

比如:dubbo.application.name=foo等价于

比如:dubbo.registry.address=10.20.153.10:9090等价于

如果 XML 有多行同名标签配置,可用 id 号区分,如果没有 id 号将对所有同名标签生效

比如:dubbo.protocol.rmi.port=1234等价于 2

比如:dubbo.registry.china.address=10.20.153.10:9090等价于


 

覆盖策略

 

Dubbo基本用法-Dubbo Provider配置_第5张图片

JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口。

XML 次之,如果在 XML 中有配置,则 dubbo.properties 中的相应配置项无效。

Properties 最后,相当于缺省值,只有 XML 没有配置时,dubbo.properties 的相应配置项才会生效,通常用于共享公共配置,比如应用名。

注意:

1.如果 classpath 根目录下存在多个 dubbo.properties,比如多个 jar 包中有 dubbo.properties,Dubbo 会任意加载,并打印 Error 日志,后续可能改为抛异常。 

2.协议的 id 没配时,缺省使用协议名作为 id

 

annotation

 

Service注解暴露服务

import com.alibaba.dubbo.config.annotation.Service; 

@Service(timeout = 5000)
public class AnnotateServiceImpl implements AnnotateService { 
// ...
}

 

javaconfig形式配置公共模块

@Configuration
public class DubboConfiguration { 
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("provider-test");
return applicationConfig;
}

@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
}

 

这种方式的配置和前面用xml配置的方式是一样的效果。


 

指定dubbo扫描路径

@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service.impl")
public class ProviderTestApp {
// ...
}

 

或者使用spring bean xml配置方式:

 

 


 


 

api直接触发

import com.alibaba.dubbo.rpc.config.ApplicationConfig;
import com.alibaba.dubbo.rpc.config.RegistryConfig;
import com.alibaba.dubbo.rpc.config.ProviderConfig;
import com.alibaba.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;

// 服务实现
XxxService xxxService = new XxxServiceImpl();

// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");

// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");

// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);

// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口

// 服务提供者暴露服务配置
ServiceConfig service = new ServiceConfig(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");

// 暴露及注册服务
service.export();

 

一般在spring应用中,不推荐使用这种方式。 具体的含义这里不做解释,可以通过github查看源码。

 

Provider 接口和实现

上面章节更多从配置角度出发,接下来通过一个完整的例子,来讲解下dubbo provider的完整使用。

这个例子中只有一个服务UserReadService,有一个方法 getUserById。 需要将这个服务通过Dubbo暴露给远程的服务。具体的步骤如下:

1.创建工程

如果本来已经有工程,可以忽略。创建一个spring boot工程,可以通过 https://start.spring.io/ 创建。

2.定义接口

定义接口:UserReadService

public interface UserReadService{
public User getUserById(Long userId);
}

 


 

这个接口一般来说会放到独立的jar包里,作为client包。 其他应用要消费这个服务的时候,一般来说需要应用引用这个client包。(除了泛化调用)

3.实现接口

实现UserReadService, 当前实现部署在Provider的应用中。

public UserReadServiceImpl implements UserReadService{
public User getUserById(Long userId){
return xxx;
}
}

 

4.Dubbo配置


 
 
 
 

 

在此我向大家推荐一个架构学习交流群。交流学习群号:478030634  里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

 


 

你可能感兴趣的:(Java,架构,Java,架构,dubbo,后端,微服务)