SpringCloud实战之路 | 应用篇(五)远程调用组件Feign

SpringCloud实战之路 | 应用篇(五)远程调用组件Feign

文章内容输出来源:拉勾教育Java高薪训练营;

Feign简介

Feign是Neflix开发的轻量级restful的HTTP服务客户端,是以Java接口注解方式调用HTTP请求。不需要我们拼接url通过调用restTemplate的api,只需要创建一个接口加上写注解直接调用接口即可。

本质: 封装http嗲用流程,更符合面向接口化编程的习惯,类似Dubbo的服务调用

Feign应用

基本配置

这里创建两个服务cloud-service-code作为消费者服务去远程调用cloud-service-email提供者服务

cloud-service-email提供者
1.controller中创建方法sendEmail()用以消费者调用

@RestController
@RequestMapping("/email")
public class EmailController  {


    @PostMapping("/{email}/{code}")
    public Boolean sendEmail(@PathVariable String email,@PathVariable String code) {

        return true;
    }
}

2.yml配置信息

server:
  port: 8082
#注册到Eureka服务中心
eureka:
  client:
    service-url:
      # 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
      defaultZone: http://EurekaServerA:8761/eureka,http://EurekaServerB:8762/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
  application:
    name: cloud-service-email

management:
  endpoints:
    web:
      exposure:
        include: "*"
  # 暴露健康接口的细节
  endpoint:
    health:
      show-details: always


3.配置启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EmailApplication {

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

}

cloud-service-code消费者

1.引入Feign依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
server:
  port: 8081
#注册到Eureka服务中心
eureka:
  client:
    service-url:
      # 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
      defaultZone: http://EurekaServerA:8761/eureka,http://EurekaServerB:8762/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
  application:
    name: cloud-service-code

management:
  endpoints:
    web:
      exposure:
        include: "*"
  # 暴露健康接口的细节
  endpoint:
    health:
      show-details: always

2.服务消费者启动类添加对Feign支持的注解@EnableFeignClients
该注解会同时支持hystrix所以不需要再添加hystrix注解@EnableCircuitBreaker

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients  // 开启Feign 客户端功能
@EntityScan("com.cloud.pojo")
public class CodeApplication {

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

3.创建Feign接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

// name:调⽤的服务名称,和服务提供者yml⽂件中spring.application.name保持⼀致
@FeignClient(name= "cloud-service-email")
public interface EmailServiceFeignClient {
	
	//调⽤的请求路径(与cloud-service-email提供者的controller方法保持一致)
    @PostMapping("/{email}/{code}")
    public Boolean sendEmail(@PathVariable("email") String email, @PathVariable("code") String code);

}

4.在controller中直接就可以完成远程调用

package com.cloud.controller;

import com.cloud.feign.EmailServiceFeignClient;
import com.cloud.service.CodeService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private EmailServiceFeignClient emailServiceFeignClient;

    @PostMapping("/create/{email}")
    public Boolean createCode(@PathVariable String email) {

        String code = RandomStringUtils.randomAlphanumeric(6);

        codeService.addCode(email, code);

        Boolean result = emailServiceFeignClient.sendEmail(email, code);

        return result;
    }

}

负载均衡配置

Feign本身已经集成了Ribbon和Hystrix的依赖和自动配置,不需要再额外引入依赖,可用通过ribbon.xx来进行全局配置,也可以服务名.ribbon.xx来对指定服务进行配置

#针对的被调⽤⽅微服务名称,不加就是全局⽣效
cloud-service-code:
  ribbon:
    #请求连接超时时间
    #ConnectTimeout: 2000
    #请求处理超时时间
    #ReadTimeout: 5000
    #对所有操作都进⾏重试
    OkToRetryOnAllOperations: true
    ####根据如上配置,当访问到故障请求的时候,它会再尝试访问⼀次当前实例(次数由MaxAutoRetries配置),
    ####如果不⾏,就换⼀个实例进⾏访问,如果还不⾏,再换⼀次实例访问(更换次数由MaxAutoRetriesNextServer配置),
    ####如果依然不⾏,返回失败信息。
    MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第⼀次调⽤
    MaxAutoRetriesNextServer: 0 #切换实例的重试次数
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整

熔断器配置

1.启用熔断功能配置

# 开启Feign的熔断功能
feign:
  hystrix:
    enabled: true

其余配置与Hystrix配置方式相同

由于两个超时时间设置(Feign/Hystrix),熔断的时候是根据这两个时间的最小值进行
2.修改feign接口

/// 使⽤fallback的时候,类上的@RequestMapping的url前缀限定,改成配置在@FeignClient的path属性中
@FeignClient(value = "cloud-service-email",fallback = EmailFallback.class,path = "/email")
public interface EmailServiceFeignClient {

    @PostMapping("/{email}/{code}")
    public Boolean sendEmail(@PathVariable("email") String email, @PathVariable("code") String code);

}

3.熔断方法
降级回退逻辑需要定义⼀个类,实现FeignClient接⼝,实现接⼝中的⽅法

import org.springframework.stereotype.Component;

@Component
public class EmailFallback implements EmailServiceFeignClient{

    @Override
    public Boolean sendEmail(String email, String code) {
        return false;
    }

}

日志配置

1.开启Feign日志功能及级别

// Feign的⽇志级别(Feign请求过程信息)
// NONE:默认的,不显示任何⽇志----性能最好
// BASIC:仅记录请求⽅法、URL、响应状态码以及执⾏时间----⽣产问题追踪
// HEADERS:在BASIC级别的基础上,记录请求和响应的header
// FULL:记录请求和响应的header、body和元数据----适⽤于开发及测试环境定位问题
@Configuration
public class FeignConfig {
	@Bean
 	Logger.Level feignLevel() {
 		return Logger.Level.FULL;
 	}
}

2.配置log级别为debug

logging:
  level:
 	# Feign⽇志只会对⽇志级别为debug的做出响应
    com.cloud.feign.EmailServiceFeignClient: debug

猜你喜欢

转载自blog.csdn.net/qq_23830637/article/details/106098017