DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Marry MDC With Spring Integration
  • Integrate Spring With Open AI
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Trending

  • AI-Powered CI Test Failure Triage
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Guess the Speed: Building a Family Internet Speed Guessing Game

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Spring Cloud Alibaba Sentinel's Integration With Feign

Spring Cloud Alibaba Sentinel's Integration With Feign

Let's explore Spring Cloud Alibaba Sentinel's integration with Feign.

By 
Andy Shi
·
Jul. 10, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in


@FeignClient(name = "service-provider")
public interface EchoService {
  @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
  String echo(@PathVariable("str") String str);
}

What Is Sentinel

Sentinel is an open source circuit breaker. It can be part of Spring Cloud Alibaba or as an individual package.

What Is Feign

Feign is a Java to HTTP client binder. It’s aiming to simplify the REST API process.

Feign emphasizes on the definition of the interfaces. There is 1:1 mapping between the methods in an interface to a REST API. This is vastly different from HttpClient or OkHttp.

How Does Feign work

First, we need to use @EnableFeignClients to turn on the features

@SpringBootApplication
@EnableFeignClients 
public class MyApplication {
  ...
}

Then we use @FeignClient to generate a proxy class

Once that’s done, it will be injected into ApplicationContext and can be auto wired to use.

How to Use Feign Client

Before that let’s dig deeper of the proxy class by looking at the sequence diagram of registering a Feign client:

Image title

Here is some explanation of the chart:

 @FeignClient  annotated interface will be translated into a  FactoryBean named  FeignClientFactoryBean, within which the  getObject()  method will eventually return a Proxy.

During the generation of Proxy, it has to utilize the information from  org.springframework.cloud.openfeign.Targeter  interface. It will also invoke  feign.Feign.Builder  to build an instance of feign.Feign.

Once the feign.Feign instance is created, the  newInstance() method will return a Proxy.

Let’s keep looking at the code on the newInstance() part:

public <T> T newInstance(Target<T> target) {
    Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
    Map<Method, MethodHandler> methodToHandler = new LinkedHashMap<Method, MethodHandler>();
    List<DefaultMethodHandler> defaultMethodHandlers = new LinkedList<DefaultMethodHandler>();

    for (Method method : target.type().getMethods()) {
      if (method.getDeclaringClass() == Object.class) {
        continue;
      } else if(Util.isDefault(method)) {
        DefaultMethodHandler handler = new DefaultMethodHandler(method);
        defaultMethodHandlers.add(handler);
        methodToHandler.put(method, handler);
      } else {
        methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method)));
      }
    }

    InvocationHandler handler = factory.create(target, methodToHandler);

    T proxy = (T) Proxy.newProxyInstance(target.type().getClassLoader(), new Class<?>[]{target.type()}, handler);

    for(DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) {
      defaultMethodHandler.bindTo(proxy);
    }
    return proxy;
  }

In this part of the code, InvocationHandlerFactory is the most important piece to remember.

Integrate Sentinel With Feign

A word of caution is that there are package-level interfaces. These interfaces are not accessible from outside the package, which means we shall not change that.

If you look carefully at the sequence diagram, you might notice Hystrix in the process. Yes, Hystrix and Feign are closely tied. Sentinel plays a similar role as Hystrix. One major difference is it’s still under active development.

spring-cloud-starter-openfeign has a dependency on feign-hystrix. As a result, the default  InvocationHandlerFactory  in the previous code snippet will return a HystrixInvocationHandler. And that’s where we are changing. We implemented SentinelInvocationHandler to swap out the Hystrix logic and inject Sentinel’s version.

Here is an example with Sentinel and Feign:

@FeignClient(name = "test-service")
public interface TestService {
  @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
  String echo(@PathVariable("str") String str);

  @RequestMapping(value = "/divide", method = RequestMethod.GET)
  String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
}

Users won't notice the changes behind the scene.

Summary

Feign provides flexibility to RESTful APIs in Spring Cloud, and it’s getting popular. As a good RESTful solution, it also provides circuit breaker integration. Sentinel is now integrated with Feign and can provide the same functionalities as Hystrix does.

Spring Cloud Integration Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • How to Marry MDC With Spring Integration
  • Integrate Spring With Open AI
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook