Gateway Router 使用 Docker 网络 URISyntaxException 异常原因

137 阅读1分钟

gateway 路由分发:

routes:
  - id: xxx
    uri: http://xxx_server:9101
    predicates:
      - Path=/xxx/**
    filters:
      - RewritePath=/xxx/(?<path>.*), /${path}

Gateway 在走router校验时会出现如下异常


org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is java.lang.IllegalStateException: Could not create URI object: Expected scheme-specific part at index 5: http:
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) ~[spring-context-5.3.29.jar:5.3.29]
        

Caused by: java.lang.IllegalStateException: Could not create URI object: Expected scheme-specific part at index 5: http:
	at org.springframework.web.util.HierarchicalUriComponents.toUri(HierarchicalUriComponents.java:527) ~[spring-web-5.3.29.jar:5.3.29]
        
        

Caused by: java.net.URISyntaxException: Expected scheme-specific part at index 5: http:
	at java.net.URI$Parser.fail(URI.java:2847) ~[na:1.8.0_422]
	at java.net.URI$Parser.failExpecting(URI.java:2853) ~[na:1.8.0_422]
	at java.net.URI$Parser.parse(URI.java:3056) ~[na:1.8.0_422]
	at java.net.URI.<init>(URI.java:673) ~[na:1.8.0_422]
	at org.springframework.web.util.HierarchicalUriComponents.toUri(HierarchicalUriComponents.java:523) ~[spring-web-5.3.29.jar:5.3.29]
	... 70 common frames omitted

主要代码部分在 UriComponentsBuilder.fromUri(this.uri)

public B uri(URI uri) {
    this.uri = uri;
    String scheme = this.uri.getScheme();
    Assert.hasText(scheme, "The parameter [" + this.uri + "] format is incorrect, scheme can not be empty");
    if (this.uri.getPort() < 0 && scheme.startsWith("http")) {
       // default known http ports
       int port = this.uri.getScheme().equals("https") ? 443 : 80;
       this.uri = UriComponentsBuilder.fromUri(this.uri).port(port).build(false).toUri();
    }
    return getThis();
}

解决:Java 的 URI 类不支持url中使用“_”,但是支持使用 “-”