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-wemedia的启动类上添加@EnableFeignClients注解
@EnableFeignClients(basePackages = "com.xxx.feign.article")
  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
...
...
05-07 23:23