• dubbo复习: (5)和springboot集成时的标签路由


    标签路由:服务提供者和服务消费者都可以指定标签。
    只有服务提供者的标签和服务消费者的标签一致时,才会进行请求路由。

    给服务提供者指定标签有两种方式,一种是通过在@DubboService注解的tag属性来指定,如下示例

    package cn.edu.tju.service;
    
    import org.apache.dubbo.config.annotation.DubboService;
    
    import java.util.Date;
    
    
    @DubboService(loadbalance = "",tag = "good")
    public class DemoServiceImpl implements DemoService {
    
        public String sayHello(String name) {
    
    
            return "Hello " + name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    另一种是通过dubbo admin进行动态指定,后者的优先级更高。
    如下示例:
    在这里插入图片描述

    force: false
    enabled: true
    runtime: false
    tags:
     - name: good
       addresses: [192.168.31.226:28093]
     - name: bad
       addresses: [192.168.31.226:28095]
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    如果consumer指定了标签为good,示例代码如下:

    package cn.edu.tju.service;
    
    import org.apache.dubbo.config.annotation.DubboReference;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.PostConstruct;
    import java.util.Date;
    
    @Service
    public class RemoteService {
    
        //@DubboReference(loadbalance = "roundrobin")
        @DubboReference(tag = "good")
        private DemoService demoService;
        
        public String  callRemoteService(){
            try {
                Thread. sleep(10);
                return new Date() + " Receive result ======> " + demoService.sayHello("world");
            } catch (InterruptedException e) {
                e.printStackTrace();
                return e.getMessage();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    ,则会将请求路由到192.168.31.226:28093

  • 相关阅读:
    ElasticSearch之score打分机制原理
    iOS性能优化
    Hello World!1分钟配置好你的Go环境
    [C++]set判断两个元素相等
    (附源码)springboot实验室预约管理系统 毕业设计 261141
    超全整理——相机标定知识汇总
    Go语言逃逸分析全纪录
    java计算机毕业设计客服管理系统源码+mysql数据库+系统+lw文档+部署
    linux内核编程13期:内存管理
    论文阅读 Streaming Graph Neural Networks
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/139172426