使用Zuul和Eureka自动配置路由

时间:2018-09-30 17:54:28

标签: spring-boot netflix-zuul netflix-eureka

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

所以我理解正确吗?还是我仍然需要显式添加路由到Zuul才能使其充当网关?

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

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

2 个答案:

答案 0 :(得分:5)

好的。在大多数微服务实现中,内部微服务端点不会暴露在外部。一组公共服务将使用API​​网关向客户端公开。

enter image description here

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

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

好的。我将向您展示网关示例。

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

答案 1 :(得分:1)

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

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