spring cloud解决eureka的client注册时默认使用hostname而不是ip


eureka的client注册到server时默认是使用hostname而不是ip。
这样的话使用feign client通过eureka进行服务间相互调用时也会使用hostname进行调用,从而调用失败。
报错如下 :
Caused by: java.net.UnknownHostException: xxxx-hostname
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
        at sun.net.www.http.HttpClient.New(HttpClient.java:339)
        at sun.net.www.http.HttpClient.New(HttpClient.java:357)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
        at feign.Client$Default.convertAndSend(Client.java:133)
        at feign.Client$Default.execute(Client.java:73)
        at org.springframework.cloud.sleuth.instrument.web.client.feign.TraceFeignClient.execute(TraceFeignClient.java:80)
        at org.springframework.cloud.netflix.feign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:77)
        at org.springframework.cloud.netflix.feign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:45)
        at com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:109)
        at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
        at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)
        at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:231)
        at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:228)
        at rx.Observable.unsafeSubscribe(Observable.java:10211)
        at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)
        at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144)
        at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
        at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
        at rx.Observable.unsafeSubscribe(Observable.java:10211)


解决方案:


1、使用ip来服务到eureka-server上,需要在eureka的client增加配置如下:

eureka.instance.prefer-ip-address=true 或  eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}     ##此行可加可不加,只是用于实例的显示

    feign接口示例:

@FeignClient("xxx-business-service-name")
public interface MyFeignClient {
......
}

2、使用域名方式调用,通过注解注入服务的域名,此处域名使用配置注入,是为了多环境适配

     feign接口示例,:

@FeignClient(name = "xxx-business-service-name", url = "http://${xxx.business.service.domain}")
public interface MyFeignClient {
......
}
 
 
posted @ 2019-07-23 20:55  xuzhujack  阅读(5695)  评论(0编辑  收藏  举报
;