@Feign通过URI指定请求地址

@FeignClient使用起来非常方便,只需要启用配置即可使用,但是有时候需要请求不通地址的服务,这时可以指定一个URI参数。

配置:

@EnableFeignClients(basePackages = "com.test.openapi.rpc")
@Configuration
public class FeignConfig extends Compact implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

@FeignClient接口

@FeignClient(
        name = "testCloudService",
        url = "https://api.test.com"
)
@RequestMapping(
        value = "/api",
        headers = {
                "clientId=MY CLIENTID",
                "version=1.0"
        }
)
public interface ITestCloudService {

    @RequestMapping(value = "/call", method = RequestMethod.GET)
    ResultResp<Map<String, Object>> region(URI uri, @RequestHeader("token") String token);

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    ResultResp<Map<String, Object>> login(URI uri, @RequestBody Map body, @RequestHeader("token") String token);
}

如果是GET方法,需要指定一连串参数,可以使用注解@SpringQueryMap如:

@RequestMapping(value = "/user", method = RequestMethod.GET)
ResultResp<ApiUserDevice> userDevice(URI uri, @SpringQueryMap Map queryMap, @RequestHeader("token") String token);

 

 

feign调服务错误:feign.RetryableException: Connection refused

2021/01/07 13:37:42.214 ERROR [http-nio-0.0.0.0-7200-exec-35] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is feign.RetryableException: Connection refused (Connection refused) executing POST http://NJ-DEVICECLOUD-SERVER/njdcd/dispatch] with root cause
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
at sun.net.NetworkClient.doConnect(NetworkClient.java:175) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_171]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_171]

配置的问题,改成IP就可以了

eureka:
  instance:
    appname: ${spring.application.name}
    virtual-host-name: ${spring.application.name}
    secure-virtual-host-name: ${spring.application.name}
    instance-id: ${server.address2}:${spring.application.name}-peer:${server.port}
    hostname: konke.app.eureka
    #    non-secure-port: 6002 #非安全通信端口
    #    non-secure-port-enabled: true #是否启用非安全端口接受请求
    #    secure-port: 6445 #安全通信端口
    #    secure-port-enabled: true #是否启用安全端口接受请求
    prefer-ip-address: true #是否优先使用IP地址作为主机名的标识,默认false
    lease-renewal-interval-in-seconds: 30 #eureka节点定时续约时间,默认30
    lease-expiration-duration-in-seconds: 90 #eureka节点剔除时间,默认90
  #    metadata-map:
  #      zone: shanghai

prefer-ip-address: true #是否优先使用IP地址作为主机名的标识,默认false

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |