Feign SpringCloud components of

Feign is a declarative Web services client. This makes the write Web service clients more convenient to use Feign create an interface and annotate. Having pluggable annotation support, including Feign notes and annotations JAX-RS. Feign also pluggable encoder and decoder. Spring Cloud adds support for Spring MVC annotations, and use the default HttpMessageConverters used in the Spring Web. Spring Cloud integration Ribbon and Eureka to provide load balancing http customers use Feign end

This article will introduce Feign principles and some of the relevant knowledge and how to use in the project

A, Feign principle

1, when starting, the program scans the package will be scanned and all packages in all @FeignClient annotated classes, these classes and injected into the vessel IOC in spring. When defining the interface is invoked in Feign, by generating RequestTemplate JDK dynamic proxy.
2, all of the information contained requestTemplate request, such as request parameters, the request URL, etc.
3, RequestTemplate generating Request, Request to the client process and then, the client's default JDK HttpURLConnection, may be OKhttp, Apache, etc. of HTTPClient
4, Finally client packaged as LoadBaLanceClient, combined with load balancing ribbon initiated calls
1562133752_1_

Second, used in the project

1, set up a registry Eureka

This article does not describe how to build a service Eureka, Eureka understand the learning can go check out this article: Eureka SpringCloud component of

2, set up the target service

a, import dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

b, startup class marked Eureka client notes

/**
 * @author Gjing
 */
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

c, yml configuration file

server:
  port: 8090
spring:
  application:
    name: demo
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

d, write interfaces available to Feign call

/**
 * @author Gjing
 **/
@RestController
public class TestController {

    @GetMapping("/test")
    public Map<String,Object>test() {
        Map<String,Object> map = new HashMap<>(16);
        map.put("code", "ok");
        return map;
    }

    @GetMapping("/test2")
    public String test2(@RequestParam(name = "param1") String param1) {
        return param1;
    }

    @PostMapping("/test3")
    public Integer test3(Integer id) {
        return id;
    }
}

3, set up call service

a, import dependencies

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

b, increase startup class notes

/**
 * @author Gjing
 */
@SpringBootApplication
@EnableFeignUtil
@EnableEurekaClient
public class FeignApplication {

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

}

c, yml configuration

server:
  port: 8083
spring:
  application:
    name: feign-demo
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

d, to create a service, call for service

/**
 * @author Gjing
 **/
@FeignClient(name = "demo")
public interface FeignTestService {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    Map<String,Object>test();

    @RequestMapping(value = "test4", method = RequestMethod.GET)
    String test2(@RequestParam("param1") String param1);

    @RequestMapping(value = "/test3", method = RequestMethod.POST)
    Integer test3(@RequestParam("id") Integer id);
}

e, writing Controller Test access

/**
 * @author Gjing
 **/
@RestController
public class FeignTestController {

    @Resource
    private FeignTestService feignTestService;

    @GetMapping("/test")
    public String test() {
        return feignTestService.test().toString();
    }

    @GetMapping("/test2")
    public ResponseEntity test2() {
        String test2 = feignTestService.test2("你好");
        return ResponseEntity.ok(test2);
    }

    @GetMapping("/test3")
    public ResponseEntity test3() {
        Integer test3 = feignTestService.test3(1);
        return ResponseEntity.ok(test3);
    }
}

4, how Feign rollback process

Feign itself integrated Hystrix, therefore, we directly start classes with the @EnableCircuitBreakercan of Hystrix do not understand can go to this article: Hystrix SpringCloud component of

a, pom increased reliance file, otherwise it will Hystrix error

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

b, increase startup class notes@EnableCircuitBreaker

c, yml file open Hystrix protection

feign:
  hystrix:
    enabled: true

Before d, create a fallback class and implement the interface class Feign

/**
 * @author Gjing
 **/
@Component
public class FeignTestFallbackImpl implements FeignTestService {
    @Override
    public Map<String, Object> test() {
        // TODO: 2019/7/3 这里就实现回退后的处理咯 
        Map<String,Object> map = new HashMap<>(16);
        map.put("code", "回退了");
        return map;
    }
    
    @Override
    public String test2(String param1) {
        return null;
    }

    @Override
    public Integer test3(Integer id) {
        return null;
    }
}

E, feign modify the interface class, the class set backoff

/**
 * @author Gjing
 **/
@FeignClient(name = "demo",fallback = FeignTestFallbackImpl.class)
public interface FeignTestService {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    Map<String,Object> test();

    @RequestMapping(value = "test4", method = RequestMethod.GET)
    String test2(@RequestParam("param1") String param1);

    @RequestMapping(value = "/test3", method = RequestMethod.POST)
    Integer test3(@RequestParam("id") Integer id);
}

f, test call

If a timeout or exception occurs, will be rolled back treatment

1562137157_1_

This concludes the article, if found wrong please correct me Oh, Feign default is to use HttpUrlConnection carried http request, and also supports okHttp httpClient of Kazakhstan, which uses all of you to study, this article will not be introduced, if interested in learning another kinds Feign, can refer to my article: SpringCloud-Feign , Demo source code address: SpringCloud-Demo

Guess you like

Origin yq.aliyun.com/articles/707308