Dubbo 注册中心配置介绍

注册中心是微服务一个不可缺少的组件,在Spring Cloud中有Eureka,Spring Cloud Zookeeper等注册中心。同样dubbo作为一个微服务框架,也实现了自己的注册中心。Dubbo支持好几种注册中心,包括Zookeeper,Multicast,Nacos,Redis和Simple。官方推荐使用Zookeeper作为注册中心。本栏目案例都是使用zookeeper作为注册中心。

Zookeeper注册中心

Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,官方推荐使用Zookeeper作为注册中心,如下图为Dubbo使用Zookeeper作为注册中心时的存储的节点。根节点为dubbo,dubbo的子节点为各个服务的全类型,全类型下节点为服务提供者和服务消费者列表。

dubbo注册中心配置类介绍

dubbo支持多种注册中心配置,包括使用xml,注解和Properties文件等。dubbo中注册中心对应的配置类为org.apache.dubbo.config.RegistryConfig。对应的参数如下表所示:

属性

类型

必选

缺省值

描述

id

String

 

注册中心引用BeanId,可以在<dubbo:service registry="">或<dubbo:reference registry="">中引用此Id

address

String

 

注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,如:ip:port,ip:port,不同集群的注册中心,请配置多个<dubbo:registry>标签

protocol

String

dubbo

注册中心地址协议,支持dubbo, multicast, zookeeper, 

redis, consul(2.7.1), sofa(2.7.2), etcd(2.7.2), nacos(2.7.2)等协议

port

int

9090

注册中心缺省端口,当address没有带端口时使用此端口做为缺省值

username

String

 

登录注册中心用户名,如果注册中心不需要验证可不填

password

String

 

登录注册中心密码,如果注册中心不需要验证可不填

transport

String

netty

网络传输方式,可选mina,netty

timeout

int

5000

注册中心请求超时时间(毫秒)

session

int

60000

注册中心会话超时时间(毫秒),用于检测提供者非正常断线后的脏数据,比如用心跳检测的实现,此时间就是心跳间隔,不同注册中心实现不一样。

file

String

 

使用文件缓存注册中心地址列表及服务提供者列表,应用重启时将基于此文件恢复,注意:两个注册中心不能使用同一文件存储

wait

int

0

停止时等待通知完成时间(毫秒)

check

boolean

true

注册中心不存在时,是否报错

register

boolean

true

是否向此注册中心注册服务,如果设为false,将只订阅,不注册

subscribe

boolean

true

是否向此注册中心订阅服务,如果设为false,将只注册,不订阅

dynamic

boolean

true

服务是否动态注册,如果设为false,注册后将显示为disable状态,需人工启用,并且服务提供者停止时,也不会自动取消注册,需人工禁用。

group

String

dubbo

服务注册分组,跨组的服务不会相互影响,也无法相互调用,适用于环境隔离。

simplified

boolean

false

注册到注册中心的URL是否采用精简模式的(与低版本兼容)

extra-keys

String

 

在simplified=true时,extraKeys允许你在默认参数外将额外的key放到URL中,格式:"interface,key1,key2"。

使用XML配置注册中心

Dubbo支持使用XML文件配置注册中心。Dubbo使用<dubbo:registry>标签配置注册中心。示例如下:

<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:registry address="127.0.0.1" protocol="zookeeper" port="2181"/>

上面的配置是最简单的配置,我们只配置了一个注册中心服务地址(这里是zookeeper地址),address属性包含了协议(protocol)属性,地址属性(服务ip)和端口(port)。我们也可以配置上面代码中的第二行的格式。我们可以根据需要在<dubbo:registry>标签中添加属性,属性的名称为RegistryConfig类中的字段名称。如下是使用Zookeeper注册中心的XML配置的一个案例HelloWorld:本案例参考官方案例dubbo-samples-simplified-registry-xml。

1.创建父模块 dubbo-register,在pom.xml中引入dobbo依赖。

<properties>
    <source.level>1.8</source.level>
    <target.level>1.8</target.level>
    <dubbo.version>2.7.1</dubbo.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
	    <groupId>org.apache.dubbo</groupId>
	    <artifactId>dubbo-dependencies-bom</artifactId>
	    <version>${dubbo.version}</version>
	    <type>pom</type>
	    <scope>import</scope>
	</dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo</artifactId>
	<version>${dubbo.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>${dubbo.version}</version>
        <type>pom</type>
    </dependency>
</dependencies>

2.公共服务接口模块 common-api 创建接口HelloService

package club.hlcode.common.service;
public interface HelloService {
    public String sayHello(String username);
}

3.服务提供者模块 dubbo-provider 在pom中引入公共接口模块 common-api,并且实现HelloService接口。

package club.hlcode.provider.service.impl;
import club.hlcode.common.service.HelloService;
public class HelloServiceImpl implements HelloService {
    public String sayHello(String username) {
        return "Hello " + username + "!";
    }
}

4.将服务注册到注册中心,如下为一个完整的注册服务的配置,Dubbo使用<dubbo:service/>标签注册服务。后续我们会详细讲解该标签配置。

<dubbo:application name="registry-xml-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" simplified="true" />
<bean id="helloService"
		class="club.hlcode.provider.service.impl.HelloServiceImpl" />
<dubbo:service ref="helloService"
		interface="club.hlcode.common.service.HelloService" />

 

 5.启动服务提供者服务,代码如下,此时一个Dubbo服务就已经完成,并且已经发布,然后我们就要该服务的使用了。

package club.hlcode.provider;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RegistryXmlProvider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/provider.xml" });
        context.start();
        System.in.read();
    }
}

前面我们使用<dubbo:service/>标签和Spring Bean标签结合,发布一个Dubbo服务。发布服务的目的是为了使用,Dubbo中使用<dubbo:reference/>标签引用一个已经发布的服务,该标签的使用后续会详细解释。创建一个服务消费模块dubbo-consume,在pom中引入公共接口模块 common-api。其简单配置如下:

<dubbo:application name="registry-xml-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="true" simplified="true"/>
<dubbo:reference id="helloService" interface="club.hlcode.common.service.HelloService"/>

编写启动类,并且调用服务,一个简单的Dubbo案例就完成了。后续我们会逐渐深入讲解Dubbo的各个功能的使用与源码的解析。该例子是Dubbo的一个入门案例

package club.hlcode.consume;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import club.hlcode.common.service.HelloService;
public class RegistryXmlConsume {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "META-INF/spring/consumer.xml" });
	context.start();
	HelloService helloService = (HelloService) context.getBean("helloService");
	System.out.println(helloService.sayHello("Pharos"));
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值