通过阅读各种书籍/教程,似乎可以在与 Eureka 服务发现结合使用时在 Zuul 中自动配置路由。这意味着我不必显式地将路由添加到 Zuul 的 application.properties。
所以我理解正确?还是我仍然需要明确地向 Zuul 添加路由,以便它作为网关工作?
我希望它根据在 Eureka 注册的应用程序名称自动创建路由。这可能吗?
(注意:我实际上已经尝试过了,但是当我转到 http://localhost:8762/routes 时,我只是看到一个错误页面。)
最佳答案
当然。在大多数微服务实现中,内部微服务端点不会暴露在外部。一组公共(public)服务将使用 API 网关公开给客户端。
zuul 代理在内部使用 Eureka Server 进行服务发现。
- 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 )
关于spring-boot - 使用 Zuul 和 Eureka 自动配置路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580597/