Feign 远程调用

目录

代码架构

 feign-api 模块解析

架构

依赖

定义接口类

lead-news-article模块

架构

yml配置 

依赖

实现类

启动类

lead-news-wemedia模块

架构

调用

启动类


代码架构

Feign 远程调用_第1张图片

 feign-api 模块解析

架构

Feign 远程调用_第2张图片

依赖

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

定义接口类

@FeignClient(value = "leadnews-article")
public interface IArticleClient {

    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) ;
}

注解分析:

@FeignClient是Spring Cloud OpenFeign提供的注解,它的作用是创建一个Feign客户端。

value属性用于指定要调用的服务名称。这个名字需要在服务发现组件(如Eureka、Consul等)中注册过。

 

lead-news-article模块

架构

Feign 远程调用_第3张图片

yml配置 

server:
  port: 51802
spring:
  application:
    name: leadnews-article
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.74.128:8848
      config:
        server-addr: 192.168.74.128:8848
        file-extension: yml
        extension-configs:
          - data-id: lead-redis.yaml
            group: DEFAULT_GROUP
            refresh: true

分析:

spring:
  application:
    name: leadnews-article

feign模块依赖,根据name 去设置value的值。

 

依赖

使用feign的模块依赖

   
            com.heima
            heima-leadnews-feign-api
        

实现类

@RestController
public class ArticleClient implements IArticleClient {

    @Autowired
    private ApArticleService apArticleService;

    @Override
    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
        return apArticleService.saveArticle(dto);
    }
}

启动类

@SpringBootApplication
@EnableDiscoveryClient

@MapperScan("com.heima.article.mapper")
@EnableFeignClients(basePackages = "com.heima.apis")
@EnableAsync
public class ArticleApplication {

    public static void main(String[] args) {
        SpringApplication.run(ArticleApplication.class, args);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

注解分析:

@EnableFeignClients 是一个 Spring Cloud Feign 的注解,用于开启 Feign 客户端的功能。

basePackages 属性是指定扫描包含 Feign 客户端的包路径。例如你设置为 "com.heima.apis",那么 Spring 就会去 "com.heima.apis" 这个包及其子包下面寻找标注了 @FeignClient 注解的类,并将它们注册为 Feign 客户端。

这样你在代码中就可以通过 @Autowired 注入这些 Feign 客户端来进行远程调用了。

lead-news-wemedia模块

架构

保存文章功能需要用到远程调用

Feign 远程调用_第4张图片

调用

Feign 远程调用_第5张图片

启动类

@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.heima.wemedia.mapper")
@EnableFeignClients(basePackages = "com.heima.apis")
@EnableScheduling
public class WemediaApplication {

    public static void main(String[] args) {
        SpringApplication.run(WemediaApplication.class,args);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

你可能感兴趣的:(java微服务,java,spring,spring,boot)