1:创建Feign接口定义
@FeignClient(name = "user-service",configuration = UserServiceFeignClientConfiguration.class)
public interface UserRegistrationService {
@PostMapping(value = "/user/register", produces = "application/json;v=3") // V3
Boolean registerUser(@RequestBody @Validated @Valid User user);
}
2:创建项目bootstrap启动类
@EnableAutoConfiguration
@EnableFeignClients(clients = UserRegistrationService.class )
@LoadBalancerClient(name = "user-service",configuration = UserServiceLoadBalanceConfiguration.class)
public class FeignClientSpringBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext context
= new SpringApplicationBuilder(FeignClientSpringBootstrap.class).web(WebApplicationType.NONE)
.build().
run(args);
UserRegistrationService userRegistrationService = context.getBean(UserRegistrationService.class);
User user = new User();
user.setId(2L);
user.setName("test");
Boolean res = userRegistrationService.registerUser(user);
System.out.println("res==="+res);
context.close();
}
}
3:创建loadbalance配置类UserServiceLoadBalanceConfiguration
@Configuration
public class UserServiceLoadBalanceConfiguration {
@Bean
public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),name);
}
@Bean
public ServiceInstanceListSupplier serviceInstanceListSupplier(){
return new UserServiceServiceInstanceListSupplier();
}
}
4:创建UserServiceServiceInstanceListSupplier类返回loadbalance调用那个服务实列
public class UserServiceServiceInstanceListSupplier implements ServiceInstanceListSupplier {
@Override
public String getServiceId() {
return "user-service";
}
@Override
public Flux<List<ServiceInstance>> get() {
return Flux.just(Arrays.asList(getLocalServiceInstance()));
}
private ServiceInstance getLocalServiceInstance(){
DefaultServiceInstance instance = new DefaultServiceInstance();
instance.setInstanceId("user-service");
instance.setHost("127.0.0.1");
instance.setPort(8091);
return instance;
}
}
5:创建Feign处理Response类ApiResponseDecoder
public class ApiResponseDecoder implements Decoder {
private final Decoder decoder;
public ApiResponseDecoder(Decoder decoder) {
this.decoder = decoder;
}
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
String contentType = getContentType(response);
MediaType mediaType = MediaType.parseMediaType(contentType);
String version = mediaType.getParameter("v");
if(version == null){
Object object = decoder.decode(response, ApiResponse.class);
if(object instanceof ApiResponse){
return ApiResponse.class.cast(object).getBody();
}
}
return decoder.decode(response,type);
}
private String getContentType(Response response){
Collection<String> types=response.headers().getOrDefault("Content-Type", Arrays.asList(""));
return types.iterator().next();
}
}
6:配置ApiResponseDecoder
@Configuration
public class UserServiceFeignClientConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Decoder feignDecoder(){
return new ApiResponseDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters)));
}
}
7:运行结果
8:代码执行逻辑
EnableFeignClients:启动Feign配置,生成代理对象UserRegistrationService
LoadBalancerClient(name = "user-service",configuration = UserServiceLoadBalanceConfiguration.class):
配置Feign远程调用时候loadbalance配置,如果没有配置,走默认配置
@FeignClient(name = "user-service",configuration = UserServiceFeignClientConfiguration.class):
配置Feign返回处理Response信息