All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fun.fengwk.gateway.share.context.GatewayContextImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package fun.fengwk.gateway.share.context;

import fun.fengwk.gateway.share.constant.GatewayBaggageKey;
import io.opentracing.Span;
import io.opentracing.util.GlobalTracer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Function;

/**
 * 使用Tracer实现的网关上下文
 *
 * @author fengwk
 */
@Slf4j
@AllArgsConstructor
public class GatewayContextImpl implements GatewayContext {

    @Override
    public String getRealIp() {
        return getBaggageValue(GatewayBaggageKey.REAL_IP, Function.identity());
    }

    @Override
    public String getAccessUserNamespace() {
        return getBaggageValue(GatewayBaggageKey.ACCESS_USER_NAMESPACE, Function.identity());
    }

    @Override
    public Long getAccessUserId() {
        return getBaggageValue(GatewayBaggageKey.ACCESS_USER_ID, Long::parseLong);
    }

    private  T getBaggageValue(GatewayBaggageKey key, Function typeConverter) {
        Span activeSpan = GlobalTracer.get().activeSpan();
        if (activeSpan == null) {
            return null;
        }
        String value = activeSpan.getBaggageItem(key.getCode());
        if (value == null) {
            return null;
        }
        return typeConverter.apply(value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy