使用spring cloud更改eureka服务器的默认端口

Posted

技术标签:

【中文标题】使用spring cloud更改eureka服务器的默认端口【英文标题】:Changing default port of eureka server using spring cloud 【发布时间】:2018-01-18 19:55:24 【问题描述】:

我得到了 spring-boot 应用程序、一个 eureka 服务器和一个 eureka 客户端。

这是我的服务器配置

server:
  port: 8761

spring:
  application:
    name: eureka-server

这是我的服务器代码

package fr.maif.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaServer
@EnableZuulProxy
public class EurekaServerApplication 

    public static void main(String[] args) 
        SpringApplication.run(EurekaServerApplication.class, args);
    

这是客户端配置

spring:
  application:
    name: eureka-client
server:
  port: 8083
eureka:
  client:
    serviceUrl:
      defaultZone: "http://localhost:8761/eureka"

这是我的客户端代码

package fr.maif.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication 

    public static void main(String[] args) 
        SpringApplication.run(EurekaClientApplication.class, args);
    

当服务器端口保持为 8761 时,上述内容完美运行,但是当我尝试在服务器端口和客户端 defaultZone 配置中更改它(更改为 8080 或任何其他值)时,服务器不断抛出 ConnectExceptions。

2017-08-10 15:54:31.424 ERROR 22219 --- [get_localhost-4] c.n.e.cluster.ReplicationTaskProcessor   : Network level connection to peer localhost; retrying after delay

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.6.2.jar:1.6.2]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.6.2.jar:1.6.2]
    at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.6.2.jar:1.6.2]
    at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:71) ~[eureka-core-1.6.2.jar:1.6.2]
    at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.6.2.jar:1.6.2]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_131]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_131]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_131]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_131]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_131]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_131]
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.3.jar:4.5.3]
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    ... 10 common frames omitted

我做错了什么?

【问题讨论】:

在新端口用浏览器访问eureka会发生什么?例如。本地主机:8080/ 您的评论帮助我解决了我的问题:我有 eureka IHM,但只有 eureka 客户端出现在注册应用程序(而不是服务器)中。我在我的服务器配置中添加了eureka.client.serviceUrl.defaultZone: "http://localhost:8080/eureka",它起作用了!非常感谢 【参考方案1】:

您只需在您的服务器 application.yml 文件中添加 eureka.client.service-url.defaultZone=http://localhost:8080/eureka 并删除两个文件中 url 周围的引号。

删除网址周围的引号(不要忘记)

【讨论】:

【参考方案2】:

是的。根据 Shchipunov 评论,以下属性可以完成这项工作 1-eureka.instance.hostname=localhost(尤里卡服务器) 2-eureka.client.serviceUrl.defaultZone(客户端项目)

【讨论】:

【参考方案3】:

试试这个配置: 1. 服务器。

server:
  port: 8080

eureka:
  instance:
   hostname: localhost
client:
  registerWithEureka: false
  fetchRegistry: false
  serviceUrl:
  defaultZone: http://$eureka.instance.hostname:$server.port/eureka/

2。客户。

spring:
  application:
  name: eureka-client
server:
  port: 8083
eureka:
  client:
  registerWithEureka: true
  fetchRegistry: true
  serviceUrl:
    defaultZone: http://localhost:8080/eureka/

【讨论】:

是的,eureka.client.serviceUrl.defaultZone 确实在服务器配置中丢失,如之前的评论中所述。感谢您的帮助 =) 也适合我。 我认为我们不需要在服务器中提供defaultZone。我只是在服务器的application.properties 文件中更改为server.port=3939,并在我的每个客户端以及ApiGateway 的application.properties 文件中配置eureka.client.serviceUrl.defaultZone,端口与服务器中提供的端口相同,即3939。这对我有用!

以上是关于使用spring cloud更改eureka服务器的默认端口的主要内容,如果未能解决你的问题,请参考以下文章

spring cloud 使用Eureka作为服务注册中心

基于Spring Cloud的微服务构建学习-3 Spring Cloud Eureka配置详解

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

spring cloud 之 Eureka