Typical Project Case 3——Fegin calls 404

1: Background introduction

Arpro has deployed two servers for production load. Both use the same nacos namespace.
Only one was updated. As a result, the request went to the one that was not updated, so a 404 problem occurred.
insert image description here

2. Basic introduction of OpenFeign

OpenFeign is Spring Cloud's secondary encapsulation of Feign, which supports Spring MVC annotations on the basis of Feign. OpenFeign's @FeignClient can parse Spring MVC's @RequestMapping @GetMapping and @PostMapping and so on.

how to use

  1. Add @EnableFeignClients to the startup class, scan the interface annotated with @FeignClient and register it in the spring container.
    2. To use, we only need to create an interface and add the @FeignClient annotation to the interface. This annotation will analyze the interface under the @RequestMapping annotation, and generate an implementation class through a dynamic proxy to achieve load balancing and service invocation.

Environmental preparation

insert image description here

Prepare a Spring Cloud project, including a consumer, a service provider, using nacos as a service discovery and configuration center

insert image description here

project structure

provider

bootstrap.yml
insert image description here

startup class

@EnableDiscoveryClient
@SpringBootApplication
public class TestFeignApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(TestFeignApplication.class, args);
    }
}

UserController class

@RestController
@RequestMapping("/user")
public class UserController {
    
    
    @GetMapping("/get")
    public String findAll() {
    
    
        return "服务调用成功";
    }

}

Invoker

bootstrap.yml

insert image description here

startup class

@SpringBootApplication
@EnableDiscoveryClient
//扫描标有@FeignClient的接口
@EnableFeignClients("com.ctsi.sddx.feign")
public class NacosApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(NacosApplication.class, args);
    }

}

Feign interface

@FeignClient(value = "arpro-provider-one")//value为Feign调用的服务名,也是注册到nacos中的服务名
public interface UserFeign {
    
    

    @GetMapping("user/get")
    String findAll();
}

UserController class

@RestController
@RequestMapping("/user")
public class UserController {
    
    


    @Resource
    private UserFeign userFegin;

    @PostMapping("feign")
    public String getDeviceListByPage() {
    
    
        return userFegin.findAll();
    }
    
}

run project

Start the Invoker service and Provider service, and observe whether the services are registered successfully on nacos.
Under the arpro namespace, we can see that both services have been registered successfully.
insert image description here
The Invoker service calls the findAll() method in the Provider service,
and it can be seen that the OpenFeign call is successful. This is the most basic application of OpenFeign, isn't it very simple.
insert image description here

Three: The reproduction of feign calling 404

After we are familiar with the basic use of OpenFeign, we return to the topic of arpro production and deploy two servers for load. Both use the same nacos namespace.
Only one was updated. As a result, the request went to the one that was not updated, so a 404 problem occurred.

To reproduce the problem, first we need two sets of applications. I have prepared two programs, test-feign-2 and test-feign-5, which are completely consistent in code and inconsistent in service port number.
First, we need to register both sets of applications on nacos. Then there will be a total of two consumer services and two provider services.

You can see that they have been registered so far, two consumer instances, and two provider instances.
insert image description here

Make OpenFeign calls

If a consumer we start calls OpenFeign, it will be load balanced by nacos and distributed according to the weight, that is, both Providers may be called. If the code version of one of the providers is behind the other provider (for example, there is no corresponding interface), then it will appear, and if the provider whose code is behind is requested, a 404 problem will appear. Now let's simulate.

First look at the weights of the two provider services. You can see that the weights of the services are both 1. In this case, it is basically called on average. If the calls are made 4 times, the two providers are called twice, and there is a sequence relationship. Call the first One, then the second, and the first, and the second.

insert image description here

Modify the code of UserController whose port number is 9009

Comment out the findAll() method ; and restart the service

@RestController
@RequestMapping("/user")
public class UserController {
    
    
//    @GetMapping("/get")
//    public String findAll() {
    
    
//        return "服务调用成功";
//    }

}

carry out testing

The consumer makes an OpenFeign call.
The first successful call is the provider service with the correct code.
insert image description here
The second call fails, and the provider service with the code commented out is used.

404 problem successfully reproduced
insert image description here

Four: Summary

  1. You need to be in awe of the production environment
  2. For the production of codes in the two environments, a strict process system is required to ensure
  3. Feedback closed loop is required, such as test verification.
    Technical means:
  4. Since the codes of the two environments are consistent, can they be built uniformly and then pushed to their respective servers. This needs to be done in the next step, using automation to improve fault tolerance, such as using jenkins (unified construction, and push to the respective servers)
  5. For the use of OpenFeign, this blog only mentions simple applications, such as OpenFeign's timeout processing fuse log , which is not mentioned.

Five: Sublimation

  1. Production is no small matter, and one needs to be in awe of the production environment.
  2. We can have a variety of ways to deal with the problem, from the perspective of process system and technical means.
  3. What I have always learned in blogging is that completion is more important than perfection. Complete the established tasks within the specified time, and expand when there is spare time.

Guess you like

Origin blog.csdn.net/wangwei021933/article/details/129582038