这段时间接手了一个公司的老项目,用的是SpringCloud,在我用的时候突然发现有一个服务,注册到Eureka后,界面显示的端口和实际Ribbon调用的实例端口是不一致的,后来我自己写了个端口获取了一下所有的实例信息,方法如下
@RestController
public class TestController {@Resourceprivate DiscoveryClient discoveryClient;@RequestMapping("getServicesList")@ResponseBodypublic Object getServicesList() {List<List<ServiceInstance>> servicesList = new ArrayList<>();//获取服务名称List<String> serviceNames = discoveryClient.getServices();for (String serviceName : serviceNames) {//获取服务中的实例列表List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);servicesList.add(serviceInstances);}return servicesList;}
}
这个可以获取当前注册到Eureka的所有实例信息。
我获取到以后发现端口和配置的端口不一致。
[{"host": "147.20.1.14","port": 8080,"serviceId": "IICS-TIMER","uri": "http://147.20.1.14:8080","metadata": {},"secure": false,"instanceInfo": {"instanceId": "147.20.1.14:8094","app": "IICS-TIMER","appGroupName": null,"ipAddr": "147.20.1.14","sid": "na","homePageUrl": "http://147.20.1.14:8080/","statusPageUrl": "http://147.20.1.14:8080/info","healthCheckUrl": "http://147.20.1.14:8080/health","secureHealthCheckUrl": null,"vipAddress": "iics-timer","secureVipAddress": "iics-timer","countryId": 1,"dataCenterInfo": {"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name": "MyOwn"},"hostName": "147.20.1.14","status": "UP","leaseInfo": {"renewalIntervalInSecs": 30,"durationInSecs": 90,"registrationTimestamp": 1678328929647,"lastRenewalTimestamp": 1678328929647,"evictionTimestamp": 0,"serviceUpTimestamp": 1678328878377},"isCoordinatingDiscoveryServer": false,"metadata": {},"lastUpdatedTimestamp": 1678328929647,"lastDirtyTimestamp": 1678328929069,"actionType": "ADDED","asgName": null,"overriddenStatus": "UNKNOWN"}
}]
通过这个可以看到实例端口和实例id是不一致的,后来通过查看源码发现了问题。
在Debug注册Eureka的时候我发现他获取了一个nonSecurePort的端口,然后通过点击发现他是获取的当前配置的server.port端口,也就是咱们设置的端口,如果没有获取到的话就用默认的8080。
不知道因为什么原因导致的eureka读取server.port不生效,但是通过查阅资料我们可以通过强制给nonSecurePort赋值的方式绑定端口。
eureka.instance.nonSecurePort=8094
直接在配置文件里添加这个配置,配置nonSecurePort参数即可。