Summary of all aspects of getting started with SpringCloud Alibaba components (Part 1): registration center-nacos, load balancing-ribbon, remote calling-feign

Article directory

    • Nacos
    • Ribbon
    • Feign
    • FeignExpansion

Nacos

Concept: Nacos is a new open source project launched by Alibaba. It is a dynamic service discovery, configuration management and service management platform that makes it easier to build cloud native applications. Nacos is committed to helping users discover, configure and manage microservices. It provides a simple and easy-to-use feature set, including dynamic service discovery, service configuration, service metadata and traffic management, helping users build and deliver more agilely and easily. and manage microservices platforms. Nacos is a service infrastructure that builds “service”-centered modern application architecture (such as microservice paradigm, cloud native paradigm).

In terms of positioning, Nacos is an open source Alibaba dynamic service discovery, configuration management and service management platform that makes it easier to build cloud native applications. It provides powerful functions and an easy-to-use feature set to help users quickly implement dynamic service discovery, service configuration, service metadata and traffic management.

In principle, Nacos realizes the registration and discovery of services through the registration center, the dynamic acquisition and update of configurations through the configuration management module, and the metadata management and traffic management of services through the service management module.

In terms of use, Nacos can be integrated with Spring, Spring Boot, Spring Cloud and other frameworks, and can replace Spring Cloud Eureka and Spring Cloud Config.
To use the registry function in Nacos, you need to follow the following steps:

  1. Add Nacos related dependencies in the project’s pom.xml file.
  2. Configure the relevant parameters of the Nacos registration center in the project’s application.yml or application.properties file, such as the Nacos address, port, user name, password, etc.
  3. Add the @EnableDiscoveryClient annotation in the main program of the project to enable service registration and discovery functions.
  4. Add @Service and @Autowired annotations to the project’s service class to declare services and inject dependencies.
  5. Add @RibbonClient and @FeignClient annotations to the project’s client class to declare the client and call the service.

Ribbon

Concept: Ribbon is an open source project based on Netflix. It is a client-side load balancing tool. Ribbon provides complete configurations, such as timeout, retry, etc., and supports multiple load balancing algorithms, such as polling, random, least connection algorithm, etc.

Conceptually, load balancing refers to distributing client requests to multiple servers to achieve high availability and performance. Ribbon acts as a load balancer, sending requests to Ribbon through the client, and then Ribbon selects a server based on the load balancing algorithm and forwards the request to the server. If that server cannot respond to the request, Ribbon automatically selects another server and forwards the request to it.

In terms of positioning, Ribbon is part of the Spring Cloud ecosystem and is mainly used for client load balancing.

In principle, Ribbon works on a weight-based polling algorithm. It assigns a weight value to all servers and determines the probability of each server being selected based on the weight value. For example, if the weight value of server A is 2 and the weight value of server B is 1, then during the polling process, the probability of server A being selected is 2/3 and the probability of server B being selected is 1/3. In addition, Ribbon also supports a variety of load balancing algorithms, such as random algorithm, least connection algorithm, IP hash algorithm, etc. These algorithms can select appropriate load balancing strategies according to different scenarios to improve system performance and availability.

In terms of use, Ribbon can be integrated with Spring Cloud. By properly configuring weights and selecting appropriate load balancing algorithms, it can help us achieve high availability and high performance distributed systems. At the same time, Ribbon also supports customized load balancing strategies, and users can implement customized load balancing algorithms according to their own needs.
To use Ribbon to implement load balancing in Spring Cloud, you need to follow the following steps:

  1. Add Spring Cloud and Ribbon related dependencies in the project’s pom.xml file.
  2. Configure Ribbon-related parameters in the project’s application.yml or application.properties file, such as Ribbon’s server list, polling interval, etc.
  3. Add the @EnableDiscoveryClient annotation to the main program of the project to enable the Nacos client function.
  4. Add @Service and @Autowired annotations to the project’s service class to declare services and inject dependencies.
  5. Add the @FeignClient annotation in the project’s client class to declare the client and call the service.

Feign

Concept: Feign is a declarative Web Service client that makes writing HTTP clients easier. Feign’s slogan is “Feign makes Java HTTP clients dead simple”. Its core annotations and method minimalism and the use of pluggable encoders and decoders make writing HTTP clients easier.

Conceptually, Feign is a lightweight RESTful HTTP service client. It calls HTTP requests in the form of Java interface annotations, similar to Dubbo. The service consumer gets the service provider’s interface and then calls the local interface. Call the same method, but what is actually issued is a remote request.

In terms of positioning, Feign is part of the Spring Cloud ecosystem and is mainly used for remote invocation of services.

In principle, Feign maps interface methods to HTTP requests through dynamic proxy, then sends the request through the HTTP client, and finally obtains the response result. When using Feign to make remote calls, we need to define an interface that defines the methods that need to be called. Methods in the interface can use annotations to specify the requested URL, request method, request parameters and other information. For example, we can use the @FeignClient annotation to specify the service name and service address that need to be called.

In terms of use, Feign can be used by introducing relevant dependency packages. In terms of configuration, remote calling can be achieved by adding annotations and writing Feign client code. At the same time, you can also customize the configuration and optimize the use of Feign according to actual needs.
To use Feign to implement remote calling in Spring Cloud, you need to follow the following steps:

  1. Add Spring Cloud and Feign related dependencies in the project’s pom.xml file.
  2. Configure Feign-related parameters in the project’s application.yml or application.properties file, such as Feign’s client name, timeout, etc.
  3. Add the @EnableFeignClients annotation in the main program of the project to enable the Feign client function.
  4. Add the @FeignClient annotation to the project’s service interface to declare the service interface and specify the URL of the remote service.
  5. Inject the service interface into the client class of the project and call the remote service method through the service interface

Feign expansion

1>The name in @FeignClient is the service name registered by the service provider on nacos, otherwise an error will be reported

Load balancer does not have available server for client:xxxx-service

2>@GetMapping(“/products/{pid}”) Specifies the interface path, which must be the same as the interface url provided by the service provider, otherwise an error will be reported

feign.FeignException$NotFound: [404] during [GET] to [http://xxx-service/xxx]

3> Define interface parameters: If you use parameter path access, you need to use @PathVariable(“pid”) to explicitly specify the path parameters, otherwise an error will be reported

feign.FeignException$NotFound: [404] during [GET] to [http://xxx-service/xxx]

4>Define interface parameters: If accessed in the normal way, the parameters need to be marked with @RequestParam, otherwise an error will be reported

feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://xxxx-service/xxxx?xxx=1]

5>Define interface parameters: If it is an object parameter, the parameter needs to be marked with @RequestBody (note that both the fegin interface and the controller interface are required), otherwise an error will be reported

Parameters cannot be obtained

6>Define interface parameters: If it is an object, you can use @SpringQueryMap to replace the @RequestBody above

7>Define interface parameters: If you need to upload files, you need to use the @RequestPart annotation tag

8>The default connection time for Feign interface call is 1s. If the computer is slow, you can configure a longer time during development.

Note: When learning sentinel later, do not configure it, as it will affect the observation effect.

feign:
  client:
    config:
      default:
        connectTimeout: 5000 #Connection time in milliseconds
        readTimeout: 5000 #Operation time