zoukankan      html  css  js  c++  java
  • 01_dubbo实例_服务分组

    【为什么要服务分组?】

    当一个接口有多种实现时,可以用group区分。

    【 Provider 的配置信息】

    <?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-4.3.xsd
                                http://code.alibabatech.com/schema/dubbo
                                http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="hello-world-app"  />
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.higgin.dubbo.service.AnimalService" ref="catService" group="catGroup" />
        <dubbo:service interface="com.higgin.dubbo.service.AnimalService" ref="dogService" group="dogGroup" />
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="catService" class="com.higgin.dubbo.service.impl.CatServiceImpl"/>
        <bean id="dogService" class="com.higgin.dubbo.service.impl.DogServiceImpl"/>
    </beans>

    【Provider 启动服务】

    public static void main(String[] args) throws Exception{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            context.start();
            System.in.read();
        }

    【 Consumer 配置信息】

    <?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="dubbo-client" />
    
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
        
        <dubbo:reference interface="com.higgin.dubbo.service.AnimalService"
                         id="catService"
                         group="catGroup"/>
    
        <dubbo:reference interface="com.higgin.dubbo.service.AnimalService"
                         id="dogService"
                         group="dogGroup"/>
        
    </beans>

    【Consumer 消费服务】

    public static void main(String[] args) throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        AnimalService catService = context.getBean("catService",AnimalService.class);
        System.out.println(catService.sayHello());
    
        AnimalService dogService = context.getBean("dogService",AnimalService.class);
        System.out.println(dogService.sayHello());
    }

    【注意】

    Consumer只能消费与自己服务分组相同的Provider的服务

  • 相关阅读:
    ReentrantLock 使用
    IDEA artifacts Web Application:Exploded Web Application:Archive
    IntelliJ IDEA project module
    Rabbitmq 基本属性
    Spring ThreadPoolTaskExecutor
    Spring AsyncRestTemplate
    OpenOffice Word文档转换成Html格式
    dubbo 多协议和多注册中心
    linux 配置Tomcat开机启动
    dubbo通信协议
  • 原文地址:https://www.cnblogs.com/HigginCui/p/9973827.html
Copyright © 2011-2022 走看看