在Flink中,可以通过getRuntimeContext().getExecutionConfig().getGlobalJobParameters()
获取到JobManager的IP地址。这个方法返回一个org.apache.flink.configuration.Configuration
对象,然后通过getString(org.apache.flink.configuration.ConfigOptions.JOB_MANAGER_HOST)
方法获取到JobManager的IP地址。
示例代码如下:
import org.apache.flink.api.common.JobID;
import org.apache.flink.runtime.jobgraph.JobStatus;
import org.apache.flink.runtime.minicluster.MiniCluster;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;
public class FlinkGetJobManagerIP {
public static void main(String[] args) throws Exception {
// 创建流执行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// 添加数据源
env.addSource(new SourceFunction<String>() {
@Override
public void run(SourceContext<String> ctx) throws Exception {
// 获取JobManager的IP地址
String jobManagerHost = env.getRuntimeContext().getExecutionConfig().getGlobalJobParameters().getString(org.apache.flink.configuration.ConfigOptions.JOB_MANAGER_HOST);
System.out.println("JobManager IP: " + jobManagerHost);
}
@Override
public void cancel() {
}
}).print();
// 启动任务
env.execute("Flink Get JobManager IP");
}
}
这段代码会输出JobManager的IP地址。