【问题标题】:Combining SpringBoot, Zuul and Eureka but bind Services to Localhost结合 SpringBoot、Zuul 和 Eureka 但将服务绑定到 Localhost
【发布时间】:2018-03-19 18:58:54
【问题描述】:

我已经为路由设置了 Zuul,为服务发现设置了 Eureka,效果很好。在设置 Eureka 之前,我使用 server.address=127.0.0.1 将我的实际服务绑定到 localhost 以便它们只能从 Api 网关内访问。

结合 Zuul 和 Eureka 时,server.address=127.0.0.1 不再起作用。我无法从我的网络内部或外部访问我的实际 REST 端点。

我的 Eureka 服务发现的 application.properties:

spring.application.name=service-discovery
server.port=8761
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

我的 Zuul API 网关的 application.properties:

spring.application.name=api-gateway
zuul.prefix=/api
server.port=8080

ribbon.eureka.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

zuul.routes.library.path=/library/**
zuul.routes.library.serviceId=library

我的实际 REST 服务的 application.properties:

spring.application.name=library
server.servlet.context-path=/library
server.port=8090
server.address=127.0.0.1

ribbon.eureka.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

当我从 REST 服务的属性文件中删除 server.address=127.0.0.1 时,我当然可以访问该资源 - 但也可以在没有 localhost 的情况下访问,这不是我想要的。

所以我尝试实现的是我的小微服务只能从localhost 内部访问(在请求通过Zuul API 网关之后)。此外,我想使用 Eureka 进行服务发现并有机会提供第二个服务实例。

【问题讨论】:

    标签: microservices netflix-zuul netflix-eureka netflix


    【解决方案1】:

    使用 eureka.instance.hostname=localhosteureka.instance.ip-address=127.0.0.1 将实际微服务注册到 Eureka 服务器,并结合微服务与 localhost (server.address=127.0.0.1) 的绑定来完成这项工作。

    这些是 application.properties 文件:

    我的 Eureka 服务发现的 application.properties:

    spring.application.name=service-discovery
    server.port=8761
    server.address=127.0.0.1
    eureka.client.registerWithEureka=false
    eureka.client.fetchRegistry=false
    

    我的 Zuul API 网关的 application.properties:

    spring.application.name=api-gateway
    zuul.prefix=/api
    server.port=8080
    ribbon.eureka.enabled=true
    eureka.client.registerWithEureka=false
    zuul.routes.library.path=/library/**
    zuul.routes.library.serviceId=library
    zuul.routes.library.stripPrefix=false
    

    我的实际 REST 服务的 application.properties:

    spring.application.name=library
    server.servlet.context-path=/library
    server.port=8090
    server.address=127.0.0.1
    ribbon.eureka.enabled=true
    eureka.client.registerWithEureka=true
    eureka.instance.hostname=localhost
    eureka.instance.ip-address=127.0.0.1
    

    “库”微服务现在只能从 localhost 获得,但仍然在 Eureka 和 Zuul API 网关后面注册。

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 2017-09-03
      • 1970-01-01
      • 2018-01-03
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多