SpringBoot feign use

feign using a three-step

1: Introduction jar

2: Start scanning feign class

3: feign statement

Following the introduction of jar

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

Scan startup class

@EnableFeignClients(basePackages = {"com.feign"})

feign statement

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

import com.ns.common.bean.Response;

@FeignClient(name = "passport",url = "${feign.cunion}")
public interface PassportFegin {

    @RequestMapping(value ="/doctor/patient/searchFuzzy",method = RequestMethod.GET)
    String searchFuzzy(@RequestParam("keyword") String keyword);

    @RequestMapping(value ="/doctor/patient/getById",method = RequestMethod.GET)
    Response<Object> getById(@RequestParam("id") String id);

}

feign header configuration

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.thc.security.util.ThreadLocalUtils;

import feign.RequestInterceptor;

@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> requestTemplate.header("x-access-token", ThreadLocalUtils.getThreadLocalValue("x-access-token"));
    }

}

Profiles

feign:
  c-union: http://xxxxxxxx/project

 The rest is injected in the business layer method to call

Guess you like

Origin www.cnblogs.com/zhuxiansheng/p/11246153.html