SpringCloud 学习笔记八 多模块搭建-实现客户端均衡(feign)

前面我们集成了ribbon实现了客户端的负载均衡,这里我们要使用feign实现

为什么要使用Feign

在前一章节,我们使用Ribbon作为客户端负载均衡完成了订单服务和用户服务的通信,其实我们可以发现,当我们通过RestTemplate调用其它服务时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻。而Feign的服务调用方式对于程序员来说更为友好,它基于Ribbon进行了封装,把一些负责的url和参数处理细节屏蔽起来,我们只需要简单编写Fiegn的客户端接口就可以像调用本地service去调用远程微服务。
为了避免和前面的案例混淆,我这里又写了一个子模块pay模块

导入相关依赖:

<dependencies>
        <!--1.导入EurekaClient的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--2.导入Feign的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--web包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--user-common-->
        <dependency>
            <groupId>cn.wly</groupId>
            <artifactId>springcloud-user-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

主配置类:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //自动开启feign服务
public class PayServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(PayServerApplication.class);
    }
}

yml:

eureka:
  client:
    serviceUrl:  #注册中心地址
      defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
  instance:
    prefer-ip-address: true  #使用ip地址注册
    instance-id: pay-server:1040   #实列id地址
spring:
  application:
    name:  pay-server  #服务名
server:
  port: 1040 #端口

Feign的客户端接口是用来调用微服务的,编写一个用来调用user服务的客户端接口,如下:

/**
 * url路径要与user服务的路径一致,
 * 服务名要一致 ,
 * 参数要一致 ,
 * 返回值类型要一致。
 */
 //注解了@FeignClient的接口将会被扫描到然后交给Spring管理。
@FeignClient(value = "user-server") //这里指定要访问的服务名 
public interface UserFeignClient {

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Long id);
}

创建payController类

@RestController
public class PayController {
    
    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/pay/{id}")
    public User getUserById(@PathVariable("id") Long id){
        return userFeignClient.getUserById(id);
    }
}

启动服务注册成功:在这里插入图片描述
浏览器多次访问:peer1:1040/pay/1
在这里插入图片描述

在这里插入图片描述
我们可以看出feign默认使用的是轮询策略

feign的工作原理

要使用Feign,我们除了导入依赖之外,需要主配置类通过@EnableFeignClients(value="")注解开启Feign,也可以通过value属性指定了Feign的扫描包。同时我们需要为Feign编写客户端接口,接口上需要注解@FeignClient标签。 当程序启动,注解了@FeignClient的接口将会被扫描到然后交给Spring管理。

当请求发起,会使用jdk的动态代理方式代理接口,生成相应的RequestTemplate,Feign会为每个方法生成一个RequestTemplate同时封装好http信息,如:url,请求参数等等

最终RequestTemplate生成request请求,交给Http客户端(UrlConnection ,HttpClient,OkHttp)。然后Http客户端会交给LoadBalancerClient,使用Ribbon的负载均衡发起调用。

Feign的参数配置

原文链接:https://blog.csdn.net/u014494148/article/details/105038349

负载均衡配置
Feign已经集成了Ribbon,所以它的负载均衡配置基于Ribbon配置即可,这里使用xml简单配置负载均衡策略如下:

user-server:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Feign的超时配置
如果在服务调用时出现了 “feign.RetryableException : Read timed out…”错误日志,说明Ribbon处理超时 ,我们可以配置Ribbon的超时时间:

ribbon:
	ConnectTimeout: 3000
    ReadTimeout: 6000

如果服务调用现“com.netflix.hystrix.exception.HystrixRuntimeException:… timed - out and no fallback available” 错误日志,是因为Hystrix超时,默认Feign集成了Hystrix,但是高版本是关闭了Hystrix,我们可以配置Hystrix超时时间:

feign:
   hystrix:
       enabled: true #开启熔断支持
hystrix:
  command:
      default:
        execution:
          isolation:
            thread:
              timeoutInMilliseconds: 6000   #hystrix超时时间

Feign开启日志调试

官方文档:https://cloud.spring.io/spring-cloud-openfeign/reference/html/#feign-logging

.配置Feign日志打印内容
有的时候我们需要看到Feign的调用过程中的参数及相应,我们可以对Feign的日志进行配置,Feign支持如下几种日志模式来决定日志记录内容多少:

  • NONE,不记录(DEFAULT)。
  • BASIC,仅记录请求方法和URL以及响应状态代码和执行时间。
  • HEADERS,记录基本信息以及请求和响应标头。
  • FULL,记录请求和响应的标题,正文和元数据

创建Feign配置类

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;	//打印Feign的所有日志
    }
}

.配置日志打印级别
配置UserFeignClient的日志打印级别,上面的配置打印Feign的那些内容,下面这个是配置日志框架打印日志的级别,不修改可能打印不出来日志,DEBUG打印日志调试信息。

logging:
  level:
    cn.itsource.springboot.feignclient.UserFeignClient: debug

原文链接:https://blog.csdn.net/u014494148/article/details/105038349

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值