apache / shenyu

Apache ShenYu is a Java native API Gateway for service proxy, protocol conversion and API governance.
https://shenyu.apache.org/
Apache License 2.0
8.43k stars 2.93k forks source link

[Question] I want the host registered with the gateway to be the hostname that I specify. How can I achieve this? #5601

Open topcatv opened 2 months ago

topcatv commented 2 months ago

Question

When using shenyu-spring-boot-starter-client-springmvc, I want the host registered with the gateway to be the hostname that I specify. How can I achieve this? Because I used the service in k8s to provide services, therefore I used the following configuration in the configuration file.

shenyu:
  register:
    registerType: http
    serverLists: http://shenyu-admin-svc:9095
    props:
      username: admin
      password: xxxxx
  client:
    http:
      props:
        host: biz-service
        contextPath: /biz
        appName: biz

However, after registering to the gateway, it was found that the upstream of the divide was still the IP address of this service instead of the service name. Through source code analysis, it was found in the org.apache.shenyu.client.springmvc.init.SpringMvcClientEventListener#buildURIRegisterDTO method

try {
    return URIRegisterDTO.builder()
            .contextPath(getContextPath())
            .appName(getAppName())
            .protocol(protocol)
            .host(super.getHost())
            .port(Integer.valueOf(getPort()))
            .rpcType(RpcTypeEnum.HTTP.getName())
            .eventType(EventType.REGISTER)
            .build();
} catch (ShenyuException e) {
    throw new ShenyuException(e.getMessage() + "please config ${shenyu.client.http.props.port} in xml/yml !");
}
public String getHost() {
    return IpUtils.isCompleteHost(this.host) ? this.host
            : IpUtils.getHost(this.host);
}

IpUtils.isCompleteHost(this.host) returns false, so the host IP will be fetched. Can this be changed here to support configuring the host name or svc in k8s?

Aias00 commented 2 months ago

hi, the IpUtils.isCompleteHost method is for checking whether the host is complete, and it actually just support ip now

topcatv commented 2 months ago

Whether to consider supporting hostname or service name of k8s service

x519286925 commented 2 months ago

Whether to consider supporting hostname or service name of k8s service

我也发现了这个问题,然后我用多语言的http客户端用调接口的方式倒可以用hostname,不知道是设计问题还是什么,也能实现正常转发 https://shenyu.apache.org/zh/docs/developer/developer-shenyu-client

I also discovered this issue, and then I used a multilingual HTTP client to call the interface using hostname. I don't know if it's a design problem or something, but I can still achieve normal forwarding

😄

Aias00 commented 2 months ago

hi, we will discuss thia problem later, and may solve this problem in next version