• dubbo入门之微服务客户端服务端配置


    正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的dubbo入门之helloWorld,我们再改改配置,即可实现正常的微服务架构。与之前相比,我新增了一个既做客户端又做服务端的工程。

    HelloServerClientServiceImpl.java:

    package com.sawshaw.dubbo_server_client;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.sawshaw.dubbo_interface.HelloInterface;
    import com.sawshaw.dubbo_interface.HelloServerClientInterface;
    @Service
    public class HelloServerClientServiceImpl implements HelloServerClientInterface{
    	@Autowired
    	private HelloInterface hello;
    
    	public String sayHi(String name) {
    		System.out.println("hello server-client......client send name is "+name);
    		String result=hello.sayHello(name);
    		return "hello-server-client "+result;
    	}
    
    }
    

    provider.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="provider"/>
        <!-- 使用zookeeper注册中心暴露服务地址,这个地址是启动zookeeper默认暴露的地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <!-- 用dubbo协议在随机端口暴露服务 端口自己设置,如果被占用了就换一个端口 -->
       <dubbo:protocol name="dubbo" port="20881" />
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.sawshaw.dubbo_interface.HelloServerClientInterface" ref="hiService"/>
        <!-- 和本地bean一样实现服务 -->
        <bean id="hiService" class="com.sawshaw.dubbo_server_client.HelloServerClientServiceImpl"/>
    </beans>
    

    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">
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 ,要实现启动不报错要改成false-->
        <dubbo:application name="consumer" default="false"/>
        <!-- 连接注册中心配置 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper" check="false"/>
        <!-- 生成远程服务代理,可以和本地bean一样使用helloService  check="false" 启动时不检查依赖是否已经启动-->
        <dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface"  check="false"/>
    </beans>
    

      

      

    client调用,HelloClient.java:

    package com.sawshaw.dubbo_client;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.sawshaw.dubbo_interface.HelloInterface;
    import com.sawshaw.dubbo_interface.HelloServerClientInterface;
    
    
    public class HelloClient{
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		HelloInterface hs = (HelloInterface) context.getBean("helloService");
            String result = hs.sayHello("dubbo");
            System.out.println( "client output...."+result);
            HelloServerClientInterface hi = (HelloServerClientInterface) context.getBean("hiService");
            String result1 = hi.sayHi("dubbo");
            System.out.println( "client output...."+result1);
    	}
    
    }
    

     client调server-client,server-client调server,所以server-client既是客户端又是服务端。

    启动时检查

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

      <!-- 生成远程服务代理,可以和本地bean一样使用helloService  check="false" 启动时不检查依赖是否已经启动-->
        <dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface"  check="false"/>
    

     当一个应用既当提供者和消费者时,必定会分别配置应用的名称,这时启动应用时就会报错:

     java.lang.IllegalStateException: Duplicate application configs: <dubbo:application name="XXX" id="XXX" /> and <dubbo:application name="XXXX" id="XXXX" />

     解决办法是,在其中一个应用里面加上default="false",例如:

      <dubbo:application name="consumer" default="false"/>
    

      

    代码地址:链接:https://pan.baidu.com/s/1h7pNFCSprX51CwUv7vHPFw 密码:cu6r

  • 相关阅读:
    OCP-1Z0-053-V12.02-597题
    OCP-1Z0-053-V12.02-599题
    OCP-1Z0-053-V12.02-609题
    OCP-1Z0-053-V12.02-604题
    OCP-1Z0-053-V12.02-607题
    OCP-1Z0-053-V12.02-588题
    OCP-1Z0-053-V12.02-592题
    OCP-1Z0-053-V12.02-587题
    OCP-1Z0-053-V12.02-582题
    OCP-1Z0-053-V12.02-583题
  • 原文地址:https://www.cnblogs.com/JAYIT/p/9645728.html
Copyright © 2020-2023  润新知