1

我有一个微服务场景,其中 Sidecar(spring boot 应用程序)映射了 python 网络服务器的端点。

python服务器监听5000并执行计算,所以我们决定在URI中用“calc”前缀映射它的主页,所以我们有以下配置:

eureka:
  [omissis] #it works
sidecar:
   port: 5000
   health-uri: http://${sidecar.hostname}:${sidecar.port}/health.json
   home-page-uri: http://${sidecar.hostname}:${sidecar.port}/
   hostname: localhost
server:
  port: 2323
  servlet:
    context-path: /pye

zuul:
  routes:
      calc:
          url: localhost:5000/

然后,我在 Sidecar 微服务内部引入了一个新的 REST 控制器,即:

@RestController
@RequestMapping("git")
public class GitController {

@GetMapping("/")
public List<String> getBranchesAlias(@RequestHeader("Authorization") String oauth2Token,
        @RequestHeader(value = "OIDC_access_token", required = false) String oidAccessToken) {
        //returns a list of branches from a git repository
    return getBranches(oauth2Token, oidAccessToken);
   }
}

最后,我编写了一个 FeignClient,它应该允许我从其他微服务调用微服务的端点,如下所示:

@FeignClient(name = "python-engine")
public interface PythonEngineClient {
@GetMapping(value = "/pye/git/", produces = MediaType.APPLICATION_JSON_VALUE)
List<String> getBranches(@RequestHeader("Authorization") String oauth2Token,
        @RequestHeader(value = "OIDC_access_token", required = false) String oidAccessToken);
}

我调用 FeignClient 得到的是,以“/git”开头的 URI 被重定向到 localhost:/5000 而不是 localhost:/2323,并带有以下日志:

DEBUG s.n.w.p.http.HttpURLConnection - sun.net.www.MessageHeader@647d9996 pairs: {GET /pye/git/branches HTTP/1.1: null}{Accept: application/json}{Authorization: Bearer [omissis] }{User-Agent: Java/1.8.0_232}{Host: localhost:5000}{Connection: keep-alive}
DEBUG s.n.w.p.http.HttpURLConnection - sun.net.www.MessageHeader@88037215 pairs: {null: HTTP/1.1 404 NOT FOUND}{Content-Length: 232}{Content-Type: text/html}{Date: Fri, 14 Feb 2020 08:53:49 GMT}{Server: waitress}
ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 
feign.FeignException: status 404 reading PythonEngineClient#getBranches(String,String)] with root cause
feign.FeignException: status 404 reading PythonEngineClient#getBranches(String,String)
at feign.FeignException.errorStatus(FeignException.java:78)

正如我们在第一条日志行中看到的,主机是“localhost:5000”。我想要做的是对“/git/**”的调用不会重定向到 python,而是由 RestController 提供服务。

我怎样才能得到它?谢谢!

4

1 回答 1

0

我的最终解决方案是这是不可能的:如果插入 RestController,Sidecar 也无法正常工作。

于 2020-04-08T10:08:34.390 回答