将@EnableFeignClients 放在spring boot 主应用java 类中。
在 Feign 客户端接口之后创建来调用 Web 服务:
@FeignClient(value = "service-auth",
configuration = ClientConfig.class,
fallbackFactory = ClientFallbackFactory.class,
url = "http://localhost:10000/oauth/")
public interface GenericAbstractClient {
@RequestMapping(value="/token",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
ResponseVo auth(@RequestBody RequestVo body);
}
和 ClientFallbackFactory 为:
@Component
class ClientFallbackFactory implements FallbackFactory<GenericAbstractClient> {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientFallbackFactory.class);
@Override
public GenericAbstractClient create(Throwable cause) {
return new GenericAbstractClient () {
@Override
public ResponseVo oauth(RequestVo body) {
LOGGER.warn("Hystrix exception", cause.getMessage());
return null;
}
};
}
}
您的 RequestBody 可能是一个 java 类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RequestVo {
private String grant_type;
private String username;
private String password;
}
还有 ResponseVo 类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseVo {
private String access_token;
private String token_type;
private String exprires_in;
private String scope;
}
你可以添加这个配置:
@Configuration
public class ClientConfig {
private int connectTimeOutMillis = 120000;
private int readTimeOutMillis = 120000;
@Bean
public Request.Options options() {
return new Request.Options(connectTimeOutMillis, readTimeOutMillis);
}
}