springboot如何通过apollo动态+ 注解@EnableDubbo 去注册dubbo服务
参考相关文章:
apollo官方文档: https://dubbo.apache.org/zh/docs/v2.7/user/configuration/configuration-load-process/
Dubbo注解方式与spring的整合原理即@DubboService的机制: https://blog.csdn.net/leisurelen/article/details/107019516
第一步:apollo配置:
spring.application.name = csc-mbhk-loyalty-member
dubbo.scan.base-packages = com.aswatson.csc.member.service
dubbo.registry.protocol = zookeeper
dubbo.registry.address = 127.0.0.1:2181
dubbo.protocol.name = dubbo
dubbo.protocol.port = -1
dubbo.protocol.dispatcher = message
dubbo.protocol.threadpool = cached
dubbo.protocol.threads = 800
。。。。。。。
第二步:注销springboot里面配置的加载dubbo的xml配置和springboot管理的dubbo的bean:
<!-- <dubbo:protocol name="dubbo" port="-1" dispatcher="message" threadpool="cached" threads="${cdc_mbhk_loyalty_member_threads:800}"/>-->
第三步:启动类加上注解:@EnableDubbo:
package com.aswatson.csc.member; import java.util.concurrent.Executor; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /** * @author Albert.Yang */ @Configuration @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @ComponentScan({"com.aswatson.csc.member.*"}) @EnableDubbo @MapperScan("com.aswatson.csc.member.infrastructure.dao.mapper.*") @ImportResource(locations = {"classpath:dubbo/spring-csc-member-agent-context.xml"}) @EnableDiscoveryClient @EnableScheduling @EnableAsync public class MemberApplication { public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); } @Bean MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) { return registry -> registry.config().commonTags("application", applicationName); } }
第四步:MemberCardService层加注解@DubboService和修改配置:
/** * @Author Tim.Li * @Date 2020/4/3 14:56 */ //ConditionalOnProperty是为了如果配置了,就可以动态开启是否加载,注册这个service配置 @ConditionalOnProperty(value = "${dubbo.service.com.aswatson.csc.member.service.MemberCardService}",havingValue = "true") @Component @Slf4j @DubboService(interfaceClass = MemberCardService.class ,version = "1.0", group = "${pos}") public class MemberCardServiceImpl implements MemberCardService { @Override @Transactional public ResponseMessages<MemberUpsetResVO> updateMemberInfo(MemberUpsetVO memberUpsetVO) { System.out.println("=====================================1"); return null; } }
解释:用dubboService注入的bean:MemberCardService 会和原生的 org.springframework.beans. 包的注入@Autowired 方式冲突,
因为一种是spring框架自己管理的bean方式,dubboService的是通过dubbo方式来注入和管理bean。需要统一成一致的,或者兼容(暂无测试兼容的版本)。
第五步:通过入口层调用dubbo服务:
private static MemberCardService memberCardService = (MemberCardService) ApplicationContextRegister .getApplicationContext().getBean("memberCardService");
//写在方法里面:
ResponseMessages<MemberUpsetResVO> responseMessages = memberCardService.updateMemberInfo(memberUpsertVO);
package com.siebel.api.server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component @Lazy(false) public class ApplicationContextRegister implements ApplicationContextAware { private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationContextRegister.class); private static ApplicationContext APPLICATION_CONTEXT; /** * * 设置spring上下文 * * @param applicationContext spring上下文 * @throws * BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { LOGGER.debug("ApplicationContext registed-->{}", applicationContext); APPLICATION_CONTEXT = applicationContext; } public static ApplicationContext getApplicationContext() { return APPLICATION_CONTEXT; } }
.................测试结果如下:
1.dubbo服务提供者项目起来之后,需要监控prodvier:是否注册到dubbo服务,拥到的工具:PrettyZoo
2.启动消费者服务,去调用provider的dubbo服务:
结果: 调用成功。注销了springboot模式下加载dubbo的bean模式,不需要配置xml形式的dubbo的bean:
学海无涯 代码作伴