Springboot集成feign远程调用

发布于:2024-05-07 ⋅ 阅读:(26) ⋅ 点赞:(0)

需求:在leadnews-wemedia微服务里需要调用leadnews-article微服务的接口。新建一个支持feign调用的名为heima-leadnews-feign-api的模块

  1. heima-leadnews-feign-api的pom文件里导入openfeign依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. heima-leadnews-feign-api里定义远程调用接口
@FeignClient(value = "leadnews-article",path = "/api/v1/article")
public interface IArticleClient {

    /****、
     * 文章保存
     */
    @PostMapping(value = "/save")
    ResponseResult<Long> save(@RequestBody ArticleDto dto);
}
  1. 在leadnews-wemedia的pom文件中引入
<dependency>
    <groupId>com.heima</groupId>
    <artifactId>heima-leadnews-feign-api</artifactId>
</dependency>
  1. leadnews-article的其他文件
  • ApArticleController
@RestController
@RequestMapping(value = "/api/v1/article")
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;

    /****、
     * 文章保存
     */
    @PostMapping(value = "/save")
    public ResponseResult<Long> save(@RequestBody ArticleDto dto){
        return apArticleService.saveArticle(dto);
    }
	...

}
  • leadnews-article配置
server:
  port: 51802
spring:
  application:
    name: leadnews-article
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.33.31:8848
      config:
        server-addr: 192.168.33.31:8848
        file-extension: yml
...
...