IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

SpringCloud 笔记 六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅。

1、概念:Feign 接口服务

2、具体内容

现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例:

Dept dept = this.restTemplate
                .exchange(DEPT_GET_URL + id, HttpMethod.GET,
                        new HttpEntity<Object>(this.headers), Dept.class)
                .getBody();

所有的数据的调用和转换都必须由用户自己来完成,而我们本身不擅长这些,我们习惯的编程模式是:通过接口来实现业务的操作,而不是通过具体的 Rest 数据。

2、1、Feign 基本使用

为了方便起见现在将“microcloud-consumer-80”模块复制为了“microcloud-consumer-feign”模块。

1、 【microcloud-consumer-feign】为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

feign 包含了 Ribbon 支持,所以导入了以上的依赖包之后就表示项目之中已经存在有了 ribbon 相关支持库。

2、 【microcloud-service】建立一个新的模块,这个模块专门负责客户端接口的定义;

3、 【microcloud-service】修改 pom.xml 配置文件,引用“microcloud-api”模块,这样就可以使用到 VO 类了;

        <dependency>
            <groupId>cn.study</groupId>
            <artifactId>microcloud-api</artifactId>
        </dependency>

4、 【microcloud-service】此时如果要通过 Feign 进行远程 Rest 调用,那么必须要考虑服务的认证问题。

· 此时可以删除原始的 RestConfig 进行的配置处理,然后添加feign的认证配置类

package cn.study.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("studyjava", "hello");
    }
}

5、 【microcloud-service】建立一个 IDeptClientService 接口;

package cn.study.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.study.commons.config.FeignClientConfig;
import cn.study.vo.Dept;
/**
 * 通过注解@FeignClient添加接口对应的远程微服务名称value="MICROCLOUD-PROVIDER-DEPT"和
 * 服务的认证configuration=FeignClientConfig.class
 *
 */
@FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class)
public interface IDeptClientService {
    @RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}")
    public Dept get(@PathVariable("id") long id) ;
    @RequestMapping(method=RequestMethod.GET,value="/dept/list")
    public List<Dept> list() ;
    @RequestMapping(method=RequestMethod.POST,value="/dept/add")
    public boolean add(Dept dept) ;
}

6、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 开发包:

        <dependency>
            <groupId>cn.study</groupId>
            <artifactId>microcloud-service</artifactId>
        </dependency>

7、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序类;

package cn.study.microcloud.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;

@RestController
public class ConsumerDeptController {
    @Resource
    private IDeptClientService deptService ;
    @RequestMapping(value = "/consumer/dept/get")
    public Object getDept(long id) {
        return this.deptService.get(id);
    }
    @RequestMapping(value = "/consumer/dept/list")
    public Object listDept() {
        return this.deptService.list();
    }
    @RequestMapping(value = "/consumer/dept/add")
    public Object addDept(Dept dept) throws Exception {
        return this.deptService.add(dept);
    }
}

8、 【microcloud-consumer-feign】修改程序启动主类,追加操作处理。

package cn.study.microcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"cn.study.service"})//进行接口IDeptClientService的扫描生成使得可以注入到ConsumerDeptController里面
public class Consumer_80_StartSpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(Consumer_80_StartSpringCloudApplication.class,
                args);
    }
}

9、 启动测试:http://client.com/consumer/dept/get?id=1

· 可以发现 Feign 在处理的时候自带有负载均衡的配置项

2、2、Feign 相关配置

1、 【microcloud-consumer-feign】Feign 之中最为核心的作用就是将 Rest 服务的信息转换为接口,但是在实际的使用之中也需要考虑到一些配置情况,例如:数据压缩,Rest 的核心本质在于:JSON 数据传输(XML、文本),于是就必须思考一种情况,如果用户发送的数据很大,这个时候可以考虑修改 application.yml 配置文件对传输数据进行压缩;

feign:
  compression:
    request:
      mime-types:       # 可以被压缩的类型
      - text/xml
      - application/xml
      - application/json
      min-request-size: 2048 # 超过2048的字节进行压缩

2、 如果有需要则可以在项目之中开启 feign 的相关日志信息(默认不开启):

· 【microcloud-consumer-feign】修改 application.yml 配置文件,追加日志追踪:

logging:
  level:
    cn.study.service: DEBUG

· 【microcloud-service】修改 FeignClientConfig,开启日志的输出:

package cn.study.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.Logger;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
    @Bean
    public Logger.Level getFeignLoggerLevel() {
        return feign.Logger.Level.FULL ;
    }
    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("studyjava", "hello");
    }
}

再次运行程序现在可以观察到如下的流程:

· 当使用 Feign 要通过接口的方法访问 Rest 服务的时候会根据设置的服务类型发出请求,这个请求是发送给 Eureka(地址: “http://MICROCLOUD-PROVIDER-DEPT/dept/list”);

· 随后由于配置了授权处理,所以继续发送授权信息(“Authorization”);

· 在进行服务调用的时候 Feign 融合了 Ribbon 技术,所以也支持有负载均衡的处理;

总结:Feign = RestTempate + HttpHeader + Ribbon + Eureka 综合体 = 业务接口的自动实例化

来源:https://www.cnblogs.com/leeSmall/category/1185489.html

文章永久链接:https://tech.souyunku.com/?p=14000


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(94) 打赏



未经允许不得转载:搜云库技术团队 » SpringCloud 笔记 六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367