You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/03/04 07:42:10 UTC

[GitHub] [servicecomb-java-chassis] zhangbeiyuan-hw opened a new issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务

zhangbeiyuan-hw opened a new issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务
URL: https://github.com/apache/servicecomb-java-chassis/issues/1620
 
 
   后台我有好几个服务,不想每个都配置map映射,想通过default配置实现自动转发,如下配置:
   如果我有两个微服务,microserviceName是serverA,提供的url是 /v1/users
   另一个微服务microserviceName是serverB,提供url是 server-b/v1/hosts
   请教一下,defalut如何配置带服务名自动转发? js中如何正确构造 url 
   ```yaml
   servicecomb:
     http:
       dispatcher:
         edge:
           default:
             enabled: true
             prefix: rest
             withVersion: true
             prefixSegmentCount: 1
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务

Posted by GitBox <gi...@apache.org>.
yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务
URL: https://github.com/apache/servicecomb-java-chassis/issues/1620#issuecomment-594985791
 
 
   [这里](https://github.com/servicestage-demo/microservice-dev-training21days/tree/day6_homework)有一个demo可以作为参考。`DefaultEdgeDispatcher`需要匹配一个url前缀来触发其工作,并且在转发请求的时候需要从url中截取出一部分作为请求的名字。
   这个demo的EdgeService服务配置跟你给的一样:
   ```yaml
   servicecomb:
     http:
       dispatcher:
         edge:
           default:                 # 使用DefaultEdgeDispatcher开发网关服务
             enabled: true          # 开启DefaultEdgeDispatcher
             prefix: rest           # 匹配请求路径前缀为/rest
             # withVersion默认值就是true,这里只是展示一下,实际上可以省略该配置
             withVersion: true      # 请求带版本号,例如v1表示[1.0.0,2.0.0)范围内的微服务版本
             # prefixSegmentCount默认值就是1,这里只是展示一下,实际上可以省略该配置
             prefixSegmentCount: 1  # 前缀长度,例如/rest/provider/v0/hello/Bob,去掉第一段,将/provider/v0/hello/Bob转发到后端
   ```
   名叫`provider`的后端服务中的接口在[这里](https://github.com/servicestage-demo/microservice-dev-training21days/blob/day6_homework/helloworld-provider/src/main/java/microservice/demo/training21days/provider/service/HelloService.java)
   
   以这种配置项为例,`prefix`是`rest`表示发给EdgeService网关的请求url需要以`rest`开头才能被`DefaultEdgeDispatcher`转发。`withVersion=true`表示url中会带版本范围。`prefixSegmentCount=1`表示`DefaultEdgeDispatcher`处理的请求会被截掉url的第一段后再转给后端服务。
   
   向EdgeService发请求 `/rest/provider/v0/hello/Bob` 时:
   - 第一段`rest`是匹配dispatcher的前缀;
   - 第二段`provider`会作为后端服务的名字,名字不对就会找不到后端服务;
   - 第三段是版本范围,`v0`表示[0.0.0, 1,0.0)的大版本范围
   
   因为`prefixSegmentCount=1`,所以第一段`/rest`会被截掉,实际发给后端的·provider`服务的是 `/provider/v0/hello/Bob` 
   
   ----------------------
   
   按照上述规则,你的serverA,提供的url是 `/v1/users`,serverB提供的请求url是 `server-b/v1/hosts`,这种url格式不符合`DefaultEdgeDispatcher`对于后端服务url `/<服务名>/<版本范围>/其他部分的url` 这样的格式要求。所以在调用的时候需要把`/<服务名>/<版本范围>`这一段额外加上去,然后把`DefaultEdgeDispatcher`的`prefixSegmentCount`设置为`3`以截掉请求url开头的`/rest/<服务名>/<版本范围>`。
   例如,`/rest/serverA/v1/v1/users`在转发时会把`/v1/users`发送给版本范围在1.0.0到2.0.0(不包含)之间的serverA服务。`/rest/serverB/v1/server-b/v1/hosts`在截取完成后就是把`/server-b/v1/hosts`发送给名为serverB服务的1.0.0到2.0.0版本。

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务

Posted by GitBox <gi...@apache.org>.
yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务
URL: https://github.com/apache/servicecomb-java-chassis/issues/1620#issuecomment-594998533
 
 
   不客气 : )

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务

Posted by GitBox <gi...@apache.org>.
yhs0092 commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务
URL: https://github.com/apache/servicecomb-java-chassis/issues/1620#issuecomment-594986867
 
 
   嫌这样外部url里有两个`v1`太丑了的话,也可以考虑设置`withVersion=false`。这样`DefaultEdgeDispatcher`不会再帮你限定转发到后端服务的版本范围了,如果有必要的话需要你自己通过额外方式来进行限定。
   总而言之`DefaultEdgeDispatcher`比较适用于后端服务的url经过统一规划,符合预设格式的场景。如果碰到url不规则的场景还要用`DefaultEdgeDispatcher`的话,只能自己额外加一段服务名(和版本范围),再在转发的时候截取掉了。

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [servicecomb-java-chassis] zhangbeiyuan-hw commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务

Posted by GitBox <gi...@apache.org>.
zhangbeiyuan-hw commented on issue #1620: edge配置是如何设置默认配置的?可以根据服务名自动转发服务
URL: https://github.com/apache/servicecomb-java-chassis/issues/1620#issuecomment-594987243
 
 
   我试试,非常感谢

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services