Spring Cloud Alibaba OpenFeign 契约配置

 

 

package com.wsm.order.config;

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

/**
 * 全局配置: 当使用@Configuration会将配置作用所有的报务提供方
 * 局部配置: 1 通过配置类:如果只针对个别服务进行配置,就不要加@Configuration
 *            2 通过配置文件
 */
//@Configuration
public class FeignConfig {

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

//    /**
//     * 修改契约配置,支持Feign原生的注解
//     * @return
//     */
//    @Bean
//    public Contract feignContract(){
//        return new Contract.Default();
//    }

}

 

package com.wsm.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}")
    public String get(@Param("id") Integer id);

//    @RequestMapping("/{id}")
//    public String get(@PathVariable("id") Integer id);
}

 

 

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
#springboot 默认的日志级别是info,feign的debug日志级别就不会输出
logging:
  level:
#    com.wsm.order.feign: debug
    com.wsm.order.feign.StockFeignService: debug
# Feign 日志局部配置
feign:
  client:
    config:
      product-service:
        loggerLevel: BASIC
        contract: feign.Contract.Default #设置为默认的契约 (还原成原生注解)

 

 

 

 

posted @ 2021-11-08 00:20  残星  阅读(165)  评论(0编辑  收藏  举报