feign的使用在spring cloud中的使用
导入依赖1.4.4.RELEASE
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
1,首先在类或实现类里调FeignService的请求接口
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class feignClient {
@Autowired
private FeignSevice feignSevice;
public List<StudentVO> getstudentAllk(List<StudentVO> student,String Id) {
JSONObject json = FeignSevice.feignClient (Id,student);
}
}
2,通过FeignService接口请求,服务名为“zx-student”的服务;如果有信息需要请求头通过@RequestHeader放入;
@RequestMappering塞入:请求的方法路径,请求方式;@RequestBody请求方法入参
@Component
@FeignClient(value = "zx-student") //服务名
public interface FeignSevice{
// 请求路径,请求方式 @RequestHeader请求头
@RequestMapping(value = "/sw/ass/getStudentAll",method = RequestMethod.POST)
JSONObject feignClient(@RequestHeader("tenant") String tenant,@RequestBody List<StudentVO> student);
}
3,最后需要在启动类里加入两个注解
@EnableFeignClients
@EnableTransactionManagement