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的服务

  • 相关阅读:
    【IOS】ObjectC 中的Selector 概念
    iOS开发笔记 3、iOS基础
    iOS开发笔记 2、Cocoa简明
    iOS开发笔记 4、iOS中的Cocoa、设计模式等
    iOS开发笔记 5、开发工具Xcode,Inteface Builder
    iOS开发笔记 8、真机调试和发布软件
    移动App如何收费的模式和步骤分析
    带滚动条的二级黑色竖直菜单(CSS实现)
    很是动感的一款js伸缩、关闭菜单
    JS实现类似腾讯QQ折叠菜单
  • 原文地址:https://www.cnblogs.com/HigginCui/p/9973827.html
Copyright © 2011-2022 走看看