dubbo参数详细说明

/ Dubbo / 1 条评论 / 929浏览

在上一篇中我们介绍了多种dubbo的相关配置,并且通过这些不同的配置已经成功的访问了我们需要的服务,在这一篇中我们主要介绍dubbo参数相关的验证,因为几乎所有的dubbo参数,官方文档中介绍的都比较详细,所以本篇重点,将已实际的参数验证为主,而有关参数的详细说明,说大家查阅dubbo官方文档。


dubbo默认情况下会在启动时检查依赖的服务是否可⽤, 如果服务不可⽤则会抛出异常, 并且会阻止项目的正常启用。 当然我们也可能通过参数修改默认设置,也就是check参数,check默认为true也就是会检查依赖是否可用,如果修改为false,则不会检查依赖服务是否可用,但在服务调用时,会抛出异常,并不会阻止项目的启动。下面我们通简单的示例来验证check参数的作用。

provider.xml源码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方信息-->
    <dubbo:application name="jilinwula" />

    <!-- 使用zookeeper暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 暴露服务端口 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接⼝ -->
    <dubbo:service interface="com.jilinwula.api.HelloWorld" ref="helloWorldImpl" />

    <!-- component-scan自动扫描注解 -->
    <context:component-scan base-package="com.jilinwula.*"/>

</beans>

Provider源码:

/**
 * 启动Dubbo提供方
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.in.read();
    }
}

consumer.xml源码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 调用方信息-->
    <dubbo:application name="jilinwula" />

    <!-- 使用zookeeper查找服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- Dubbo自动生成远程服务代理 -->
    <dubbo:reference id="helloWorld" interface="com.jilinwula.api.HelloWorld"/>

</beans>

Consumer源码:

/**
 * 服务调用方
 */
public class Consumer {
    public static void main(String[] args) throws IOException, InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
        context.start();

        HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);

    }
}

由于我们在consumer.xml文件中使用的是默认配置,所以check参数为true,也就是默认会检查依赖的服务是否可用,下面我们为了演示服务不可用,我们只启动Consumer,而不启用Provider,来看一下日志输出。日志输出如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8508®ister.ip=192.168.5.179&side=consumer×tamp=1531292774999 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
	at com.jilinwula.consumer.Consumer.main(Consumer.java:16)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8508®ister.ip=192.168.5.179&side=consumer×tamp=1531292774999 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:59)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
	... 6 more

这也就是证明了check参数确实是在启动的时候,检查服务是否可用,因为我们没有启动Provider,所以报上述错误,是正常的,下面我们将check参数修改为false,来继续看一下日志输出。日志输出如下:

2018-07-11 15:09:08:085 INFO [ProductBossService,org.I0Itec.zkclient.ZkClient,713]: zookeeper state changed (SyncConnected)
2018-07-11 15:09:08:152 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,272]:  [DUBBO] Register: consumer://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=consumers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:326 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,295]:  [DUBBO] Subscribe: consumer://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=providers,configurators,routers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:550 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,383]:  [DUBBO] Notify urls for subscribe url consumer://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=providers,configurators,routers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, urls: [empty://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=providers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, empty://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=configurators&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, empty://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=routers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973], dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:554 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000®ister.ip=192.168.5.179&side=consumer×tamp=1531292946973, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:565 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:565 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistryFactory,64]:  [DUBBO] Close all registries [zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.alibaba.dubbo.registry.RegistryService&pid=8000×tamp=1531292947075], dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:565 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,450]:  [DUBBO] Destroy registry:zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.alibaba.dubbo.registry.RegistryService&pid=8000×tamp=1531292947075, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:565 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,459]:  [DUBBO] Destroy unregister url consumer://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=consumers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:566 INFO [ProductBossService,com.alibaba.dubbo.registry.support.AbstractRegistry,475]:  [DUBBO] Destroy unsubscribe url consumer://192.168.5.179/com.jilinwula.api.HelloWorld?application=jilinwula&category=providers,configurators,routers&check=false&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8000&side=consumer×tamp=1531292946973, dubbo version: 2.6.0, current host: 192.168.5.179
2018-07-11 15:09:08:566 INFO [ProductBossService,org.I0Itec.zkclient.ZkEventThread,83]: Terminate ZkClient event thread.
2018-07-11 15:09:08:607 INFO [ProductBossService,org.apache.zookeeper.ZooKeeper,684]: Session: 0x1647dbfff9e006a closed

我们看日志没有报错了,这就说明当将参数check修改为false时,dubbo在启动时,并不会检查依赖的服务是否可用。下面我们修改一下Consumer类来证明我们上面提到的,check参数为false时,虽然不会检查依赖的服务是否可用,但在服务真正被调用时,如果服务不可用,那么dubbo还是会抛出异常。Consumer代码修改如下:

/**
 * 服务调用方
 */
public class Consumer {
    public static void main(String[] args) throws IOException, InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
        context.start();

        HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);

        System.out.println(helloWorld.getHelloWorld());
    }
}

我们只新增了一行代码,也就是调用服务的。下面我们继续看日志。日志输出如下:

Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.jilinwula.api.HelloWorld on consumer 192.168.5.179 use dubbo version 2.6.0, may be providers disabled or not registered ?
	at com.alibaba.dubbo.registry.integration.RegistryDirectory.doList(RegistryDirectory.java:572)
	at com.alibaba.dubbo.rpc.cluster.directory.AbstractDirectory.list(AbstractDirectory.java:73)
	at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.list(AbstractClusterInvoker.java:265)
	at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:224)
	at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:70)
	at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:51)
	at com.alibaba.dubbo.common.bytecode.proxy0.getHelloWorld(proxy0.java)
	at com.jilinwula.consumer.Consumer.main(Consumer.java:18)

我们看日志又报错了,虽然我们已经将check参数为false,但dubbo还是抛出了异常,并且,上述异常在实际的开发中经常遇到,如果不幸遇到了上述异常,那我们可以按照上面的思路去解决,基本原因,就是服务不可用。


我们在实际的开发中,通常注册中心有很多个服务,但有时基于测试等原因,希望能够直接连接提供者测试,而不影响注册中心中其它的服务调用。dubbo为了满足我们这种需要,于是提供了直连提供者的功能,就是它允许我们直接连接提供者的服务,而忽略注册中心提供者的服务,从而满足我们的需要。下面我们看具体直连的配置。

consumer.xml源码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 调用方信息-->
    <dubbo:application name="jilinwula" />

    <!-- 使用zookeeper查找服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- Dubbo自动生成远程服务代理 -->
    <dubbo:reference id="helloWorld" interface="com.jilinwula.api.HelloWorld" url="dubbo://127.0.0.1:20880"/>

</beans>

Provider源码:

/**
 * 启动Dubbo提供方
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.in.read();
    }
}

日志输出:

2018-07-11 15:47:20:858 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url dubbo://127.0.0.1:20880/com.jilinwula.api.HelloWorld?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=8836®ister.ip=192.168.5.179&side=consumer×tamp=1531295240251, dubbo version: 2.6.0, current host: 192.168.5.179
Hello World Dubbo
2018-07-11 15:47:21:106 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.5.179

日志显示我们已经调用成功了,因为我们采用就直连提供者,所以我们也可能将consumer.xml中的连接zookeeper中的代码去掉,服务也依然可能调用成功。

consumer.xml源码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 调用方信息-->
    <dubbo:application name="jilinwula" />

    <!-- Dubbo自动生成远程服务代理 -->
    <dubbo:reference id="helloWorld" interface="com.jilinwula.api.HelloWorld" url="dubbo://127.0.0.1:20880"/>

</beans>

日志输出:

2018-07-11 16:14:47:391 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url dubbo://127.0.0.1:20880/com.jilinwula.api.HelloWorld?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=5988®ister.ip=192.168.5.179&side=consumer×tamp=1531296886664, dubbo version: 2.6.0, current host: 192.168.5.179
Hello World Dubbo
2018-07-11 16:14:47:566 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.5.179

我们在实际的开发中,可能服务与服务之间有依赖关系,并且某些服务可能还没有开发完,而另一个服务已经开发完,且已经在功的的注册到了注册中心中,但由于有的服务没有开发完,所以我们并不想让调用者调用没有开发完该服务,而服务报供者又依赖此服务,这时我们就可以使用dubbo中的只订阅功能,当使用此功能时,调用方无法调用该服务,而服务方则可正常依赖。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方信息-->
    <dubbo:application name="jilinwula" />

    <!-- 使用zookeeper暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" register="false"/>

    <!-- 暴露服务端口 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接⼝ -->
    <dubbo:service interface="com.jilinwula.api.HelloWorld" ref="helloWorldImpl"/>

    <!-- component-scan自动扫描注解 -->
    <context:component-scan base-package="com.jilinwula.*"/>

</beans>

当我们配置register为false时,则表示该服务为只订阅模式,如果调用方此时调用该服务时,则会抛出异常。

日志输出:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2996®ister.ip=192.168.5.179&side=consumer×tamp=1531301106151 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
	at com.jilinwula.consumer.Consumer.main(Consumer.java:16)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2996®ister.ip=192.168.5.179&side=consumer×tamp=1531301106151 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:59)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
	... 6 more

如果我们什么都不管,死活都要调用这个未开发好的服务,那我们还是可以用直连的方式,调用这个只订阅的服务的。


如果我们希望⼈⼯控制服务提供者的上线和下线时,那我们可能将注册中⼼设置为⾮动态管理模式。也就是dynamic参数。下面我具体的配置:

provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方信息-->
    <dubbo:application name="jilinwula"/>

    <!-- 使用zookeeper暴露服务地址 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper" dynamic="false"/>

    <!-- 暴露服务端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 声明需要暴露的服务接⼝ -->
    <dubbo:service interface="com.jilinwula.api.HelloWorld" ref="helloWorldImpl"/>

    <!-- component-scan自动扫描注解 -->
    <context:component-scan base-package="com.jilinwula.*"/>

</beans>

当我们设置dynamic为false时,在dubbo-admin中可以查看到该服务的状态已经表示为静态类型了。如下图所示:

img

如果此时,我们在消费端调用服务时,则可能正确返回结果。如果我们将状态修改为禁用时,如果此时消费端在调用接口时,则会报错。

img

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=5440®ister.ip=192.168.5.179&side=consumer×tamp=1531389762476 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
	at com.jilinwula.consumer.Consumer.main(Consumer.java:16)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=5440®ister.ip=192.168.5.179&side=consumer×tamp=1531389762476 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:59)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
	... 6 more

如果此时,我们将提供方的服务删除后,即使提供方的服务,正在运行,则消费方,还是会抛出异常。

img

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloWorld': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=14676®ister.ip=192.168.5.179&side=consumer×tamp=1531390080183 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
	at com.jilinwula.consumer.Consumer.main(Consumer.java:16)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.jilinwula.api.HelloWorld. No provider available for the service com.jilinwula.api.HelloWorld from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=jilinwula&dubbo=2.6.0&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=14676®ister.ip=192.168.5.179&side=consumer×tamp=1531390080183 to the consumer 192.168.5.179 use dubbo version 2.6.0
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:59)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
	... 6 more

下面我们看一下动态管理模式,也就是将参数dynamic设置为true,也是默认值。还是和静态模式一样,当服务提供方启动成功后,dubbo-admin中服务类型标识为动态模式,并且默认启用。

img

当然,如果我们此时调用消费端,依然可以成功调用服务。如果此时将服务禁用,则消费端同样会抛出异常,这里基本和静态服务一样,但有一点区别,就是动态模式下,服务是不支持删除功能。但静态服务则可以删除。

img


上述内容就是本篇的全部内容,因为dubbo参数比较多,所以下一篇中,我们将继续学历dubbo参数的内容。

  1. 写的很好 要是有源码分析 就更牛XXXXX了

    回复