定义FeignService
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = "调用服务名")
public interface FeignService {
//请求方式根据被调用方定义 JSONObject 返回实体 可根据自己需要自定义实体接收 feign会自动转换属性名一致的字段
@PostMapping("/地址")
JSONObject xxxxx(@RequestBody 请求实体);
@GetMapping("/地址")
返回接收 xxxx(@RequestParam(value = "xxx") xxx);
}
impl层
//自动装配 直接调用拿到返回进行处理即可
@Autowired
private FeignService feignService;
feign 也可以支持服务间http调用
在feign注解中添加 url = "url"
本文介绍了如何在Spring Cloud环境中定义并使用FeignClient进行服务间的HTTP调用。通过@FeignClient注解指定调用的服务名,@PostMapping和@GetMapping注解定义请求方法及URL,Feign会自动转换请求参数。在实现层,通过@Autowired直接注入FeignService即可方便地调用其他服务,实现微服务间的通信。此外,还提到了Feign支持自定义URL进行服务调用的灵活性。
167万+

被折叠的 条评论
为什么被折叠?



