解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for service x.x.x on consumer
- 背景
- 问题分析
- 问题解决
背景
今天接收一个项目,实在原有代码基础上增加一些功能,项目采用的架构是springboot+dubbo+mybatisPlus+zookeeper,按照原有程序结构,我试着谢了一个功能,结果在接口测试的时候报了500错误。
错误内容为:
No provider available from registry 127.0.0.1:2181 for service io.platform.jinyu.service.client.api.BizLiveServiceClient:1.0.0 on consumer 172.16.1.161 use dubbo version 2.6.2, please check status of providers(disabled, not registered or in blacklist).
如下图:
问题分析
错误的意思很明显就是在服务消费端的功能中调用服务提供端的逻辑时没有发现服务提供者对象。主要就是服务提供方代码的问题了,原服务提供放服务实现代码如下:
package io.platform.jinyu.service.client.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.platform.base.common.BaseServiceClient;
import io.platform.base.common.utils.ExecuteResult;
import io.platform.jinyu.biz.entity.BizLive;
import io.platform.jinyu.biz.service.BizLiveService;
import io.platform.jinyu.dto.BizLiveDto;
import io.platform.jinyu.service.client.api.BizLiveServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @description:
* @author: zheng
* @date: Created in 2021/2/22 16:27
* @version: 0.0.1
* @modified By:
*/
@Component
@Service()
public class BizLiveServiceClientImpl extends BaseServiceClient<BizLive, BizLiveDto> implements BizLiveServiceClient {
private Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private BizLiveService bizLiveService;
@Override
public BizLiveDto queryObject(Long id) {
BizLive bizLive = this.bizLiveService.selectById(id);
BizLiveDto bizLiveDto = new BizLiveDto();
BeanUtils.copyProperties(bizLive, bizLiveDto);
return bizLiveDto;
}
@Override
public List<BizLiveDto> queryList(Map<String, Object> map) {
List<BizLive> bizLiveList = this.bizLiveService.selectByMap(map);
return convertList(bizLiveList, BizLiveDto.class);
}
@Override
public int queryTotal(Map<String, Object> map) {
EntityWrapper<BizLive> ew = new EntityWrapper<>();
ew.allEq(map);
return this.bizLiveService.selectCount(ew);
}
}
仔细观察上面的代码发现其中的类注解@Service所在的包为org.springframework.stereotype.Service,而用于标注对外暴露dubbo接口的@Service注解所在包为com.alibaba.dubbo.config.annotation.Service。
问题解决
通过修正@Service注解的包就解决了这个问题。
@Service注解在spring和dubbo中都有,很容易用错,因此在使用的过程中一定要注意,避免发生以上错误。
com.alibaba.dubbo.config.annotation.Service:用于标注对外暴露的dubbo接口实现类。
org.springframework.stereotype.Service:用于标注根据业务块分离的Service的实现类,对应的是业务层(如一个dubbo方法可能调用多个业务块的service,这些service的实现类就用Spring的注解)。
修改后重新启动provider服务,再次接口测试一切正常,如下图:
更多推荐
解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for servi
发布评论