提供接口的服務爲被調用方,發起Feign 請求的服務爲調用方,Feign 配置在調用方,也成爲 Feign client 客戶端spring
1. 在調用方配置pom,引入依賴json
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2. 寫一個 interfaceapp
//www.1b23.com @FeignClient("xing-user")//服務名public interface UserInterface { @RequestMapping(value ="/user/findByNameEn/{nameEn}" ,method =RequestMethod.GET )//必須使用RequestMapper,使用GetMapping啓動報錯 public User findByNameEn(@PathVariable("nameEn") String nameEn);//@PathVariable後面須要指定nameEn,否則可能報錯 }
3. 啓動類上加上註解ide
@EnableFeignClients(basePackages = {"org.fh.movie"})指定上面接口所在的類,能夠只到父包spa
4. 在處理類 Controller 中調用上面寫上orm
@Autowired private UserInterface userInterface; //www.1b23.com @GetMapping(value = "/findUserByNameEn/{nameEn}",produces = { "application/json;charset=UTF-8" })public User findUserByNameEn(@PathVariable String nameEn) { User user = userInterface.findByNameEn(nameEn); System.out.println("findUserByNameEn----"+user); return user; }
被調用方,接口正常寫便可接口