SpringCloud Alibaba 2021微服务实战二十六 openfeign最佳实践

1,依赖pom



    
        cloud_alibaba_learn
        com.liu.learn
        1.0-SNAPSHOT
    
    4.0.0

    cloud_order

    

       
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-undertow
        

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            com.squareup.okhttp3
            okhttp
        
        
        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        


    


配置文件

server:
  port: 8090
spring:
  #profiles:
    #active: dev
  application:
    main:
      allow-bean-definition-overriding: true
    name: order-service #服务名
    cloud:
      nacos:
        discovery:
          server-addr: 127.0.0.1:8848
feign:
  sentinel:
    #为feign整合Sentinel
    enabled: true
    okhttp:
    enabled: true
  httpclient:
    enabled: false
    max-connections: 1000
    max-connections-per-route: 100
server:
  undertow:
    max-http-post-size: 0
    # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,数量和CPU 内核数目一样即可
    io-threads: 4
    # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载  io-threads*8
    worker-threads: 32
    # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
    # 每块buffer的空间大小,越小的空间被利用越充分
    buffer-size: 1024
    # 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region
    #   buffers-per-region: 1024 # 这个参数不需要写了
    # 是否分配的直接内存
    direct-buffers: true

 

feign客户端





import com.liu.consts.SecurityConstants;
import com.liu.consts.ServiceNameConstants;
import com.liu.entity.SysLog;
import com.liu.feign.factory.RemoteLogServiceFallbackFactory;
import com.liu.rs.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
//value = ServiceNameConstants.LOG_SERVICE 改为自己服务名
//CommonResult 返回值类
@FeignClient(contextId = "remoteLogService", value = ServiceNameConstants.LOG_SERVICE,
		fallbackFactory = RemoteLogServiceFallbackFactory.class)
public interface RemoteLogService {

	/**
	 * 保存日志
	 * @param sysLog 日志实体
	 * @param from 内部调用标志
	 * @return succes、false
	 */
	@PostMapping("/log")
    CommonResult saveLog(@RequestBody SysLog sysLog, @RequestHeader(SecurityConstants.FROM) String from);

}
import com.liu.feign.RemoteLogService;
import com.liu.feign.fallback.RemoteLogServiceFallbackImpl;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;


@Component
public class RemoteLogServiceFallbackFactory implements FallbackFactory {

	@Override
	public RemoteLogService create(Throwable throwable) {
		RemoteLogServiceFallbackImpl remoteLogServiceFallback = new RemoteLogServiceFallbackImpl();
		remoteLogServiceFallback.setCause(throwable);
		return remoteLogServiceFallback;
	}

}
import com.liu.entity.SysLog;
import com.liu.feign.RemoteLogService;
import com.liu.rs.CommonResult;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class RemoteLogServiceFallbackImpl implements RemoteLogService {

	@Setter
	private Throwable cause;

	/**
	 * 保存日志
	 * @param sysLog 日志实体
	 * @param from 内部调用标志
	 * @return succes、false
	 */
	@Override
	public CommonResult saveLog(SysLog sysLog, String from) {

		log.error("feign 插入日志失败,{}", cause.getMessage());
		cause.printStackTrace();
		log.error("feign 插入日志失败,{}", cause.getLocalizedMessage());
		return null;
	}

}
  1. fallback和fallbackFactory是有一定的冲突的,你可以理解为fallbackFactory是fallback的一个增强版,

  2.  
  3. 因为fallback只是复写方法 ,没办法打印日志,而这个更强一点,fallbackFactory可以拦截异常这一块

你可能感兴趣的:(sringcloud系统整理,SpringCloud,Alibaba微服务实战)