hsf框架,提供服务者,怎么获取调用本服务的调用者的ip地址?

@Slf4j
@HSFProvider(serviceInterface = YwcxService.class,serviceVersion = "1.0.0")
public class YwcxServiceImpl implements YwcxService {
    @Override
    public String inster(List<YwcxQuery> ywcxQuerylist) {
        log.info("localAddress:{},remoteAssress:{}",RpcContext.getContext().getLocalAddress(),RpcContext.getContext().getRemoteAddress());
//RpcContext.getContext().getRemoteAddress() 为空为什么?
        //业务逻辑……
    }
}

//服务B
@Scheduled(cron = "${task.cron.runTaskHuayu}")
public String dsrw(){
    return YwcxService.inster(fqXcsqQuerylist);
}

//调用的RpcContext类,edas-sdk-1.8.3.jar

package com.alibaba.dubbo.rpc;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

public class RpcContext {
    private static final ThreadLocal<RpcContext> LOCAL = new ThreadLocal<RpcContext>() {
        protected RpcContext initialValue() {
            return new RpcContext();
        }
    };
    private Future<?> future;
    private String methodName;
    private Class<?>[] parameterTypes;
    private Object[] arguments;
    private InetSocketAddress localAddress;
    private InetSocketAddress remoteAddress;
    private final Map<String, String> attachments = new HashMap();
    private final Map<String, Object> values = new HashMap();
    private boolean isProvider;

    public static RpcContext getContext() {
        return (RpcContext)LOCAL.get();
    }

    public static void removeContext() {
        LOCAL.remove();
    }

    protected RpcContext() {
    }

    public boolean isProviderSide() {
        return this.isProvider;
    }

    public RpcContext setProviderSide(boolean isProvider) {
        this.isProvider = isProvider;
        return this;
    }

    public boolean isConsumerSide() {
        return false;
    }

    public <T> Future<T> getFuture() {
        return this.future;
    }

    public void setFuture(Future<?> future) {
        this.future = future;
    }

    public String getMethodName() {
        return this.methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Class<?>[] getParameterTypes() {
        return this.parameterTypes;
    }

    public void setParameterTypes(Class<?>[] parameterTypes) {
        this.parameterTypes = parameterTypes;
    }

    public Object[] getArguments() {
        return this.arguments;
    }

    public void setArguments(Object[] arguments) {
        this.arguments = arguments;
    }

    public RpcContext setLocalAddress(InetSocketAddress address) {
        this.localAddress = address;
        return this;
    }

    public RpcContext setLocalAddress(String host, int port) {
        if (port < 0) {
            port = 0;
        }

        this.localAddress = InetSocketAddress.createUnresolved(host, port);
        return this;
    }

    public InetSocketAddress getLocalAddress() {
        return this.localAddress;
    }

    public RpcContext setRemoteAddress(InetSocketAddress address) {
        this.remoteAddress = address;
        return this;
    }

    public RpcContext setRemoteAddress(String host, int port) {
        if (port < 0) {
            port = 0;
        }

        this.remoteAddress = InetSocketAddress.createUnresolved(host, port);
        return this;
    }

    public InetSocketAddress getRemoteAddress() {
        return this.remoteAddress;
    }

    public String getAttachment(String key) {
        return (String)this.attachments.get(key);
    }

    public RpcContext setAttachment(String key, String value) {
        if (value == null) {
            this.attachments.remove(key);
        } else {
            this.attachments.put(key, value);
        }

        return this;
    }

    public RpcContext removeAttachment(String key) {
        this.attachments.remove(key);
        return this;
    }

    public Map<String, String> getAttachments() {
        return this.attachments;
    }

    public RpcContext setAttachments(Map<String, String> attachment) {
        this.attachments.clear();
        if (attachment != null && attachment.size() > 0) {
            this.attachments.putAll(attachment);
        }

        return this;
    }

    public void clearAttachments() {
        this.attachments.clear();
    }

    public Map<String, Object> get() {
        return this.values;
    }

    public RpcContext set(String key, Object value) {
        if (value == null) {
            this.values.remove(key);
        } else {
            this.values.put(key, value);
        }

        return this;
    }

    public RpcContext remove(String key) {
        this.values.remove(key);
        return this;
    }

    public Object get(String key) {
        return this.values.get(key);
    }
}

别的服务调用YwcxService的inster方法的时候,怎么获取调用者ip,使用RpcContext.getContext().getRemoteAddress() 获取为null,为什么?

使用 RpcContext.getContext().getRemoteAddress() 获取,为空。

阅读 440
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题