springCloud Feign 异步调用远程服务
1. Feign API
package com.sande.configserver.api;
import java.util.concurrent.CompletableFuture;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.netflix.hystrix.HystrixCommand;
import feign.hystrix.HystrixFeign;
import io.netty.util.concurrent.Future;
@FeignClient(value = "user-server", fallback = UserApiFeignHystrix.class)
public interface UserApi {
@PostMapping(value="/user/getByTokenUser")
//CompletableFuture<User> getByTokenUser(@RequestParam("token") String token);
HystrixCommand<User> getByTokenUser(@RequestParam("token") String token);
}
2. 实现服务降级的类
package com.sande.configserver.api;
import static org.hamcrest.CoreMatchers.nullValue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.springframework.stereotype.Component;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.contrib.javanica.utils.FutureDecorator;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.bytebuddy.asm.Advice.Return;
@Component
public class UserApiFeignHystrix implements UserApi {
private static final Setter hystrixCommandGroupKey =
Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserApi"));
@Override
public HystrixCommand<User> getByTokenUser(final String token) {
return new HystrixCommand<User>(hystrixCommandGroupKey) {
@Override
protected User run() throws Exception {
// TODO Auto-generated method stub
System.out.println("Feign 服务异步调用服务降级");
User user = new User();
user.setPhone("13811111111");
user.setUserName("lixia");
return user;
}
};
}
}
3. 服务调用方(消费端)的 Controller
package com.sande.configserver.api;
import static org.assertj.core.api.Assertions.setLenientDateParsing;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import feign.hystrix.HystrixFeign;
@RestController
public class UserController {
@Resource UserApi userApi;
@RequestMapping(value="/getByTokenUser")
public User getByTokenUser(HttpServletRequest request,HttpServletResponse response) throws InterruptedException, ExecutionException {
String token ="token_cef257c4db1d456fbea00f1e8a50b3b0";
Future<User> future = userApi.getByTokenUser(token).queue();
return future.get();
}
}
今天的文章springCloud Feign 异步调用远程服务DEMO分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/64971.html