提供接口的服务为被调用方,发起Feign 请求的服务为调用方,Feign 配置在调用方,也成为 Feign client 客户端
1. 在调用方配置pom,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2. 写一个 interface
//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. 启动类上加上注解
@EnableFeignClients(basePackages = {"org.fh.movie"})指定上面接口所在的类,可以只到父包
4. 在处理类 Controller 中调用上面写上
@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;
}
被调用方,接口正常写即可