我正在尝试将JHipster从使用Zuul迁移到Spring Cloud Gateway。在当前的Zuul实现中,有一个gatewayresource
,用于获取路由及其服务实例的列表。
package com.mycompany.myapp.web.rest;
import com.mycompany.myapp.web.rest.vm.RouteVM;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.http.*;
import org.springframework.security.access.annotation.Secured;
import com.mycompany.myapp.security.AuthoritiesConstants;
import org.springframework.web.bind.annotation.*;
/**
* REST controller for managing Gateway configuration.
*/
@RestController
@RequestMapping("/api/gateway")
public class GatewayResource {
private final RouteLocator routeLocator;
private final DiscoveryClient discoveryClient;
public GatewayResource(RouteLocator routeLocator, DiscoveryClient discoveryClient) {
this.routeLocator = routeLocator;
this.discoveryClient = discoveryClient;
}
/**
* {@code GET /routes} : get the active routes.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the list of routes.
*/
@GetMapping("/routes")
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<List<RouteVM>> activeRoutes() {
List<Route> routes = routeLocator.getRoutes();
List<RouteVM> routeVMs = new ArrayList<>();
routes.forEach(route -> {
RouteVM routeVM = new RouteVM();
routeVM.setPath(route.getFullPath());
routeVM.setServiceId(route.getId());
routeVM.setServiceInstances(discoveryClient.getInstances(route.getLocation()));
routeVMs.add(routeVM);
});
return ResponseEntity.ok(routeVMs);
}
}
/API/gateway/routes
endpoint返回如下所示的数据:
[
{
"path": "/services/blog/**",
"serviceId": "blog",
"serviceInstances": [
{
"serviceId": "BLOG",
"secure": false,
"instanceId": "blog:17c5482e0ccf49f19efb6dba8c5e5aa1",
"instanceInfo": {
"instanceId": "blog:17c5482e0ccf49f19efb6dba8c5e5aa1",
"app": "BLOG",
"appGroupName": null,
"ipAddr": "192.168.0.20",
"sid": "na",
"homePageUrl": "http://192.168.0.20:8081/",
"statusPageUrl": "http://192.168.0.20:8081/management/info",
"healthCheckUrl": "http://192.168.0.20:8081/management/health",
"secureHealthCheckUrl": null,
"vipAddress": "blog",
"secureVipAddress": "blog",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "192.168.0.20",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 5,
"durationInSecs": 10,
"registrationTimestamp": 1581934876730,
"lastRenewalTimestamp": 1581935287273,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1581934876214
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"zone": "primary",
"profile": "dev",
"management.port": "8081",
"version": "0.0.1-SNAPSHOT"
},
"lastUpdatedTimestamp": 1581934876730,
"lastDirtyTimestamp": 1581934876053,
"actionType": "ADDED",
"asgName": null
},
"port": 8081,
"host": "192.168.0.20",
"metadata": {
"zone": "primary",
"profile": "dev",
"management.port": "8081",
"version": "0.0.1-SNAPSHOT"
},
"uri": "http://192.168.0.20:8081",
"scheme": null
}
]
}
]
spring:
application:
name: jhipster
cloud:
gateway:
default-filters:
- TokenRelay
discovery:
locator:
enabled: true
lower-case-service-id: true
predicates:
- name: Path
args:
pattern: "'/services/'+serviceId.toLowerCase()+'/**'"
filters:
- name: RewritePath
args:
regexp: "'/services/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
replacement: "'/${remaining}'"
route-id-prefix: ""
httpclient:
pool:
max-connections: 1000
package com.mycompany.myapp.web.rest;
import com.mycompany.myapp.security.AuthoritiesConstants;
import com.mycompany.myapp.web.rest.vm.RouteVM;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.route.*;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
/**
* REST controller for managing Gateway configuration.
*/
@RestController
@RequestMapping("/api/gateway")
public class GatewayResource {
private final RouteLocator routeLocator;
private final DiscoveryClient discoveryClient;
public GatewayResource(RouteLocator routeLocator, DiscoveryClient discoveryClient) {
this.routeLocator = routeLocator;
this.discoveryClient = discoveryClient;
}
/**
* {@code GET /routes} : get the active routes.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the list of routes.
*/
@GetMapping("/routes")
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<List<RouteVM>> activeRoutes() {
Flux<Route> routes = routeLocator.getRoutes();
List<RouteVM> routeVMs = new ArrayList<>();
routes.subscribe(route -> {
System.out.println("route: " + route.toString());
RouteVM routeVM = new RouteVM();
routeVM.setPath(route.getPredicate().toString());
String serviceId = route.getId().substring(route.getId().indexOf("_") + 1).toLowerCase();
routeVM.setServiceId(serviceId);
routeVM.setServiceInstances(discoveryClient.getInstances(serviceId));
routeVMs.add(routeVM);
});
return ResponseEntity.ok(routeVMs);
}
}
[
{
"path": "Paths: [/services/blog/**], match trailing slash: true",
"serviceId": "blog",
"serviceInstances": [
{
"uri": "http://192.168.0.20:8081",
"serviceId": "BLOG",
"port": 8081,
"host": "192.168.0.20",
"instanceId": "blog:17c5482e0ccf49f19efb6dba8c5e5aa1",
"secure": false,
"instanceInfo": {
"instanceId": "blog:17c5482e0ccf49f19efb6dba8c5e5aa1",
"app": "BLOG",
"appGroupName": null,
"ipAddr": "192.168.0.20",
"sid": "na",
"homePageUrl": "http://192.168.0.20:8081/",
"statusPageUrl": "http://192.168.0.20:8081/management/info",
"healthCheckUrl": "http://192.168.0.20:8081/management/health",
"secureHealthCheckUrl": null,
"vipAddress": "blog",
"secureVipAddress": "blog",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "192.168.0.20",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 5,
"durationInSecs": 10,
"registrationTimestamp": 1581934876730,
"lastRenewalTimestamp": 1581965726885,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1581934876214
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"zone": "primary",
"profile": "dev",
"management.port": "8081",
"version": "0.0.1-SNAPSHOT"
},
"lastUpdatedTimestamp": 1581934876730,
"lastDirtyTimestamp": 1581934876053,
"actionType": "ADDED",
"asgName": null
},
"metadata": {
"zone": "primary",
"profile": "dev",
"management.port": "8081",
"version": "0.0.1-SNAPSHOT"
},
"scheme": null
}
]
},
{
"path": "Paths: [/services/jhipster/**], match trailing slash: true",
"serviceId": "jhipster",
"serviceInstances": [{...}]
}
]
route: Route{id='ReactiveCompositeDiscoveryClient_BLOG', uri=lb://BLOG, order=0, predicate=Paths: [/services/blog/**], match trailing slash: true, gatewayFilters=[[org.springframework.cloud.security.oauth2.gateway.TokenRelayGatewayFilterFactory$$Lambda$1404/0x0000000800a2dc40@2b9e183d, order = 1], [[RewritePath /services/blog/(?<remaining>.*) = '/${remaining}'], order = 1]], metadata={}}
route: Route{id='ReactiveCompositeDiscoveryClient_JHIPSTER', uri=lb://JHIPSTER, order=0, predicate=Paths: [/services/jhipster/**], match trailing slash: true, gatewayFilters=[[org.springframework.cloud.security.oauth2.gateway.TokenRelayGatewayFilterFactory$$Lambda$1404/0x0000000800a2dc40@59032a74, order = 1], [[RewritePath /services/jhipster/(?<remaining>.*) = '/${remaining}'], order = 1]], metadata={}}
几个问题:
Route.GetPredicate().ToString()
给出了“路径:[/services/blog/**],匹配尾随斜杠:true”
而我只需要/services/blog/**
.spring.cloud.gateway.discovery.location.route-id-prefix:“”
不删除默认前缀?我必须使用route.getid().substring(route.getid().indexof(“_”)+1).toLowerCase()
routelocator
返回/services/jhipster
路由?这是一条通往网关的路由。使用Zuul,只返回了一条路由。我相信你要找的东西已经在里面了。https://cloud.spring.io/spring-cloud-gateway/reference/html/#Retrieving-the-routes-defined-in-the-gateway
我正在开发一个具有这种结构的rest Web服务 http://localhost:8080/context/login/{用户}/{密码} 此请求的示例为 http://localhost:8080/context/login/admin/admin 我在applicationContext中配置了一个AbstractPhaseInterceptor。我的spring应用程序的xml。拦截器类是
我的目标是开发一个单一的骆驼路线来映射这些服务器,接受路径中服务器的名称。类似于这样: 我的(简化且不起作用)Blueprint.xml: 问题是,我不知道如何从路径中移除/center、/north或/south,因此头部被传递给目标服务,而目标服务不知道如何处理它。调用:
在以下React应用程序中,有两个路由URLhttp://myapp 正确布线到布局构件。但是,URLhttp://myapp/login 也路由到布局组件,而不是登录。如果我将path=“/login”更改为“/sign”,它将正确路由到登录组件。 React路由器中的“/login”路径将其路由到路由是否有特殊之处?或者我设置这个路由的方式有错误吗?
我有以下入口设置: 当我点击时,我被重定向到,并带有NGINX 404未找到。 根据日志,可以看到< code>grafana窗格被查询命中: logger = context traceID = 0000000000000000000000000000 userId = 0 orgId = 0 uname = t = 2022-10-13t 16:19:57.989170173 z level
问题内容: 我正在使用React Router。我想检测我来自何处的上一页(在同一应用程序内)。我在上下文中拥有路由器。但是,我在路由器对象上看不到“上一条路径”或历史记录之类的任何属性。我该怎么做? 问题答案: 您可以在生命周期方法中保存先前的路径。该逻辑非常接近文档的疑难解答部分中提供的示例。 最近,可以从州访问它。