springboot如何通過apollo動態去注冊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:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM