首页 > 解决方案 > 我们可以在同一个 SpringBootApplication 上配置 spring 云网关和服务发现(eureka 服务器)吗

问题描述

我正在尝试在同一个@SpringBootApplication 上运行 Spring Cloud“Gateway”和“Eureka-Server”,下面是我的代码:

主类:

@SpringBootApplication
@EnableEurekaServer
@EnableAdminServer
public class DemoGatewayApplication {

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

}

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
</dependencies>

应用程序.yml

server:
  port: ${GATEWAY_SERVER_PORT:8080}
             
eureka:
  instance:
    hostname: localhost
    appname: ${spring.application.name}
  client:
    enabled: true
    healthcheck:
      enabled: true
    fetch-registry: true
    register-with-eureka: true
    instance-info-replication-interval-seconds: 10
    registry-fetch-interval-seconds: 10
    service-url:
      defaultZone: http://${REGISTRY_SERVER_IP:localhost}:${REGISTRY_SERVER_PORT:8080}/eureka
   

当我运行此应用程序时,我收到如下错误。我不确定是什么问题,或者是否可以在同一实例上拥有网关和 eureka 服务器?

2021-09-13 18:10:12.411  INFO [gateway-service,,] 224 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_GATEWAY-SERVICE/gateway-service:9fd20bce350b1ccb477d53ad05e3504f - registration status: 404
2021-09-13 18:10:12.831  INFO [gateway-service,,] 224 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2021-09-13 18:10:13.979  INFO [gateway-service,,] 224 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2021-09-13 18:10:14.056  INFO [gateway-service,,] 224 --- [           main] c.n.gateway.DemoGatewayApplication    : Started DemoGatewayApplication in 27.56 seconds (JVM running for 29.713)
2021-09-13 18:10:15.459  INFO [gateway-service,,] 224 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8080/eureka/}

标签: spring-cloudspring-cloud-netflixspring-cloud-gateway

解决方案


推荐阅读