Spring Cloud Feigin (Load Balancing)

The service consumers mentioned above use RestTemplate+ Ribbon (to achieve load balancing). Currently, there are two ways to invoke restful services in Spring Cloud

  • RestTemplate +ribbon restTemplate defines 36 methods for interacting with REST resources, most of which correspond to HTTP methods. There are only 11 separate methods, ten of which are overloaded in three ways, and the eleventh one is overloaded six times, making a total of 36 methods.
  1. delete()Perform an HTTP DELETE operation on a resource at a specific URL
  2. exchange()A specific HTTP method is executed on the URL to return the ResponseEntity containing the object mapped from the response body
  3. execute()Executes a specific HTTP method on the URL that returns an object mapped from the response body
  4. getForEntity()Send an HTTPGET request that returns the ResponseEntity containing the object mapped to the response body
  5. getForObject()Send an HTTP GET request, and the returned request body is mapped to an object
  6. postForEntity()POST data to a URL that returns a ResponseEntity containing an object mapped from the response body
  7. postForObject()POST data to a URL that returns an object based on the response body match
  8. headForHeaders()Send an HTTP HEAD request that returns an HTTP header containing the URL of a particular resource
  9. optionsForAllow()Send an HTTP OPTIONS request that returns the Allow header for a specific URL
  10. postForLocation()POST data to a URL that returns the URL of the newly created resource
  11. put()PUT a resource to a specific URL
  • Feign (default integration with ribbon, Hystrix (Fuse – more on that later))

    Feign is adeclarativePseudo-http client, which makes writing Http clients much easier. With Feign, you just create an interface and annotate it, and it hasPluggable annotation feature, you can use Feign annotations andJAX - RS annotationsTo Feign supportpluggableEncoders and decoders,Feign integrates the Ribbon by defaultandEureka combination.Load balancing is implemented by defaultThe effect.

    Jax-ws and JAX-RS are different styles of SOA architectures.

    1.JAX-WS: The Java API for XML Web Services specification is a set of Java apis for XML Web Services that allow developers to choose rPC-oriented or message-oriented to implement their own Web Services. Centered on the verb, it specifies that each time the function is executed (RPC).

    2.JAX-RSJava API for RESTful Web Services, noun-centric, refers to a resource every time it is executed (REST).

Feign features

  • Pluggable annotation support (a new feature of servlet3), including Feign annotations and jax-rs annotations
  • Supports pluggable HTTP encoders and decoders
  • Support for Hystrix and its Fallback(which can be customized if the service is down) to avoid consuming servlet container thread resources.
  • Support for Ribbon load balancing
  • Supports compression of HTTP requests and responses

Feign can do what the Ribbon and Hystrix do. However, in order to use the Ribbon and Hystrix annotations, you must import the corresponding JAR packages. Feign also provides HTTP request templates. You can define HTTP request parameters, format, address, and so on

Add the dependent

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

Open the Feign

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {

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

The create interface (the HTTP request template provided by Feign) specifies which service to invoke via @feignClient (” service name “)

@FeignClient("eureka-provider")
public interface  HomeClient {

    @GetMapping("/") // Service provider "/" Method of access
    String consumer(a);
}
Copy the code

Consumption method

// No explanation
@RestController
public class ConsumerController {

    @Autowired
    private HomeClient homeClient;

    @GetMapping(value = "/hello")
    public String hello(a) {
        returnhomeClient.consumer(); }}Copy the code

Consumption configuration

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: feign-consumer

server:
  port: 9000
Copy the code

conclusion

There is a full deployment of Spring Cloud on Github. Other related articles Spring Cloud (1)- Introduction and options Spring Cloud (2)- Service Discovery (Eureka, Consul) Spring Cloud (3)- Load Balancing (Feign, Ribbon Spring Cloud (4)- Hystrix Spring Cloud (5)- Zuul Spring Cloud (6)- Configuration Management and Refresh (Config, Bus) Give a star ~ personal blog ~ Jane book ~