OpenFeign项目

4 篇文章 0 订阅

1.配置类

pom
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

        </dependency>
        <!-- common -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
</dependencies>
yml

server:
  port: 8082
spring:
  application:
    name: cloud-order-service-openfeign
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka  单机版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   # 集群
ribbon:
  ReadTimeout: 5000 # 超时配置
  ConnectTimeout: 5000
logging: #日志配置
  level:
    com.cloud.openfeign.service.OpenFeignService: debug

主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
public class Application8082 {
    public static void main(String[] args) {
        SpringApplication.run(Application8082.class, args);
    }
}

2.业务层

controller : 调用服务端接口
@RestController
public class OpenFeignController {

    @Autowired
    private OpenFeignService openFeignService;

    @GetMapping("/consumer/openfeign/{id}")
    public CommonResult selectPaymentById(@PathVariable(value = "id") Long id){
        //http://localhost:8082/consumer/openfeign/1
        CommonResult  result = openFeignService.selectPaymentById(id);
        return CommonResult.success().setData(result);
    }

    @GetMapping("/consumer/openfeign/timeout")
    public void timeout(){
        //http://localhost:8082/consumer/openfeign/timeout
        openFeignService.timeout();
    }

}
接口层
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OpenFeignService {

    @GetMapping(value = "/payment/{id}")
    CommonResult selectPaymentById(@PathVariable(value = "id") Long id);

    @GetMapping("/payment/timeout")
    void timeout();
}
配置类
@Configuration
public class FeignLogConfig {
    // NONE, 不记录
    //        BASIC, 仅记录请求方法、URL以及响应状态码和执行时间
    //        HEADERS, BASIC + 记录请求和响应的头信息
    //        FULL; 记录所有请求与响应的明细, 包括头信息、 请求体、 元数据等

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

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值