背景,2022.4.13,当前dubbo-spring-boot-starter版本是3.0.7, dubbo-api-docs-core版本在2.7.8.3
由于dubbo默认采用dubbo协议非http方式调用,项目中不利于把接口元数据信息暴露并呈现在类swagger的文档管理界面上,当前springboot项目中有效的方式是在服务端maven上引用dubbo-api-docs-core和dubbo-api-docs-annotations的来暴露元数据 ,在dubbo-admin中进行查看. 基于目前dubbo3.x与dubbo-api-docs-core2.7.8.3版本有冲突,dubbo-api-docs-core内部依赖dubbo2.7.8首先要解决依赖冲突.
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-api-docs-annotations</artifactId>
<version>2.7.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-api-docs-core</artifactId>
<version>2.7.8.3</version>
<exclusions>
<exclusion>
<artifactId>dubbo</artifactId>
<groupId>org.apache.dubbo</groupId>
</exclusion>
</exclusions>
</dependency>
同样如果注册中心采用nacos,也要解决依赖冲突
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
服务端添加注解
@ApiModule(value = "测试接口服务", apiInterface = UserService.class)
@DubboService(version = "1.0")
public class UserServiceImpl implements UserService {
@ApiDoc(value = "测试查询接口", description = "通过id查用户", responseClassDescription = "A quick star response bean")
@Override
public SysUserDto getUserById(String userId) {
SysUserEntity entity = sysUserDataService.getById(userId);
SysUserDto result = BeanHelper.copyProperties(entity, SysUserDto::new);
return result;
}
}
服务端application.yml或bootstrap.yml中添加配置
dubbo:
registry:
address: nacos://localhost:8848
protocol:
name: dubbo
port: 20881
consumer:
check: false
retries: 1
timeout: 10000
metadata-report:
address: nacos://localhost:8848
在启动时报错提示,如下
Description:
Field application in org.apache.dubbo.apidocs.core.DubboApiDocsAnnotationScanner required a bean of type 'org.apache.dubbo.config.ApplicationConfig' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.apache.dubbo.config.ApplicationConfig' in your configuration.
分析原因是DubboApiDocsAnnotationScanner这个类需要依赖ApplicationConfig ,RegistryConfig,ProtocolConfig 注入的bean,但是dubbo3在前面未在beanFactory中创建这3个bean对象。
@Import({DubboDocProviderImpl.class})
public class DubboApiDocsAnnotationScanner implements ApplicationListener<ApplicationReadyEvent> {
...
@Autowired
private ApplicationConfig application;
@Autowired
private RegistryConfig registry;
@Autowired
private ProtocolConfig protocol;
private ParamBean processHtmlType(Class<?> classType, RequestParam annotation, ParamBean param) {
....
private <I, T> void exportDubboService(Class<I> serviceClass, T serviceImplInstance, boolean async) {
ServiceConfig<T> service = new ServiceConfig<>();
service.setApplication(application); #这里有用
service.setRegistry(registry); #这里有用
service.setProtocol(protocol); #这里有用
service.setInterface(serviceClass);
service.setRef(serviceImplInstance);
service.setAsync(async);
service.export(); #内部
}
因此只需要添加配置类启用文档并注入bean.
@Profile("dev")
@EnableDubboApiDocs
@Configuration
public class DubboDocConfig {
@ConditionalOnMissingBean
@Bean
ApplicationConfig application() {
return new ApplicationConfig();
}
@Bean
@ConditionalOnMissingBean
RegistryConfig registry() {
return new RegistryConfig();
}
@Bean
@ConditionalOnMissingBean
ProtocolConfig protocol() {
return new ProtocolConfig();
}
}
再启动,不报错,完成。启动duboo-admin.接口列表出来了
点此参考完整源码
|