springboot整合dubbo、shiro 无.xml配置文件在自定义的ShrioRealm中无法注入dubbo服务,全网各种找资料发现除了增加一个dubbo配置文件,别的都不行,本人一点一点的排查可能出现的问题,最终解决,在此作为记录,希望能为再碰到此坑的人提供一点小小的帮助。
springboot整合dubbo、shiro 无.xml配置文件,在只有springboot和dubbo没有碰到问题,dubbo服务可以正常注入。
dubbo配置类代码:
- @Configuration
- public class DubboConfig {
- /**
- * 应用名称
- */
- @Value("${dubbo.application.name}")
- private String applicationName;
- /**
- * 应用版本号
- */
- @Value("${dubbo.application.version}")
- private String applicationVersion;
- /**
- * 组织
- */
- @Value("${dubbo.application.organization}")
- private String organization;
- /**
- * 负责人
- */
- @Value("${dubbo.application.owner}")
- private String owner;
- /**
- * 注册中心地址
- */
- @Value("${dubbo.registry.address}")
- private String registryAddress;
- /**
- * 缓存文件地址
- */
- @Value("${dubbo.registry.file}")
- private String registryFile;
- /**
- * dubbo协议端口号
- */
- @Value("${dubbo.protocol.port}")
- private Integer protocolPort;
- /**
- * 应用相关配置
- *
- * @return
- */
- @Bean
- public ApplicationConfig applicationConfig() {
- ApplicationConfig applicationConfig = new ApplicationConfig();
- applicationConfig.setName(applicationName);
- applicationConfig.setVersion(applicationVersion);
- applicationConfig.setOrganization(organization);
- applicationConfig.setOwner(owner);
- return applicationConfig;
- }
- /**
- * 注册中心配置
- * 基于zookeeper
- *
- * @return
- */
- @Bean
- public RegistryConfig registryConfig() {
- RegistryConfig registryConfig = new RegistryConfig();
- registryConfig.setProtocol("zookeeper");
- registryConfig.setAddress(registryAddress);
- registryConfig.setFile(registryFile);
- registryConfig.setTimeout(30000);
- return registryConfig;
- }
- /**
- * 协议配置
- * 基于dubbo
- *
- * @return
- */
- @Bean
- public ProtocolConfig protocolConfig() {
- ProtocolConfig protocolConfig = new ProtocolConfig();
- protocolConfig.setName("dubbo");
- protocolConfig.setPort(protocolPort);
- return protocolConfig;
- }
- /**
- * 监控配置
- * 监控的地址从注册中心中找
- *
- * @return
- */
- @Bean
- public MonitorConfig monitorConfig() {
- MonitorConfig monitorConfig = new MonitorConfig();
- monitorConfig.setProtocol("registry");
- return monitorConfig;
- }
- /**
- * 设置dubbo扫描包
- *
- * @return
- */
- @Bean
- public static AnnotationBean annotationBean() {
- AnnotationBean annotationBean = new AnnotationBean();
- annotationBean.setPackage("com.vocedu.web.service.dubbo");
- return annotationBean;
- }
- /**
- * 提供者配置
- *
- * @return
- */
- @Bean(name = "defaultProvider")
- @ConditionalOnClass(Exporter.class)
- public ProviderConfig providerConfig() {
- ProviderConfig providerConfig = new ProviderConfig();
- providerConfig.setApplication(applicationConfig());
- providerConfig.setProtocol(protocolConfig());
- providerConfig.setRegistry(registryConfig());
- providerConfig.setMonitor(monitorConfig());
- providerConfig.setTimeout(30000);
- providerConfig.setRetries(0);
- providerConfig.setDelay(30000);
- return providerConfig;
- }
- /**
- * 消费者配置
- *
- * @return
- */
- @Bean(name = "defaultConsumer")
- @ConditionalOnClass(Invoker.class)
- public ConsumerConfig consumerConfig() {
- ConsumerConfig consumerConfig = new ConsumerConfig();
- consumerConfig.setApplication(applicationConfig());
- consumerConfig.setRegistry(registryConfig());
- consumerConfig.setMonitor(monitorConfig());
- consumerConfig.setCheck(false);
- consumerConfig.setTimeout(60000);
- consumerConfig.setRetries(0);
- return consumerConfig;
- }
- }
DubboSupport部分代码:
- @Component
- public class DubboSupport {
- @Reference(interfaceName = "com.vocedu.api.sys.AccountService")
- public AccountService accountService;
- @Reference(interfaceName = "com.vocedu.api.sys.AuthOauthClientService")
- public AuthOauthClientService authOauthClientService;
- }
但是当整合shiro时自定义的shiroRealm通过
- @Autowired
- private DubboSupport dubboSupport;
调用服务一直报空。纠结了好久,网上查各种资料最靠谱的说是整合shiro时不能用@Reference注解而要把自定义的shiroRealm中需要用到的服务使用dubbo的.xml配置
此种方法亲测确实可行。但是本人就是感觉别扭这样违背了springboot零配置文件的原则,于是不达目的不罢休,亲自研究。
把ShiorConfiguration配置类修改一下,先注入DubboSupport然后传入到ShiroRealm中
- @Configuration
- public class ShiroConfiguration {
- @Bean
- public static DubboSupport getDubboSupport() {
- DubboSupport dubboSupport = new DubboSupport();
- return dubboSupport;
- }
- //将自己的验证方式加入容器
- @Bean
- public ShiroRealm shiroRealm() {
- ShiroRealm shiroRealm = new ShiroRealm(getDubboSupport());
- return shiroRealm;
- }
- ………………………………………………………………
- ……………………………………………………
- }
此时,DubboSupport中修改为:
- //@Component
- public class DubboSupport {
- @Reference(interfaceName = "com.vocedu.api.sys.AccountService")
- public static AccountService accountService;
- @Reference(interfaceName = "com.vocedu.api.sys.AuthOauthClientService")
- public static AuthOauthClientService authOauthClientService;
- ………………………………
- ………………
- }
ShiroRealm做一下修改
- public class ShiroRealm extends AuthorizingRealm{
- //@Autowired
- private DubboSupport dubboSupport;
- public ShiroRealm(DubboSupport dubboSupport) {
- this.dubboSupport = dubboSupport;
- }
- …………………………
- …………………………
- }