首页 » Latest Post

在spring mvc 模式下,war包会解压出来文件,可以用如下方法读取

String path = this.getClass().getResource("/rr-hitube-firebase-adminsdk-ivopy-0743e9a8b3.json").getFile();

但是在spring boot 模式下,这个会报找不到文件,可以用如下方法读取

File file = new ClassPathResource("rr-hitube-firebase-adminsdk-ivopy-0743e9a8b3.json").getInputStream();

Spring boot 不会解压jar包,所以没有File的概念,直接用getInputStream,读取数据流。

用firebase向android和ios推送消息,ios 是apsalert,android 是androidNotification,首先添加依赖https://firebase.google.com/docs/admin/setup?hl=zh-cn

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>6.4.0</version>
</dependency>

初始化firebase app

    @PostConstruct
    public void initFireBase() throws IOException {
        String path = this.getClass().getResource("/rr-hitube-firebase-adminsdk-ivopy-0743e9a8b3.json").getPath();
        FileInputStream serviceAccount = new FileInputStream(path);

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
//                .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
                .build();

        FirebaseApp.initializeApp(options);
    }

发消息,注意对于一条消息,格式上要兼容ios和android

public void pushMessage(ConsumerRecord<Long, String> consumerRecord) {
        try {
            PushMessageEvent pushMessageEvent = JSON.parseObject(consumerRecord.value(), PushMessageEvent.class);
            String topic = "";
            if (null != pushMessageEvent && null != pushMessageEvent.getUserId()) {
                topic = "9";
                ApsAlert apsAlert = ApsAlert.builder() //ios格式
                        .setTitle(pushMessageEvent.getTitle())
                        .setBody(pushMessageEvent.getContent())
                        .setLaunchImage(pushMessageEvent.getImage())
                        .build();
                AndroidNotification androidNotification = AndroidNotification.builder() //andorid格式
                        .setTitle(pushMessageEvent.getTitle())
                        .setBody(pushMessageEvent.getContent())
                        .setIcon(pushMessageEvent.getImage())

                        .build();

                Message message = Message.builder()
                        .setApnsConfig(ApnsConfig.builder()
                                .putHeader("apns-priority", "10")
                                .setAps(Aps.builder()
                                        .setAlert(apsAlert)
                                        .setBadge(1)
                                        .build())
                                .build())
                        .setAndroidConfig(AndroidConfig.builder().setNotification(androidNotification).build())
                        .putData("type",pushMessageEvent.getPushType().toString())
                        .setTopic(topic)
                        .build();

                String response = FirebaseMessaging.getInstance().send(message);
                System.out.println("Successfully sent message: " + response);
            }

        } catch (Exception e) {
            log.error("exception:", e);
        }

    }

这样就收到标准的消息了

kafka 发消息报错

org.apache.kafka.common.errors.SerializationException: Can't convert value of class java.lang.String to class org.apache.kafka.common.serialization.ByteArraySerializer specified in value.serializer

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to [B
    at org.apache.kafka.common.serialization.ByteArraySerializer.serialize(ByteArraySerializer.java:21)
    at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
    at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
    at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:791)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:768)
    at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:363)
    at brave.kafka.clients.TracingProducer.send(TracingProducer.java:79)
    at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:355)
    at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:182)
    at tv.rr.thor.bus.kafka.producer.KafkaMsgProducer.send(KafkaMsgProducer.java:34)
    at tv.rr.thor.bus.kafka.producer.KafkaMsgProducer$$FastClassBySpringCGLIB$$65ccb95e.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at org.springframework.cloud.sleuth.instrument.async.TraceAsyncAspect.traceBackgroundThread(TraceAsyncAspect.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at tv.rr.thor.bus.kafka.producer.KafkaMsgProducer$$EnhancerBySpringCGLIB$$d0f2998e.send(<generated>)
    at tv.rr.thor.bus.ThorBusApplicationTests.testKafka(ThorBusApplicationTests.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

代码如下


@Component
public class KafkaMsgProducer {

    @Autowired
    private KafkaTemplate<Long, String> kafkaTemplate;



    private static final Logger log = LoggerFactory.getLogger(KafkaMsgProducer.class);

    @Async
    public void send(String topic, Object msg) {

        ListenableFuture<SendResult<Long, String>> future = kafkaTemplate.send(topic, JSON.toJSONString(msg));
        future.addCallback(new ListenableFutureCallback<SendResult<Long, String>>() {
            @Override
            public void onFailure(Throwable throwable) {
                log.warn("Sending message to Kafka failed, topic {}, msg: {}", topic, JSON.toJSONString(msg));
            }

            @Override
            public void onSuccess(SendResult<Long, String> season) {
                log.info("Sending message to Kafka finished, topic {}, msg: {}", topic, JSON.toJSONString(msg));
            }
        });

    }
}

原因分析:java value的序列化和反序列化没有配置,配置如下

spring:
  kafka:
    producer:
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
#      key-serializer: org.apache.kafka.common.serialization.LongSerializer
    consumer:
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

还有有的项目不需要配置,怀疑是版本问题,默认序列化器有的是String,有的不是

今天配置hikari后犯了一个低级错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.datasource.type' to java.lang.Class<javax.sql.DataSource>:

    Property: spring.datasource.type
    Value: com.zaxxer.hikari.HikariDataSource
    Origin: "spring.datasource.type" from property source "bootstrapProperties"
    Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<javax.sql.DataSource>]

Action:

Update your application's configuration

原因是没有引用hikari连接池

<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>