springboot集成springcloud openfeign rest接口调用

1.描述

springcloud作为java的微服务框架,从java开发者的角度可以用一整套的cloud组件实现微服务编排治理,但是随着容器化如docker、kubernetes的兴起,微服务的治理编排工作使用cloud组件实际上就跟语言和代码耦合了,所以本文只使用openfeign组件做微服务直接的调用,主要目的是相比自己封装http更加方便,第三方接口统一整理方便维护

2.版本信息 

springboot版本:2.5.6

springcloud版本:2020.0.4

由于目前只使用了openfeign组件,控制了cloud与boot对应版本,方便后期按需引入

springboot与springcloud版本对应查询(一定要对应,否则冲突):

https://start.spring.io/actuator/info 建议火狐打开转为JSON方便查看

3.pom.xml



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring.cloud-version}
            pom
            import
        
    


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


    io.github.openfeign
    feign-okhttp
    10.12

 4.相关配置

application.properties

# openfeign配置
feign.httpclient.enabled=false   #禁用默认http url connect调用
feign.okhttp.enabled=true        #开启okhttp调用
feign.client.config.default.connect-timeout=15000
feign.client.config.default.read-timeout=60000
feign.client.config.default.logger-level=full #日志级别

 OpenfeignConfig.java 全局拦截配置(可选,主要针对调用前做一些处理)

package com.bylz.framework.openfeign;

import com.bylz.common.constans.Constants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Author: lizhi
 * Date: 2021-08-27
 * Describe: feign拦截器配置
 */
public class OpenfeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //携带请求头信息
        ServletRequestAttributes attributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            requestTemplate.header(Constants.AUTHORIZATION, request.getHeader(Constants.AUTHORIZATION));
        }
    }

}

 springboot启动类配置扫描

@EnableFeignClients注解 

value为第三方接口定义目录

defaultConfiguration为上面定义的全局拦截(可选)

package com.bylz.baseproject;

import com.bylz.framework.openfeign.OpenfeignConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.bylz"})
@EnableFeignClients(value = "com.bylz.baseproject.controller.openfeign", defaultConfiguration = OpenfeignConfig.class)
@SpringBootApplication
public class BaseProjectApplication {
    public static void main(String[] args) {
        SpringApplication.run(BaseProjectApplication.class, args);
    }
}

5.使用测试

在上述@EnableFeignClients注解value配置的包目录下定义第三方调用接口

package com.bylz.baseproject.controller.openfeign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "sysUser", url = "http://localhost:5730")
public interface DemoClient {

    @GetMapping("/user/detailById.json")
    String getUserInfo(@RequestParam("userId") Long userId);

}

备注:@FeignClient注解name属性可自定义不能重复,url为需要调用的服务地址(可配置注册中心的服务名做负载均衡)

controller调用

@Resource
private DemoClient demoClient;



@GetMapping("/feign")
public String feign() {
    return demoClient.getUserInfo(1L);
}

请求接口就能获取上述第三方服务接口返回

更多openfeign接口类型参数传递及接口定义与调用方式可以查看网上资料,这里只做简单演示

你可能感兴趣的:(java,springcloud,微服务,spring,boot,spring,cloud,java,微服务,后端)