在启动类上添加@EnableFeignClients注解来启用Feign的客户端功
能
添加UserService接口完成对user-service服务的接口绑定
通过@FeignClient注解实现了一个Feign客户端,其中的value为user-service表示这是对user-
service服务的接口调用客户端。可以回想下user-service中的UserController,只需将其改为接
口,保留原来的SpringMvc注释即可。
添加UserFeignController调用UserService实现服务调用
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApplication.class, args);
}
}
@FeignClient(value = "user-service", fallback = UserFallbackService.class)
public interface UserService {
@PostMapping("/user/insert")
Result insert(@RequestBody User user);
@GetMapping("/user/{id}")
Result<User> getUser(@PathVariable Long id);
@GetMapping("/user/listUsersByIds")
Result<List<User>> listUsersByIds(@RequestParam List<Long> ids);
@GetMapping("/user/getByUsername")
Result<User> getByUsername(@RequestParam String username);
@PostMapping("/user/update")
Result update(@RequestBody User user);
@PostMapping("/user/delete/{id}")
Result delete(@PathVariable Long id);
}
@RestController
@RequestMapping("/user")
public class UserFeignController {
@Autowired
private UserService userService;
@PostMapping("/insert")
public Result insert(@RequestBody User user) {
return userService.insert(user);
}