preface

Spring Cloud was used in this paper as 2.1.8RELEASE, version=Greenwich.SR3

This article is based on the implementation of eureka-Server and Eureka-client in the previous two articles. reference

  • eureka-server

  • eureka-client

Create the Feign project

1.1 Creating a SpING Boot project: Eureka-Feign

1.2 Adding pom.xml dependencies

<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>
Copy the code

1.3 Application Adds configuration information

spring:
  application:
    name: eureka-feign

server:
  port: 8601

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Copy the code

Added annotations to the EurekaFeignApplication class

package spring.cloud.demo.eurekafeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EurekaFeignApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); }}Copy the code

@enableDiscoveryClient: Use the EnableDiscoveryClient annotation, which is described in eureka-client but not here

@enableFeignClients: Enable the Feign client

1.5 Creating a Service Interface EurekaFeignService

package spring.cloud.demo.eurekafeign.service;

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

/ * * *@auther: maomao
 * @DateT: the 2019-09-17 * /
@FeignClient(value = "eureka-client")
public interface EurekaFeignService {

    @RequestMapping(value = "/info")
    String syaHello(a);
}
Copy the code

@feignClient: Defines the Feign client and calls the remote client. Eureka-client indicates the spring.application.name of the client. Example: http://eureka-client/info

1.6 Creating the EurekaFeignController control class

package spring.cloud.demo.eurekafeign.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekafeign.service.EurekaFeignService;

import javax.annotation.Resource;

/ * * *@auther: maomao
 * @DateT: the 2019-09-17 * /
@RestController
@RequestMapping("/feign")
public class EurekaFeignController {

    @Resource
    private EurekaFeignService eurekaFeignService;

    @RequestMapping("/sayHello")
    public String sayHello(a) {
        return "feign result: "+ eurekaFeignService.syaHello(); }}Copy the code

1.7 Starting the Eureka-Feign service

You can see whether eureka-Feign was successfully registered in the Eureka-Server service registry.

The content in the red box indicates that Eureka-Feign has been started and successfully registered with the Eureka-Server service registry.

Visit http://localhost:8601/feign/sayHello, shows as follows:

Feign’s default load balancing policy is polling. To modify Feign’s load balancing policy, see Eureka-Ribbon configuration

At this point, a simple standalone Feign service consumer project is set up.

eggs

Feign integrates Hystrix circuit breaker

Pom.xml adds dependencies

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

Add the application.yml configuration

feign:
  hystrix:
    enabled: true
Copy the code

Modify EurekaFeignService

Add fallback to the @feignClient annotation

package spring.cloud.demo.eurekafeign.service;

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

/ * * *@auther: maomao
 * @DateT: the 2019-09-17 * /
@FeignClient(value = "eureka-client", fallback = EurekaFeignServiceFailure.class)
public interface EurekaFeignService {

    @RequestMapping(value = "/info")
    String syaHello(a);
}
Copy the code

Increase EurekaFeignServiceFailure class:

package spring.cloud.demo.eurekafeign.service;

import org.springframework.stereotype.Service;

/ * * *@auther: maomao
 * @DateT: the 2019-09-17 * /
@Service
public class EurekaFeignServiceFailure implements EurekaFeignService {
    @Override
    public String syaHello(a) {
        return "Busy network, please try again later."; }}Copy the code

Start the Eureka-Feign service

First of all, in the case of eureka – client all don’t start, go to http://localhost:8601/feign/sayHello to see if all is not normal, then stopped one eureka – client, The refresh many times http://localhost:8601/feign/sayHello, shows as follows:

At this point, Feign integrated Hystrix circuit breaker mechanism is complete.

conclusion

This article is mainly about the simple implementation of feign as a simple application of service consumption and Hystrix integration.

The code address

Making the address


  • Eureka Server Service Registry tutorial for Spring Cloud 2.x
  • Eureka Client Service Provider tutorial for Spring Cloud 2.x
  • Spring Cloud 2.x Ribbon Service Discovery Tutorial (with Integrated Hystrix Fuse)
  • Feign Service Discovery Tutorial with Integrated Hystrix Circuit breaker in Spring Cloud 2.x
  • Spring Cloud 2.x Zuul Routing Gateway Tutorial
  • Spring Cloud 2.x Config Distributed Configuration Center tutorial
  • Spring Cloud 2.x Hystrix Dashboard Circuit breaker tutorial

Please indicate the source of reprint,