Added @LoadBalanced cannot use ip to call remote interface

problem background

I wrote a timed task at work before, in which I needed to call the data of another module, so I used RestTemplate to call, I configured the address of that module in the configuration file (using the microservice name in the registry) ), and injected into the place of use through @Value. There was no problem at first, but when my colleague was recording the log, he happened to be using the same module. He also wrote the address of that module in the configuration file, but it was in the form of ip, just right, he saw me like this Write, just imitate me to use the microservice name to call, he found that the call was unsuccessful, so he changed to the method of directly creating a new RestTemplate in the code, but the url address in the configuration file was changed for me to use ip form. When I tested the function today, I found that my scheduled task could not run all the time. When I saw an error, it said that there was no such load balancing instance. After a period of thinking and discovery, I saw that the configuration in the configuration file was changed for me, that is, the original microservice name was changed to ip for me. So I reported an error, and after my own thinking, I found that the @LoadBalanced annotation could not be called using ip. So find the cause of the problem, and it is easy to solve it later.

@LoadBalanced annotation description

When developing microservice applications, it is inevitable to make mutual calls between services. Generally, we will use RestTemplate to make remote calls. But there is a problem that needs to be paid attention to when putting RestTemplate into the Spring container, as follows

@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
}

We use the @LoadBalanced annotation to indicate that a load balancing strategy needs to be used when using remote calls. We know that load balancing is actually used in the same service and in the presence of a cluster. Therefore, in development, we need to pay attention that if the @LoadBalanced annotation is used, when using the getForObject() or exchange() methods provided by RestTemplate, the url cannot be the ip address, but must be the microservice name in the registry .
why? Because when load balancing is called, there is more than one service in the registry that provides this interface. When the ribbon is encapsulated, it can only be called with the service name in the registry, so as to provide the load balancing effect.

Therefore, when using RestTemplate, in some scenarios, we simply use ip to call (such as calling third-party interface services), and RestTemplate cannot be annotated with @LoadBanlanced at this time . Another way is to directly create a new RestTemplate object where RestTemplate needs to be used. as follows

public void test(){
    
    
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.exchange("接口地址",xxx,xxx,xxx);//这里的接口地址就可以直接放协议+ip+端口,原理与上面一样
}

Guess you like

Origin blog.csdn.net/qq_41570752/article/details/113647958