springcloud之openfegin

需求

  • 在使用restTemplate访问远程接口的时候,我们难以将接口管理起来,当接口变动的时候我们可能会修改多处。所以,需要通过springcloud的openfegin来解决这个问题。

1.概念

OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

2.引入依赖

 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>

3.创建启动类,并添加注解

@EnableFeignClients申明该项目是Feign客户端,扫描对应的feign client。

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class OpenfeginOrder80 {
    public static void main(String[] args) {
        SpringApplication.run(OpenfeginOrder80.class,args);
    }
}

4.创建API接口

我们需要集中化管理API,就可以通过接口统一管理,需要新增商品服务的接口类OpenFeginService ,然后添加@FeignClient(name = “cloud-payment-service”)注解,其中name表示的是我们要访问的服务名称。 @GetMapping("/v1payp/payment/get/{id}")和我们访问提供者的这个方法路径是一致的。

@Component
@FeignClient(name = "cloud-payment-service")
public interface OpenFeginService {

    @GetMapping("/v1payp/payment/get/{id}")
    @ApiOperation(value = "根据id查询payment",notes = "cloud-provider-payment8001.getPaymentById")
    @ApiImplicitParam(name = "id", value = "payment的id", required = false, dataType = "String", paramType = "path")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long  id);
}

5.修改controller

现在接口已经封装在OpenFeginService 里面了,现在我们不需要通过微服务名加地址去访问了。

import com.study.cloud.entities.CommonResult;
import com.study.cloud.entities.Payment;
import com.study.cloud.service.OpenFeginService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
public class OpenfeginController {

    @Resource
    private OpenFeginService openFeginService;
    
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentByid(@PathVariable("id") Long id){
        return openFeginService.getPaymentById(id);
    }
}

6.测试访问结果

在这里插入图片描述
在这里插入图片描述

7.超时控制

直接在application.yaml上ribbon的相关信息, Readtimeout: 5000,指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间.ConnectTimeout: 5000,指的是建立连接后,从服务器读取资源所用的时间

eureka:
  client:
    #表示是否将自己注册金eurekaserver
    register-with-eureka: true
    #是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,   http://eureka7002.com:7002/eureka/
ribbon:
  #指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间
  Readtimeout: 5000
  #指的是建立连接后,从服务器读取资源所用的时间
  ConnectTimeout: 5000

8.日志控制

首先编写一个config类

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeginConfig {

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

然后在application.yaml文件中配置相关内容

eureka:
  client:
    #表示是否将自己注册金eurekaserver
    register-with-eureka: true
    #是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,   http://eureka7002.com:7002/eureka/
ribbon:
  #指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间
  Readtimeout: 5000
  #指的是建立连接后,从服务器读取资源所用的时间
  ConnectTimeout: 5000
logging:
  level:
    #fegin日志以什么级别监控哪个接口
    com.study.cloud.service.OpenFeginService: debug

其中,logging下就是配置日志的相关内容

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值