SoFunction
Updated on 2025-03-03

feign name specified service call invalid problem and solution

background

When making three-party external interfaces, a very strange problem occurred.

A feign call must be usedurl Only by specifying the path can you access it, because it is clearly usednameThe attribute can specify the service.

This is very strange~~~~

@FeignClient(name = "outerService", url = "${}",configuration = , fallbackFactory = )
// @FeignClient(value = "outerService",configuration = , fallbackFactory = , decode404 = true)
@Service
public interface outerServiceClientFeign extends BafooCommonApi {

}

Increased knowledge

When declaring the interface, it can be used after injecting the container through @Resource in the code.

Common properties of @FeignClient annotation are as follows:

  • value/name: The value and name function are the same, and are used to specify the name of FeignClient; if there is no url configured and the project uses Eureka, nacos, or ribbon, the name attribute will be used as the name of the microservice for service discovery. Otherwise, it's just a name. This property corresponds to .
  • url: Generally used for debugging, the function is to specify the API address called by @FeignClient, rather than obtain it from the service center.
  • urlandnamePlaceholders can be used, such as: @FeignClient(name = "",url="${}");
  • decode404: When an http 404 error occurs, if the field is true, decoder will be called for decoding; otherwise, a FeignException will be thrown.
  • configuration: Feign configuration class, which is used to customize Feign's Encoder, Decoder, LogLevel, and Contract.
  • fallback: Define fault-tolerant processing class. When the remote interface is called fails or timed out, the fault-tolerant logic of the corresponding interface will be called. The class specified by fallback must implement the interface marked by @FeignClient.
  • fallbackFactory: Factory class, used to generate fallback class instances, implement common fault-tolerant logic for each interface, and reduce duplicate code.
  • path: Defines the unified prefix of the current FeignClient.
  • contextId: Set a separate timeout for an interface, corresponding to the attributes in config.

reason

The configuration file of the third-party service has been added with the prefix path

 servlet:
    context-path: /outer

This results in that when feign is called, the spliced ​​path is missing one prefix/outer

Solution

usepathProperties, specify the prefix.

@FeignClient(value = "outerService",path = "/outer",configuration = , fallbackFactory = , decode404 = true)

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.