本文介绍了使用Zuul和Eureka自动配置路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Through reading various books / tutorials, it appears that it is possible to auto-configure routes in Zuul when using it in combination with Eureka service discovery. That means that I don't have to explicitly add routes to Zuul's application.properties.

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

So I understand this correctly? Or do I still need to add routes explicitly to Zuul in order it to work as a gateway?

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

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

(注意:我实际上已经尝试过,但是当我进入 http://localhost:8762/路线,我只会看到一个错误页面.)

(Note: I have actually tried this, but when I go to http://localhost:8762/routes I just get an error page.)

推荐答案

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

Sure. In most microservices implementations, internal microservices endpoints are not exposed outside. A set of public services will be exposed to the clients using an API gateway.

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

The zuul proxy internally uses the Eureka Server for service discovery.

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

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

Sure. I will show you gateway example.

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

创建application.properties文件

create application.properties file

# --- 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代理

1.@EnableZuulproxy to tell Spring Boot that this is a Zuul proxy

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServiceApplication {

2.创建一个application.properties文件

2.create an application.properties file

# =======================================
# 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)

这篇关于使用Zuul和Eureka自动配置路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 11:38