环境
spring cloud gateway
K8S
服务是nodeport方式暴露出去,没有用ingress
K8S集群需要配置 否则也是获取不到真实IP
我这边是用nodeport方式暴露服务修改如下
spec:
clusterIP: 10.43.72.235
clusterIPs:
- 10.43.72.235
externalTrafficPolicy: Local
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: p9090
nodePort: 31090
port: 9090
protocol: TCP
targetPort: 9090
selector:
workload.user.cattle.io/workloadselector: gateway-service
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
java获取ip代码
mport com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author: chenweida