标签路由:服务提供者和服务消费者都可以指定标签。
只有服务提供者的标签和服务消费者的标签一致时,才会进行请求路由。
给服务提供者指定标签有两种方式,一种是通过在@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;
}
}
另一种是通过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]
如果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();
}
}
}
,则会将请求路由到192.168.31.226:28093