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

spring clould alibaba nacos 读取配置

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

场景:集成极光推送需要配置
appkey和MasterSecret,所以在naocs的自己的模块下新建了
spring clould alibaba nacos 读取配置 集成极光推送_第1张图片
spring clould alibaba nacos 读取配置 集成极光推送_第2张图片
然后以下是启动方法

/**
 * 

* 项目启动时创建对象推送 *

* * @author lihl * @since 2020/4/20 */ //启动类启动的同时加载 @Component //日志 @Log @Getter //配置装载 @Configuration public class InitJpushCompnent implements CommandLineRunner { /** *

* 读取配文件 *

* * @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;
    /**
     * 

* 初始化所有对象 *

* * @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;
    /**
     * 

* 推送个部分人员 *

* * @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; } }

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

你可能感兴趣的:(我学到的,java,spring,后端)