使用 Zuul 和 Eureka 自动配置路由

Auto-configure routes with Zuul and Eureka

通过阅读各种书籍/教程,似乎可以在与 Eureka 服务发现结合使用时在 Zuul 中自动配置路由。这意味着我不必显式地将路由添加到 Zuul 的 application.properties.

所以我理解正确?还是我仍然需要显式添加路由到 Zuul 以使其用作网关?

我希望它根据在 Eureka 注册的应用程序名称自动创建路由。这可能吗?

(注意:我实际上已经尝试过这个,但是当我转到http://localhost:8762/routes时,我只是得到一个错误页面。)

是的。您可以将 Zuul 与 Eureka 集成,并根据在 Eureka 中注册的应用程序名称配置路由。只需将以下配置添加到 Zuul 应用程序:

zuul:
  ignoredServices: "*"
  routes:
    a-service: /a-service/**
    b-service: /b-service/**
    c-service: /c-service/**
    d-service: /d-service/**

当然可以。在大多数微服务实现中,内部微服务端点不会暴露在外部。将使用 API 网关向客户端公开一组 public 服务。

zuul 代理内部使用 Eureka Server 进行服务发现。

  1. I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?

当然可以。我将向您展示网关示例。

1.创建您的服务项目(用户服务)

创建application.properties 文件

# --- Spring Config
spring:
  application:
    name: OVND-USER-SERVICE

# Eureka client
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}

2。设置 Zuul 项目(网关服务)

1.@EnableZuulproxy 告诉Spring启动这是一个Zuul代理

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {

2.create 一个 application.properties 文件

# =======================================
# Gateway-service Server Configuration
# =======================================

# --- Spring Config
spring:
  application:
    name: gateway-service

server:
  port: ${PORT:8080}

# Eureka client
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}

zuul:
  host:
  routes:
    ## By default, all requests to user service for example will start with: "/user/"
    ## What will be sent to the user service is what comes after the path defined,
    ## So, if request is "/user/v1/user/tedkim", user service will get "/v1/user/tedkim".
    user-service:
      path: /user/**
      service-id: OVND-USER-SERVICE
    another-service:
      path: /another/**
      service-id: OVND-ANOTHER-SERVICE

尤里卡网站(localhost:8761)