spring clould alibaba nacos 读取配置 集成极光推送

spring clould alibaba nacos 读取配置

spring clould alibaba nacos 读取配置
在使用使用中,我们常常需要一些静态配置,如果写在代码中,不利于维护,因此我们可以利用nacos中加入配置,然后启动项目的时候读取配置

场景:集成极光推送需要配置
appkey和MasterSecret,所以在naocs的自己的模块下新建了
以上是极光通讯的配置
我们新建一个对应的配置类
然后以下是启动方法

/**
 * <p>
 * 项目启动时创建对象推送
 * </p>
 *
 * @author lihl
 * @since 2020/4/20
 */
 //启动类启动的同时加载
@Component
//日志
@Log
@Getter
//配置装载
@Configuration
public class InitJpushCompnent implements CommandLineRunner {

    /**
     * <p>
     * 读取配文件
     * </p>
     *
     * @author lihl
     * @since 2020/4/20
     */
     //提供实例
    @Bean
    //jpush与nacos配置对应
    @ConfigurationProperties("jpush")
    public JPushProperties jPushProperties() {
        return new JPushProperties();
    }
}

这样就能将nacos的配置拿出来,我的用法如下

 /**
     * c端对象
     */
    private JPushClient cJpushClient;
    /**
     * b端对象
     */
    private JPushClient bJpushClient;
    /**
     * b端消息对象
     */
    private JMessageClient bJmessgaeClient;
    /**
     * c端消息对象
     */
    private JMessageClient cJmessgaeClient;
    /**
     * c端消息对象
     */
    private Long timeToLive;
    /**
     * <p>
     * 初始化所有对象
     * </p>
     * 
     * @param args args
     * @return void
     * @author lihl 
     * @since 2020/4/20
     */
    @Override
    public void run(String... args) throws Exception {
        JPushProperties jPushProperties = jPushProperties();
        cJpushClient = new JPushClient(jPushProperties.getCMasterSecret(),jPushProperties.getCAppKey());
        bJpushClient = new JPushClient(jPushProperties.getBMasterSecret(),jPushProperties.getBAppKey());
        bJmessgaeClient = new JMessageClient(jPushProperties.getBAppKey(),jPushProperties.getBMasterSecret());
        cJmessgaeClient = new JMessageClient(jPushProperties.getCAppKey(),jPushProperties.getCMasterSecret());
        timeToLive = jPushProperties.getTimeToLive();
        log.fine(this.getClass().getName() + " DuckLoad 方法加载数据");;
    }

用于实例化极光对象

@Slf4j
@Service
@AllArgsConstructor
public class AynsJPushServiceImpl implements AynsJPushService {
//已经初始化
    final  InitJpushCompnent initJpushCompnent;
    /**
     * <p>
     * 推送个部分人员
     * </p>
     *
     * @since 2020/4/15
     */
    @Override
    public PushResult pushToUser(AsynPushEntity pushEntity, DeviceType deviceType,BaseMsgType type) {
        PushPayload payload = sendPush(pushEntity,type);
        try {
            PushResult result = DeviceType.b.equals(deviceType)?
//在这里使用初始完成的极光对象            initJpushCompnent.getBJpushClient().sendPush(payload):initJpushCompnent.getBJpushClient().sendPush(payload);
            if(result.error!=null){
                R.failed(result.error.getMessage());
            }
            System.out.println("success");
            System.out.println("消息id为:"+result.msg_id);
            System.out.println("发送id为:"+result.sendno);
            return  result;
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }
}

这里是调用过程,我在其他地方调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值