4.cloud-OpenFeign服务接口调用

https://github.com/spring-cloud/spring-cloud-openfeign

1.consul使用步骤

  1.1 pom



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

 


    org.springframework.cloud
    spring-cloud-starter-consul-discovery

  1.2 yml

server:
  port: 80

spring:
  application:
    name: cloud-order-service
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

  1.3 业务类

    cloud-payment:consul服务名

@Service
@FeignClient(value = "cloud-payment")
public interface PaymentFeignService {

    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);

}

  1.4 控制器

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }

}

  1.5 主程序

@EnableFeignClients
@SpringBootApplication
public class OrderOpenFeignMain {

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

  1.5 

2.eureka使用步骤

  2.1 pom



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




    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

  2.2 yml

eureka:
  instance:
    instance-id: order${server.port}
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 10
    lease-expiration-duration-in-seconds: 10

  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

  2.3 主程序

@EnableFeignClients
@SpringBootApplication
public class OrderOpenFeignMain {

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

3.超时

feign:
  client:
    config:
      default:
        #建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
        connect-timeout: : 6000
        #指建立连接后从服务端读取到可用资源所用的时间
        read-timeout: : 10000

4.日志

  4.1 日志级别

        1) NONE:默认,不显示日志

        2) BASIC:仅记录请求方法

        3) HEADERS

        4) FULL

  4.2 配置日志bean

package com.example.cloud.config;

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

@Configuration
public class FeignConfig {

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

  4.2 YML

logging:
  level:
    com.example.cloud.config.FeignConfig: debug
 

你可能感兴趣的:(SpringCloud,java,开发语言)