本应用使用 Eureka Service Registry、Spring Cloud Gateway、Open Feign 开发,用于内部通信。目前,我面临着使用 Feign 客户端在微服务之间进行内部通信的问题。
这是 Eureka 仪表板,用于将所有服务正确注册到注册表中。
以下是我用来将这些属性注册为 Eureka 服务注册表的客户端的示例属性。注册表在端口 8081 上运行。
eureka.client.service-url.defaultZone=http://localhost:8081/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.hostname=localhost
这里是 Feign 客户端接口,用于在银行核心用户服务和银行核心服务之间建立通信。
@FeignClient(name = "banking-core-service", configuration = CustomFeignClientConfiguration.class)
public interface BankingCoreFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/api/v1/user/{identification}")
UserResponse readUser(@PathVariable("identification") String identification);
}
这是feign客户端的配置类。
package com.javatodev.finance.config;
import com.javatodev.finance.rest.client.CustomFeignErrorDecoder;
import feign.Logger;
import feign.codec.ErrorDecoder;
import feign.okhttp.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomFeignClientConfiguration {
@Bean
public OkHttpClient client() {
return new OkHttpClient();
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
以下是我通过 postman 的 API 调用执行通信层时得到的错误响应。
异常发生在 API feign.RetryableException:banking-core-service:nodename 或 servname 提供,或未知执行 GET http://banking-core-service/api/v1/user/808829932V
似乎 feign 没有使用服务发现解析正确的 API。当我将银行核心服务 IP 地址和端口硬编码为 @FeignClient 的 URL 属性时,这是有效的。但是我需要使用在服务注册表中注册的名称来解析 URL。