spring-boot framework dubbo annotation @Reference injection service, service is null when calling method

Service consumers:

    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION)
    UserService userService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION)
    TenantService tenantService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION)
    private UserPermissionService userPermissionService;

When using these objects, directly report the null pointer exception:

   public UserDTO getUserDTOByClientId(String clientId) {
        log.info("userservice======"+userService);
        return userService.getUserInfo(clientId);
    }

Confirmed-the print object is null.
Analysis:
Due to conflicts in operation and maintenance scripts, each time you start, you need to start the service consumer first, and then start the service provider. Viewing the log zk can notify the registration information of the service consumer provider, but the object cannot be obtained when calling-NullpointException.

solution

1. Lazy loading:


    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,lazy = true)
    UserService userService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,lazy = true)
    TenantService tenantService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,lazy = true)
    private UserPermissionService userPermissionService;

2. Start the service provider first, and then start the service consumer.
3. According to the information, there is a check parameter set in the dubbo annotation settings check=false. When the service provider has not started, the service consumer will create a corresponding proxy object when the object is initialized, and will not generate a null object. . This scheme is relatively suitable and recommended.


    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,check = false)
    UserService userService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,check = false)
    TenantService tenantService;
    @Reference(version = DubboConfig.INNOVATION_SERVICE_USER_VERSION,check = false)
    private UserPermissionService userPermissionService;

For details, please refer to: http://dubbo.apache.org/zh-cn/docs/user/demos/preflight-check.html

Insert picture description here

Published 41 original articles · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Yunwei_Zheng/article/details/104018337