栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

4、openFeign契约配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4、openFeign契约配置

Spring Cloud 在 Feign 的基础上做了扩展,使用 Spring MVC 的注解来完成Feign的功能。原生的 Feign 是不支持 Spring MVC 注解的,如果你想在 Spring Cloud 中使用原生的注解方式来定义客户端也是可以的,通过配置契约来改变这个配置,Spring Cloud 中默认的是 SpringMvcContract。

Spring Cloud 1 早期版本就是用的原生Fegin. 随着netflix的停更替换成了Open feign

为什么需要契约配置?
答:比方说,以前springcloud1.0的早期版本它就是用的原生的feign,随着netflix的停更才替换成了Open feign,Open feign在 Feign 的基础上做了扩展,使用 Spring MVC 的注解来完成Feign的功能,从而降低了学习的成本。假如说我有项目就是用的spring cloud 1.0早期的版本,我现在想做版本的升级,想升级为比较新的版本,我想用openFeign;但是我一升级的话,项目中的feign的接口当中的原生注解是不是都得改掉,在小的代码变更都会引起bug的出现。我们尽量能少改代码就改代码。我们用契约配置就可以符合我们的功能。我们既能升级我的spring cloud的版本,我又可以保留我原生的feign注解,从而降低修改代码产生的风险。

全局配置 在order-feign模板修改 第一步:修改FeignConfig
package com.example.order.config;

import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    
    @Bean
    public Contract feignContract(){
        return new Contract.Default();
    }
}

第二步:修改所有Feign服务
package com.example.order.feign;

import com.example.order.config.FeignConfig;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "stock-service",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {

    
    @RequestLine("GET /reduck")    //相当于@RequestMapping("/reduck")
    String reduck();


    
}

package com.example.order.feign;

import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "product-service",path = "/product")
public interface ProductFeignService {

    
    @RequestLine("GET /{id}") //相当于@RequestMapping({id})
    String get(@Param("id") Integer id); //相当于PathVariable("id")
}

第三步:重启order-openfeign 重启order-openfeign,访问http://localhost:8086/order/add发现正常访问ok 局部配置 第一种:通过配置类 第一步:修改FeignConfig
package com.example.order.config;

import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;


//@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    
    @Bean
    public Contract feignContract(){
        return new Contract.Default();
    }
}

第二步:修改StockFeignService和ProductFeignService
package com.example.order.feign;

import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "product-service",path = "/product")
public interface ProductFeignService {

    
    @RequestMapping("/{id}") //@RequestLine相当于@RequestMapping({id})
    String get(@PathVariable("id") Integer id); //@Param相当于@PathVariable("id")
}

package com.example.order.feign;

import com.example.order.config.FeignConfig;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "stock-service",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {

    
    @RequestLine("GET /reduck")    //相当于@RequestMapping("/reduck")
    String reduck();

}

第三步:重启order-openfeign 重启order-openfeign,访问http://localhost:8086/order/add发现正常访问ok 第二种:通过配置文件 第一步:修改FeignConfig
package com.example.order.config;

import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;


//@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    
//    @Bean
//    public Contract feignContract(){
//        return new Contract.Default();
//    }
}

第二步:修改application.yml
server:
  port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  # 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出
  # logging.level=debug这样配置是对所有的日志级别进行配置
  # 该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.example.order.feign=debug
logging:
  level:
    com.example.order.feign: debug
feign:
  client:
    config:
      # 提供方的服务名
      product-service:
        #请求日志级别
        loggerLevel: BASIC
        contract: feign.Contract.Default #设置为默认的契约(还原成原生注解)

第三步:修改StockFeignService和ProductFeignService
package com.example.order.feign;

import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "product-service",path = "/product")
public interface ProductFeignService {

    
    @RequestLine("GET /{id}") //@RequestLine相当于@RequestMapping({id})
    String get(@Param("id") Integer id); //@Param相当于@PathVariable("id")
}

package com.example.order.feign;

import com.example.order.config.FeignConfig;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "stock-service",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {

    
    @RequestMapping("/reduck")    //相当于@RequestMapping("/reduck")
    String reduck();


    
}

第四步:重启order-openfeign 重启order-openfeign,访问http://localhost:8086/order/add发现正常访问ok
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号