feign使用三步
1:引入jar
2:启动类扫描feign
3:feign声明
引入jar如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
启动类扫描
@EnableFeignClients(basePackages = {"com.feign"})
feign声明
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.ns.common.bean.Response; @FeignClient(name = "passport",url = "${feign.cunion}") public interface PassportFegin { @RequestMapping(value ="/doctor/patient/searchFuzzy",method = RequestMethod.GET) String searchFuzzy(@RequestParam("keyword") String keyword); @RequestMapping(value ="/doctor/patient/getById",method = RequestMethod.GET) Response<Object> getById(@RequestParam("id") String id); }
feign header配置
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.thc.security.util.ThreadLocalUtils; import feign.RequestInterceptor; @Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return requestTemplate -> requestTemplate.header("x-access-token", ThreadLocalUtils.getThreadLocalValue("x-access-token")); } }
配置文件
feign: c-union: http://xxxxxxxx/project
剩下的就是在业务层注入调用方法即可