Dubbo from entry to actual combat: combat chapter

 

First, the addition of a zookeeper registry

In the previous case, we did not use any of the registry, but by way of direct conducted. However, in practice many times, we are using dubbo + zookeeper way, using the registry as a zookeeper, here we introduce the zookeeper as the use of registry.

Here, we have to transform in front of the entry instance.

1.1 server

The service side, we only need to modify provider.xml.

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://code.alibabatech.com/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTP: // the WWW. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd springframework.org/schema/beans/spring-beans.xsd "> 

    <-! current in the name of the entire project unique distributed architecture which is calculated dependent on the relationship between the label -> 
    <Dubbo: the Application name = "Provider" owner = "Sihai"> 
        <Dubbo: the Parameter Key = "qos.enable" value = "true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo: the Parameter Key = "qos.port" value = "55555" /> 
    </ dubbo: the Application> 

    <dubbo: Monitor Protocol = "Registry" /> 

    <- corresponds dubbo this service to be exposed to the service address! Register Center -> 
    <- <Dubbo:! Registry address = "N / A" /> -> 
    <Dubbo: Registry address = "ZooKeeper: // localhost: 2181" Check = "to false" /> 

    <-! - current service release depends agreement; webserovice, Thrift, Hessain, HTTP -> 
    <Dubbo: protocol name = "Dubbo" Port = "20880" /> 

    ! <- service publishing configuration, needs to be exposed service interface - -> 
    < Dubbo:-Service
             interface = "com.sihai.dubbo.provider.service.ProviderService" 
            REF = "providerService"/>

    <!--Bean bean定义-->
    <bean id="providerService" class="com.sihai.dubbo.provider.service.ProviderServiceImpl"/>

</beans>

Focus on this sentence

<dubbo:registry address="zookeeper://localhost:2181" />

In the address, use the address of our zookeeper.

If zookeeper cluster  , then use the following way. 

<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>

Configure the server just fine, with the other entry cases  the same. 

1.2 Consumer end

Like the server, in the consumer end, we just need to change  to. consumer.xml

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xmlns: Dubbo =" http://code.alibabatech.com/schema/dubbo " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTP: // the WWW. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd springframework.org/schema/beans/spring-beans.xsd "> 

    <-! current in the name of the entire project unique distributed architecture which is calculated dependent on the relationship between the label -> 
    <Dubbo: the Application name = "Consumer" owner = "Sihai" /> 

    ! <- Dubbo this service to be exposed to the service address corresponding to registry -> 
    <!- point to point -> 
    <- <Dubbo:! Registry address = "N / A" /> ->
    <dubbo:registry address="zookeeper://localhost:2181" check="false"/>

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
    <!--<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"
                     url="dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService"/>-->

    <dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>

</beans>

① registry configuration with the same server.

<dubbo:registry address="zookeeper://localhost:2181"/>

② dubbo: reference here because we use the registry as a zookeeper, so, with the way point is not the same, this is no longer the need dubbo url server provides the only direct reference to the need to provide an interface to the server.

<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>

Well, the consumer side is also configured, so that you can use to modify the entry case  , restart the run. 

The same success.

The difference is that this time,  the registration dubbo released url to the zookeeper, zookeeper from the consumer end consumer, zookeeper equivalent to an intermediary, to provide consumers with services.

You think this is over? No, the real fun has just begun.

Second, a variety of configurations

In the introductory example  , when we use the xml configuration  mode for dubbo environment configured, however, the official also provides other configurations, where we break down one by one.  

2.1 API configuration

In fact, the official in this way is not recommended  , the official recommended use xml configuration  , however, when some of the test, or can be used to, and the other, in order to guarantee the integrity, the content is still necessary to talk about of.  

First comes back to the server project.

Server

 

这里我们使用 api 的方式 配置,所以, provider.xml 这个配置文件就暂时不需要了,我们只需要在上面的 AppApi 这个类中的 main 方法中用 api配置及启动即可。

package com.sihai.dubbo.provider;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.sihai.dubbo.provider.service.ProviderService;
import com.sihai.dubbo.provider.service.ProviderServiceImpl;

import java.io.IOException;

/**
 * Api方式启动
 * api的方式调用不需要其他的配置,只需要下面的代码即可。
 * 但是需要注意,官方建议:
 * Api方式用于测试用例使用,推荐xml的方式
 */
public class AppApi
{
    public static void main( String[] args ) throws IOException {

        // 服务实现
        ProviderService providerService = new ProviderServiceImpl();

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

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

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

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

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

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

分析

看到上面的代码是不是云里雾里,不要慌,我们通过对照 xml 的方式分析一下。

registry 的 xml 方式

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

API 的方式

RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://localhost:2181");

dubbo:registry 节点对应 RegistryConfig ,xml 的 属性 对应 API 方式用 set 方法就可以了。对比之下,你就会发现,如果 API 的方式不熟悉,可以对照 xml 配置方式就可以。

其他 API

org.apache.dubbo.config.ServiceConfig
org.apache.dubbo.config.ReferenceConfig
org.apache.dubbo.config.ProtocolConfig
org.apache.dubbo.config.RegistryConfig
org.apache.dubbo.config.MonitorConfig
org.apache.dubbo.config.ApplicationConfig
org.apache.dubbo.config.ModuleConfig
org.apache.dubbo.config.ProviderConfig
org.apache.dubbo.config.ConsumerConfig
org.apache.dubbo.config.MethodConfig
org.apache.dubbo.config.ArgumentConfig

更详细的可以查看官方文档: dubbo.apache.org/zh-cn/docs/…

我们再看看我配置的消费端的 Api 方式。

消费端

同样,我们不需要 consumer.xml 配置文件了,只需要在 main 方法中启动即可。

package com.sihai.dubbo.consumer;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.sihai.dubbo.provider.service.ProviderService;

/**
 * api的方式调用
 * api的方式调用不需要其他的配置,只需要下面的代码即可。
 * 但是需要注意,官方建议:
 * Api方式用于测试用例使用,推荐xml的方式
 */
public class AppApi {

    public static void main(String[] args) {
        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        application.setOwner("sihai");

        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://localhost:2181");

        // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接

        // 引用远程服务
        ReferenceConfig<ProviderService> reference = new ReferenceConfig<ProviderService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
        reference.setApplication(application);
        reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
        reference.setInterface(ProviderService.class);

        // 和本地bean一样使用xxxService
        ProviderService providerService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
        providerService.SayHello("hello dubbo! I am sihai!");
    }
}

这部分的 API 配置的方式就到这了,注意: 官方推荐 xml 的配置方法 。

2.2 注解配置方式

注解配置方式还是需要了解一下的,现在微服务都倾向于这种方式,这也是以后发展的趋势,0 配置应该是这几年的趋势。

那么如何对 dubbo 使用注解的方式呢?我们先看服务端。

服务端

第一步:定义接口及实现类,在上面的截图中的 annotation 包下

package com.sihai.dubbo.provider.service.annotation;

/**
 * 注解方式接口
 */
public interface ProviderServiceAnnotation {
    String SayHelloAnnotation(String word);
}
package com.sihai.dubbo.provider.service.annotation;

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

/**
 * 注解方式实现类
 */
@Service(timeout = 5000)
public class ProviderServiceImplAnnotation implements ProviderServiceAnnotation{

    public String SayHelloAnnotation(String word) {
        return word;
    }
}

@Service

@Service 用来配置 Dubbo 的服务提供方。

**第二步:组装服务提供方。**通过 Spring 中 Java Config 的技术( @Configuration )和 annotation 扫描( @EnableDubbo )来发现、组装、并向外提供 Dubbo 的服务。

package com.sihai.dubbo.provider.configuration;

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;

/**
 * 注解方式配置
 */
@Configuration
@EnableDubbo(scanBasePackages = "com.sihai.dubbo.provider.service.annotation")
public class DubboConfiguration {

    @Bean // #1 服务提供者信息配置
    public ProviderConfig providerConfig() {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(1000);
        return providerConfig;
    }

    @Bean // #2 分布式应用信息配置
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("dubbo-annotation-provider");
        return applicationConfig;
    }

    @Bean // #3 注册中心信息配置
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("localhost");
        registryConfig.setPort(2181);
        return registryConfig;
    }

    @Bean // #4 使用协议配置,这里使用 dubbo
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        return protocolConfig;
    }
}

分析

  • 通过 @EnableDubbo 指定在 com.sihai.dubbo.provider.service.annotation 下扫描所有标注有 @Service 的类
  • 通过 @Configuration 将 DubboConfiguration 中所有的 @Bean 通过 Java Config 的方式组装出来并注入给 Dubbo 服务,也就是标注有 @Service 的类。这其中就包括了:
    • ProviderConfig:服务提供方配置
    • ApplicationConfig:应用配置
    • RegistryConfig:注册中心配置
    • ProtocolConfig:协议配置

看起来很复杂,其实。。。

第三步:启动服务

package com.sihai.dubbo.provider;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.sihai.dubbo.provider.configuration.DubboConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import sun.applet.Main;

import java.io.IOException;

/**
 * 注解启动方式
 */
public class AppAnnotation {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboConfiguration.class); 
        context.start();
        System.in.read(); 
    }
}

发现输出下面信息就表示 success 了。

消费端

同样我们下看看消费端的工程,有一个感性认识。

第一步:引用服务

package com.sihai.dubbo.consumer.Annotation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sihai.dubbo.provider.service.annotation.ProviderServiceAnnotation;
import org.springframework.stereotype.Component;

/**
 * 注解方式的service
 */
@Component("annotatedConsumer")
public class ConsumerAnnotationService {

    @Reference
    private ProviderServiceAnnotation providerServiceAnnotation;

    public String doSayHello(String name) {
        return providerServiceAnnotation.SayHelloAnnotation(name);
    }
}

 ConsumerAnnotationService 类中,通过 @Reference 引用服务端提供的类,然后通过方法调用这个类的方式,给消费端提供接口。 **注意:**如果这里找不到 ProviderServiceAnnotation 类,请在服务端先把服务端工程用 Maven intall 一下,然后将服务端的依赖放到消费端的 pom 中。如下:

<dependency>
          <groupId>com.ouyangsihai</groupId>
            <artifactId>dubbo-provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

第二步:组装服务消费者这一步和服务端是类似的,这里就不在重复了。

package com.sihai.dubbo.consumer.configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
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;

import java.util.HashMap;
import java.util.Map;

/**
 * 注解配置类
 */
@Configuration
@EnableDubbo(scanBasePackages = "com.sihai.dubbo.consumer.Annotation")
@ComponentScan(value = {"com.sihai.dubbo.consumer.Annotation"})
public class ConsumerConfiguration {
    @Bean // 应用配置
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("dubbo-annotation-consumer");
        Map<String, String> stringStringMap = new HashMap<String, String>();
        stringStringMap.put("qos.enable","true");
        stringStringMap.put("qos.accept.foreign.ip","false");
        stringStringMap.put("qos.port","33333");
        applicationConfig.setParameters(stringStringMap);
        return applicationConfig;
    }

    @Bean // 服务消费者配置
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

    @Bean // 配置注册中心
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("localhost");
        registryConfig.setPort(2181);
        return registryConfig;
    }
}

第三步:发起远程调用

 main 方法中通过启动一个 Spring Context ,从其中查找到组装好的 Dubbo 的服务消费者,并发起一次远程调用。

package com.sihai.dubbo.consumer;

import com.sihai.dubbo.consumer.Annotation.ConsumerAnnotationService;
import com.sihai.dubbo.consumer.configuration.ConsumerConfiguration;
import com.sihai.dubbo.provider.service.ProviderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * 注解方式启动
 *
 */
public class AppAnnotation
{
    public static void main( String[] args ) throws IOException {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); 
        context.start(); // 启动
        ConsumerAnnotationService consumerAnnotationService = context.getBean(ConsumerAnnotationService.class); 
        String hello = consumerAnnotationService.doSayHello("annotation"); // 调用方法
        System.out.println("result: " + hello); // 输出结果

    }
}

结果

三、常用场景

在下面的讲解中,都会是以 xml 配置的方式来讲解的,这也是 dubbo 官方比较推荐的方式。以下的操作都是在服务端的 xml 配置文件和消费端的配置文件来讲解的。

3.1 启动时检查

Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 `check="true"。

但是,有的时候,我们并不是都需要启动时就检查的,比如测试的时候,我们是需要更快速的启动,所以,这种场景的时候,我们是需要关闭这个功能的。

下面,我们看看如何使用这个功能。

在服务端注册的时候(客户端注册时同样适用);

<dubbo:registry protocol="zookeeper" address="localhost:2181,localhost:2182,localhost:2183" check="false"/>

在客户端引用服务端服务的时候;

<dubbo:reference check="false" id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>

就是这么简单,就是这么强!

3.2 集群容错

dubbo 也是支持集群容错的,同时也有很多可选的方案,其中,默认的方案是 failover ,也就是重试机制。

首先,我们先把所有的容错机制都整理一遍,然后再看看使用。

使用实例在发布服务或者引用服务的时候设置

<!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service cluster="failover" retries="2"
            interface="com.sihai.dubbo.provider.service.ProviderService"
            ref="providerService"/>
<dubbo:reference cluster="failover" retries="2" check="false" id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>

3.3 负载均衡

负载均衡想必是一个再熟悉不过的概念了,所以,dubbo 支持也是再正常不过了,这里也总结一下 dubbo 支持的负载均衡的一些方案及使用方法。

3.4 直连提供者

在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,所以,这种情况下,我们只需要直接连接服务端的地即可,其实,这种方法在前面的讲解已经使用到了,第一种讲解的方式就是这种方式,因为这种方式简单。

使用

<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"
                     url="dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService"/>

说明:可以看到,只要在消费端在 dubbo:reference 节点使用 url 给出服务端的方法即可。

 

3.5 只订阅

只订阅就是只能够订阅服务端的服务,而不能够注册。

引用官方的使用场景如下:

为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行。 可以让服务提供者开发方,只订阅服务(开发的服务可能依赖其它服务),而不注册正在开发的服务,通过直连测试正在开发的服务。

 

<dubbo:registry register="false" protocol="zookeeper" address="localhost:2181,localhost:2182,localhost:2183" check="false"/>

① 使用只订阅方式

当在服务提供端使用 register="false" 的时候,我们使用下面的方式获取服务端的服务;

<dubbo:reference cluster="failover" retries="2" check="false" id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>

启动信息

发现,这时候并不是向 注册中心 zookeeper 注册,而只是做了 发布服务和启动netty

 

② 不使用只订阅方式

<dubbo:registry protocol="zookeeper" address="localhost:2181,localhost:2182,localhost:2183" check="false"/>

启动信息

可以发现,这里就向注册中心 zookeeper 注册了。

3.6 只注册

只注册正好跟前面的只订阅相反,这个时候可以向注册中心注册,但是,消费端却不能够读到服务。

应用场景

如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部署,另一个注册中心还没来得及部署,而两个注册中心的其它应用都需要依赖此服务。这个时候,可以让服务提供者方只注册服务到另一注册中心,而不从另一注册中心订阅服务。

使用说明

<dubbo:registry subscribe="false" address="localhost:2181"></dubbo:registry>

在服务端的 dubbo:registry 节点下使用 subscribe="false" 来声明这个服务是只注册的服务。

这个时候消费端调用的时候是不能调用的。

3.7 多协议机制

在前面我们使用的协议都是 dubbo 协议,但是 dubbo 除了支持这种协议外还支持其他的协议,比如,rmi、hessian等,另外,而且还可以用多种协议同时暴露一种服务。

使用方法

① 一种接口使用一种协议

先声明多种协议

<!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:protocol name="rmi" port="1099" />

然后在发布接口的时候使用具体协议

<!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service cluster="failover" retries="2"
            interface="com.sihai.dubbo.provider.service.ProviderService"
            ref="providerService"/>
    <dubbo:service cluster="failover" retries="2"
                   interface="com.sihai.dubbo.provider.service.ProviderService"
                   ref="providerService" protocol="rmi"/>

在输出日志中,就可以找到rmi发布的接口。

rmi://192.168.234.1:1099/com.sihai.dubbo.provider.service.ProviderService?anyhost=true&application=provider&bean.name=com.sihai.dubbo.provider.service.ProviderService&cluster=failover&dubbo=2.0.2&generic=false&interface=com.sihai.dubbo.provider.service.ProviderService&methods=SayHello&owner=sihai&pid=796&retries=2&side=provider&timestamp=1564281053185, dubbo version: 2.6.6, current host: 192.168.234.1

② 一种接口使用多种协议声明协议和上面的方式一样,在发布接口的时候有一点不一样。

<dubbo:service cluster="failover" retries="2"
                   interface="com.sihai.dubbo.provider.service.ProviderService"
                   ref="providerService" protocol="rmi,dubbo"/>

**说明:**protocol属性,可以用 , 隔开,使用多种协议。

3.8 多注册中心

Dubbo 支持同一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心上去,甚至可以同时引用注册在不同注册中心上的同名服务。

服务端多注册中心发布服务

一个服务可以在不同的注册中心注册,当一个注册中心出现问题时,可以用其他的注册中心。

注册

<!--多注册中心-->
    <dubbo:registry protocol="zookeeper" id="reg1" timeout="10000" address="localhost:2181"/>
    <dubbo:registry protocol="zookeeper" id="reg2" timeout="10000" address="localhost:2182"/>
    <dubbo:registry protocol="zookeeper" id="reg3" timeout="10000" address="localhost:2183"/>

发布服务

<!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service cluster="failover" retries="2"
            interface="com.sihai.dubbo.provider.service.ProviderService"
            ref="providerService" registry="reg1"/>
    <dubbo:service cluster="failover" retries="2"
                   interface="com.sihai.dubbo.provider.service.ProviderService"
                   ref="providerService" protocol="rmi" registry="reg2"/>

**说明:**使用 registry="reg2" 指定该接口使用的注册中心,同时也可以使用多个,用 隔开,例如, registry="reg1,,reg2" 。

消费端多注册中心引用服务

首先,先向不同注册中心注册;

<!--多注册中心-->
    <dubbo:registry protocol="zookeeper" id="reg1" timeout="10000" address="localhost:2181"/>
    <dubbo:registry protocol="zookeeper" id="reg2" timeout="10000" address="localhost:2182"/>
    <dubbo:registry protocol="zookeeper" id="reg3" timeout="10000" address="localhost:2183"/>

其次,不同的消费端服务引用使用不同的注册中心;

<!--不同的服务使用不同的注册中心-->
    <dubbo:reference cluster="failover" retries="2" check="false" id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService" registry="reg1"/>
    <dubbo:reference cluster="failover" retries="2" check="false" id="providerService2"
                     interface="com.sihai.dubbo.provider.service.ProviderService" registry="reg2"/>

**说明:**上面分别使用注册中心1和注册中心2。

3.9 多版本

不同的服务是有版本不同的,版本可以更新并且升级,同时,不同的版本之间是不可以调用的。

<!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service cluster="failover" retries="2"
            interface="com.sihai.dubbo.provider.service.ProviderService"
            ref="providerService" registry="reg1" version="1.0.0"/>
    <dubbo:service cluster="failover" retries="2"
                   interface="com.sihai.dubbo.provider.service.ProviderService"
                   ref="providerService" protocol="rmi" registry="reg2" version="1.0.0"/>

加入了版本控制。

3.10 日志管理

dubbo 也可以将日志信息记录或者保存到文件中的。

① 使用accesslog输出到log4j

<dubbo:protocol accesslog="true" name="dubbo" port="20880"/>
    <dubbo:protocol accesslog="true" name="rmi" port="1099" />

② 输出到文件

<dubbo:protocol accesslog="http://localhost/log.txt" name="dubbo" port="20880"/>
    <dubbo:protocol accesslog="http://localhost/log2.txt" name="rmi" port="1099" />

四、总结

这系列的文章就到这里了,主要讲了一下几个内容 1、为什么需要dubbo 2、dubbo架构简析 3、dubbo入门 4、zookeeper注册中心加入dubbo 5、dubbo多种配置方式(xml、api、注解) 6、常用场景介绍 下一篇文章,将讲讲源码分析。

文章有不当之处,欢迎指正,如果喜欢微信阅读,你也可以关注我的 微信公众号 : Java技术zhai ,获取优质学习资源。

免费Java高级资料需要自己领取,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共40G。 
传送门: https://mp.weixin.qq.com/s/DHCu6AkF4nz4hNOv9c0IZg

 

Guess you like

Origin www.cnblogs.com/lfs2640666960/p/11283592.html