Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Inter-service invocation is an essential part of microservice system. In the official website of SpringCloud Alibaba, dubbo is recommended to be used. However, due to the small concurrency and relatively complex dubbo, all the building owners still choose OpenFeign in the selection of inter-service invocation. At the same time, the interface of interservice invocation is encapsulated as a sub-project for unified API management. Avoid friends building duplicate wheels.

1. Create an API subproject

1. Build sub-projects

A new subproject has been created with the following file structure.

2. Add the pom. XML

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> < version > 2.2.6. RELEASE < / version > < / dependency >Copy the code

3. Implement fallback fusing method

This class is jumped when the service invocation fails. This article will be used in conjunction with Sentinel.

@Component public class UserFallBack implements UserApi { @Override public Result<UserVO> getCurrentUser() { Result<UserVO> result = new Result<>(); String error = "Failed to call system to get current user information!" ; result.setCode(506); result.setMessage(error); return result; } @Override public Result<UserVO> getUser(UserVO UserVO) { Result<UserVO> result = new Result<>(); String error = "Failed to call system to get input user information!" ; result.setCode(506); result.setMessage(error); return result; }}Copy the code

4. Feign interface implementation

@FeignClient(name = "system", Fallback = userfallback.class) // Where name is the service name in nacOS public interface UserApi {/** * get the current user information ** @return */ @GetMapping("/getCurrentUser") Result<UserVO> getCurrentUser(); /** * getUser information from entity ** @return */ @getmapping ("/getUser") Result<UserVO> getUser(@springquerymap UserVO UserVO); }Copy the code

Note here that Feign will convert get to POST if there is a value in the BOBY at the time of a GET request, so the @SpringQueryMap annotation is required in this case.

2. Call the parent project

1. Modified pom. XML

1. Introduce subprojects into POM files. For details, please refer to the main article of this article:

    <dependencies>
        <dependency>
            <groupId>com.vanpeng</groupId>
            <artifactId>common-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
Copy the code

2. Modify the startup class

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients("com.common.api")
public class DatacenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(DatacenterApplication.class, args);
    }

}
Copy the code

Note: @enableFeignClients (“com.common.api”) must specify the API subproject path or the call will not be executed.

3. Call

public class DemoController { @Autowired UserApi userApi; @GetMapping("/demo") public void demo() { Result<RestSysUserVO> xxx = userApi.getCurrentUser(); }}Copy the code