Search in sources :

Example 1 with RpcTypeEnum

use of org.apache.shenyu.common.enums.RpcTypeEnum in project incubator-shenyu by apache.

the class ShenyuPlugin method skip.

/**
 * plugin is executed.
 * if return true this plugin can not execute.
 *
 * <p>the same for:
 * <pre>
 * <code>Objects.equals(rpcType, typeA.getName())
 * || Objects.equals(rpcType, typeB.getName())
 * || Objects.equals(rpcType, type...getName())
 * </code>
 * </pre>
 *
 * @param exchange the current server exchange
 * @param rpcTypes the skip rpc type list
 * @return current rpcType == someone rpcType
 */
default boolean skip(ServerWebExchange exchange, RpcTypeEnum... rpcTypes) {
    if (ArrayUtils.isEmpty(rpcTypes)) {
        return false;
    }
    ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    String rpcType = shenyuContext.getRpcType();
    for (final RpcTypeEnum type : rpcTypes) {
        if (Objects.equals(rpcType, type.getName())) {
            return true;
        }
    }
    return false;
}
Also used : ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum)

Example 2 with RpcTypeEnum

use of org.apache.shenyu.common.enums.RpcTypeEnum in project incubator-shenyu by apache.

the class NacosServerRegisterRepository method subscribeRpcTypeService.

private void subscribeRpcTypeService(final RpcTypeEnum rpcType) {
    final String serviceName = RegisterPathConstants.buildServiceInstancePath(rpcType.getName());
    try {
        Map<String, List<URIRegisterDTO>> services = new HashMap<>();
        List<Instance> healthyInstances = namingService.selectInstances(serviceName, true);
        healthyInstances.forEach(healthyInstance -> {
            String contextPath = healthyInstance.getMetadata().get("contextPath");
            String serviceConfigName = RegisterPathConstants.buildServiceConfigPath(rpcType.getName(), contextPath);
            subscribeMetadata(serviceConfigName);
            metadataConfigCache.add(serviceConfigName);
            String metadata = healthyInstance.getMetadata().get("uriMetadata");
            URIRegisterDTO uriRegisterDTO = GsonUtils.getInstance().fromJson(metadata, URIRegisterDTO.class);
            services.computeIfAbsent(contextPath, k -> new ArrayList<>()).add(uriRegisterDTO);
            uriServiceCache.computeIfAbsent(serviceName, k -> new ConcurrentSkipListSet<>()).add(contextPath);
        });
        if (RPC_URI_TYPE_SET.contains(rpcType)) {
            services.values().forEach(this::publishRegisterURI);
        }
        LOGGER.info("subscribe uri : {}", serviceName);
        namingService.subscribe(serviceName, event -> {
            if (event instanceof NamingEvent) {
                List<Instance> instances = ((NamingEvent) event).getInstances();
                instances.forEach(instance -> {
                    String contextPath = instance.getMetadata().get("contextPath");
                    uriServiceCache.computeIfAbsent(serviceName, k -> new ConcurrentSkipListSet<>()).add(contextPath);
                });
                refreshURIService(rpcType, serviceName);
            }
        });
    } catch (NacosException e) {
        throw new ShenyuException(e);
    }
}
Also used : Constants(org.apache.shenyu.common.constant.Constants) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) MetaDataRegisterDTO(org.apache.shenyu.register.common.dto.MetaDataRegisterDTO) ShenyuRegisterCenterConfig(org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig) Lists(com.google.common.collect.Lists) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Map(java.util.Map) NacosException(com.alibaba.nacos.api.exception.NacosException) ConfigFactory(com.alibaba.nacos.api.config.ConfigFactory) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) Listener(com.alibaba.nacos.api.config.listener.Listener) ConfigService(com.alibaba.nacos.api.config.ConfigService) Logger(org.slf4j.Logger) Properties(java.util.Properties) PropertyKeyConst(com.alibaba.nacos.api.PropertyKeyConst) Executor(java.util.concurrent.Executor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NacosPathConstants(org.apache.shenyu.common.constant.NacosPathConstants) ShenyuServerRegisterPublisher(org.apache.shenyu.register.server.api.ShenyuServerRegisterPublisher) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Join(org.apache.shenyu.spi.Join) Optional(java.util.Optional) RegisterPathConstants(org.apache.shenyu.register.common.path.RegisterPathConstants) NamingFactory(com.alibaba.nacos.api.naming.NamingFactory) ShenyuServerRegisterRepository(org.apache.shenyu.register.server.api.ShenyuServerRegisterRepository) NamingService(com.alibaba.nacos.api.naming.NamingService) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) ArrayList(java.util.ArrayList) NacosException(com.alibaba.nacos.api.exception.NacosException) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) List(java.util.List) ShenyuException(org.apache.shenyu.common.exception.ShenyuException)

Aggregations

RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)2 PropertyKeyConst (com.alibaba.nacos.api.PropertyKeyConst)1 ConfigFactory (com.alibaba.nacos.api.config.ConfigFactory)1 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NamingFactory (com.alibaba.nacos.api.naming.NamingFactory)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1