Microservice - call of service

typora-copy-images-to: imgs

Call between microservices

1. Call of microservice - Ribbon + resttemplate (independent component)

1. What makes Ribbon

Ribbon is a client-side load balancer, which is used to call the load balancing of microservices between spring cloud

2. Load balancing between server and client

[failed to transfer the pictures in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-vZlGLVD5-1593147103566)(imgs/1592359063328.png))

Load balancing method

1. Client load balancing

2. Load balancing on the server

Network layer 4 load

Network layer 7 load -- nginx

2. Use of bbbbon + resttemplate

1. Create an eureka service center, create a student service and class service, and let the student service call the class service

[failed to transfer the pictures in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-lKB8omo9-1593147103568)(imgs/1592359230273.png))

2. Add a dependency on the active caller (student service calls class service, and student service is the active caller)

        <!--Microservice called ribbon Dependence on-->

3. Declare the Restemplate component in the caller's startup class

    @LoadBalanced //load balancing 
    public RestTemplate restTemplate(){
        return new RestTemplate();

4. Write call code at the caller

public class StudentController {
    private RestTemplate restTemplate;
    public String findStudent( @RequestParam("sid")Integer sid){
    String className =  restTemplate.
//            According to the id, query which class the students belong to
//       Spring class is the name of class micro service
        switch (sid){
            case 1:
                return "Xiao Zhang"+"class:"+className;
            case 2:
                return "Xiao Feng"+"class:"+className;
            case 3:
                return "Small directors"+"class:"+className;
                return "No one";

5. Load balancing of ribbon

How does eureka know whether two services are clustered or independent
 The same name of microservice is cluster relationship
 The microservice name is the name of the global configuration file spring.application.name Value of

6. Configuration of ribbon's load balancing strategy

IRule interface represents load balancing strategy
    public IRule iRule(){
//        RandomRule is a sub implementation class of IRule, which is a load balancing strategy
        return new RandomRule();

3. Service call - feign (in fact, the bottom layer is the Ribbon)

1. Why do we need Feign when we have Ribbon?

The bottom layer of feign also calls the Ribbon. Feign is essentially a collection of the bbon + hystrix (porcupine). Because the writing method of Ribbon itself is not object-oriented, many java programmers are not accustomed to this writing method

2. Use of feign

2.1 add a dependency on the caller (create a teacher's microservice and let the class microservice call the teacher's microservice)

        <!--add to feign rely on-->

2.2 define a feign interface

//The name of the microservice is used in the annotation
public interface ITeacherFeign {
//    Declare the method and request path, and the method name can be started at will, but for the convenience of later management, use the method name to be called
    @RequestMapping(value = "findTeaById",method = RequestMethod.GET)
     String getTeacherName( @RequestParam("tid")Integer tid);

2.3. Inject where the microservice needs to be called and write code

public class ClassControler {
    private ITeacherFeign iTeacherFeign;
    public String findClass( @RequestParam("cid")Integer cid){
        String s = iTeacherFeign.getTeacherName(cid);
        switch (cid){
            case 1:
                return "java1910"+s;
            case 2:
                return "java1911"+s;
            case 3:
                return "java1912"+s;
                return "There is no such class";

4. Feign timeout and retry

The default timeout of Feign is 1s. If the callee does not return a result within 1s, it will fail by default, and then try again. Sometimes, without intervention, it may cause repeated sending of the request, leading to various problems

Note: Feign's own timeout and retry functions are turned off by default, so the timeout and retry effects we see are provided by the underlying Ribbon. If Feign's timeout retry is turned on, the settings of the Ribbon will be overwritten

4.1 configure timeout and retry of Ribbon


eureka.client.service-url.defaultZone: http://localhost:20000/eureka
#Configure connection timeout
#Configure read timeout
#Maximum retries of the same instance, excluding the first call
##Retry the maximum number of retries for other instances of load balancing, excluding the first call
# So the number of retries is equal to (MaxAutoRetries+1)*(MaxAutoRetriesNextServer+1)

# Configure feign timeout. Once feign timeout is configured, the timeout and retry of ribbon will fail. It can configure different timeout for different microservices

#Configure the connection timeout of spring teacher microservice
#Configure read timeout

Keywords: Spring network Nginx Java

Added by PcGeniusProductions on Fri, 26 Jun 2020 09:14:44 +0300