标签:http net app def framework 一个 方法 img spring
首先先创建一个FeignConfig类,代码如下:
package com.xing.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import feign.Contract; import feign.Feign; @Configuration public class FeignConfig { //配置是在FeignClient中的接口中使用Feign自带的注解 @Bean public Contract feignContract(){ return new feign.Contract.Default(); } //禁用Hystrix @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }
第一个个bean配置的是使用Feign的默认注解,添加第一个配置之后,下面的UserInterface类就一定要使用@RequestLine这个注解才行(这个是Feign的注解),使用@RequestMapping会报Method findByNameEn not annotated with HTTP method type (ex. GET, POST)的异常,
如果你要使用@RequestMapping这个注解你就把feignContract这个方法注释掉,就好了。
第二个bean配置的是是禁用Hystrix
接着Feign调用User服务的接口类UserInterface中添加一些东西,我这里就直接把这个类贴出来了
package com.xing.movie.FeignInteface; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.xing.config.FeignConfig; import com.xing.movie.entity.User; import feign.Param; import feign.RequestLine; @FeignClient(name = "xing-user" ,fallback = UserInterfaceFallback.class,configuration = FeignConfig.class)//服务名 public interface UserInterface {
//@RequestMapping(value ="/user/findByNameEn/{nameEn}" ,method =RequestMethod.GET ) //必须使用RequestMapper,使用GetMapping启动报错
@RequestLine("GET /user/findByNameEn/{nameEn}") //当配置了feignContract之后要使用这个Feign的注解,使用上面的报错
public User findByNameEn(@PathVariable("nameEn") String nameEn);//@PathVariable后面需要指定nameEn,不然可能报错 } //不一定要内部类可以是外部类 @Component class UserInterfaceFallback implements UserInterface { @Override public User findByNameEn(String nameEn) { User user = new User(); user.setName(""); user.setNameEn(""); user.setId(0); return user; } }
这样就可以禁用掉Feign的Hystrix,测试成功,完整源码在https://github.com/OnlyXingxing/SpringCloud
标签:http net app def framework 一个 方法 img spring
原文地址:https://www.cnblogs.com/xing-12/p/9952971.html