简体   繁体   English

如何在Spring Cloud Zuul中配置路由

[英]How to config routes in Spring Cloud Zuul

In Spring Cloud Zuul server we can define all routes which need be redirected via "application.properties". 在Spring Cloud Zuul服务器中,我们可以通过“application.properties”定义需要重定向的所有路由。

For example: 例如:

zuul.routes.resource.path=/api/**
zuul.routes.resource.url=http://api.com:9025

Once the fat jar is created the "application.properties" is encapsulated into jar, and it's not possible to edit and reload the rules inside the file. 创建胖jar后,“application.properties”被封装到jar中,并且无法编辑和重新加载文件中的规则。

Is there any to inform Zuul about the routes in an external file, and at the same time reload them without stopping the server? 是否有任何通知Zuul外部文件中的路由,同时重新加载它们而不停止服务器?

You can use spring cloud config for that. 您可以使用spring cloud配置。

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。 With the Config Server you have a central place to manage external properties for applications across all environments 使用Config Server,您可以在所有环境中管理应用程序的外部属性

http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_client http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_client

...a bit late to the party, but... ......派对有点晚了,但......

You can do all that you want with the ConfigServer! 您可以使用ConfigServer完成所需的一切!

  1. Create an application.yml for config that is common across ALL applications 为所有应用程序通用的config创建application.yml
  2. Create profile specific application-mycommonprofile.yml. 创建特定于配置文件的应用程序-mycommonprofile.yml。 As 1. but for the 'mycommonprofile' profile. 作为1.但对于'mycommonprofile'配置文件。
  3. Create an appX.yml for each application that is specific to that application. 为每个特定于该应用程序的应用程序创建appX.yml。
  4. Create profile specific appX-myprofile.yml. 创建特定于appX-myprofile.yml的配置文件。 As 3. but for the 'myprofile' profile. 作为3.但对于'myprofile'配置文件。

All of these files are optional and are not dependent on any others. 所有这些文件都是可选的,不依赖于任何其他文件。 You can have an application-mycommonprofile.yml without an application.yml for example. 例如,您可以在没有application.yml的情况下使用application-mycommonprofile.yml。

Hope that helps! 希望有所帮助!

Another late-to-the-party answer, but another way is to use a profile config file, which lives in the filesystem, outside the fatjar. 另一个迟到的答案,但另一种方法是使用配置文件配置文件,它位于fatjar之外的文件系统中。

If the configuration name of your Zuul proxy is 'zuul' and your normal config file is 'zuul.properties' or 'zuul.yaml', then it looks for a profile-specific config file in 'zuul-.properties' or 'zuul-.yaml'. 如果Zuul代理的配置名称是'zuul',而您的普通配置文件是'zuul.properties'或'zuul.yaml',那么它会在'zuul-.properties'或'zuul中查找特定于配置文件的配置文件-.yaml”。 If you do not specify a profile, then the profile named 'default' is active. 如果未指定配置文件,则名为“default”的配置文件处于活动状态。

So you can load properties from an external file name 'zuul-default.properties' or 'zuul-default.yaml' (or 'zuul-default.yml', if you use a 3-letter filename extension). 因此,您可以从外部文件名'zuul-default.properties'或'zuul-default.yaml'(或'zuul-default.yml',如果您使用3个字母的文件扩展名)加载属性。 This will then be loaded when no other profile is specified. 然后在没有指定其他配置文件时加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM