spring-boot - 使用 Zuul 和 Eureka 自动配置路由

标签 spring-boot netflix-zuul netflix-eureka

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

所以我理解正确?还是我仍然需要明确地向 Zuul 添加路由,以便它作为网关工作?

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

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

最佳答案

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

enter image description here

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 Boot这是一个Zuul代理

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {

2.创建一个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

Eureka 网站 ( localhost:8761 )

enter image description here

关于spring-boot - 使用 Zuul 和 Eureka 自动配置路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580597/

相关文章:

java - 如何在 Mockito 单元测试中摆脱 NullPointerException

java - 如何使用 ZUUL 处理多个服务的 POST 请求

spring-boot - 不重启网关动态删除zuul路由

spring-boot - 在配置中定义类型为 'com.netflix.discovery.AbstractDiscoveryClientOptionalArgs' 的 bean

java - Spring Cloud Gateway 不适用于 @Bean DiscoveryClientRouteDefinitionLocator

java - 无代理的 RestTemplate 调用

java - 设置实体数据添加到现有数据,而不是替换补丁请求上的所有现有值

microservices - 为什么微服务推荐使用 API 网关?

java - Eureka检测服务状态

java - 如何使用 RequestBodyAdvice