共计 5706 个字符,预计需要花费 15 分钟才能阅读完成。
SLS 日志服务的集成配置是怎样的,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
配置 pom.xml 依赖
!-- 服务器 --
dependency
groupId com.aliyun.openservices /groupId
artifactId log-loghub-producer /artifactId
version 0.1.4 /version
exclusions
exclusion
groupId com.alibaba /groupId
artifactId fastjson /artifactId
/exclusion
/exclusions
/dependency
dependency
groupId com.aliyun.openservices /groupId
artifactId aliyun-log-producer /artifactId
version 0.3.4 /version
/dependency
dependency
groupId com.aliyun.openservices /groupId
artifactId aliyun-log /artifactId
version 0.6.33 /version
/dependency
dependency
groupId com.google.protobuf /groupId
artifactId protobuf-java /artifactId
version 2.5.0 /version
/dependency
dependency
groupId com.aliyun.openservices /groupId
artifactId loghub-client-lib /artifactId
version 0.6.16 /version
/dependency
配置 AliLogConfig
package com.yhzy.doudoubookserver.global.alilog;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.producer.LogProducer;
import com.aliyun.openservices.log.producer.ProducerConfig;
import com.aliyun.openservices.log.producer.ProjectConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
* @author zhangqinghe
* @version 1.0.0
* @email her550@dingtalk.com
* @date 2020/12/23
* @since 1.0.0
*/
@Configuration
@Scope(singleton)
public class AliLogConfig {
public static String accessKeyId =
public static String accessKeySecret =
public static String endPoint =
public static String projectName = SLSEnvironment.BOOK_PROJECT;
@Bean
@ConditionalOnClass(LogProducer.class)
public LogProducer getLogProducer() { LogProducer producer = new LogProducer(producerConfig());
producer.setProjectConfig(projectConfig());
return producer;
}
@Bean
@ConditionalOnClass(ProducerConfig.class)
public ProducerConfig producerConfig() { ProducerConfig producerConfig = new ProducerConfig();
// 被缓存起来的日志的发送超时时间,如果缓存超时,则会被立即发送,单位是毫秒
producerConfig.packageTimeoutInMS = 1000;
// 每个缓存的日志包的大小的上限,不能超过 5MB,单位是字节
producerConfig.logsBytesPerPackage = 5 * 1024 * 1024;
// 每个缓存的日志包中包含日志数量的最大值,不能超过 4096
producerConfig.logsCountPerPackage = 4096;
// 单个 producer 实例可以使用的内存的上限,单位是字节
producerConfig.memPoolSizeInByte = 1000 * 1024 * 1024;
//IO 线程池最大线程数量,主要用于发送数据到日志服务
producerConfig.maxIOThreadSizeInPool = 50;
// 当使用指定 shardhash 的方式发送日志时,这个参数需要被设置,否则不需要关心。后端 merge 线程会将映射到同一个 shard 的数据 merge 在一起,而 shard 关联的是一个 hash 区间, //producer 在处理时会将用户传入的 hash 映射成 shard 关联 hash 区间的最小值。每一个 shard 关联的 hash 区间,producer 会定时从 loghub 拉取,该参数的含义是每隔 shardHashUpdateIntervalInMS 毫秒, producerConfig.shardHashUpdateIntervalInMS = 10 * 60 * 1000;
producerConfig.retryTimes = 3;
return producerConfig;
}
@Bean
@ConditionalOnClass(ProjectConfig.class)
public ProjectConfig projectConfig() { return new ProjectConfig(projectName, endPoint, accessKeyId, accessKeySecret);
}
/**
* 读取 sls 对象 用于读取数据
* @return
*/
@Bean
public Client client(){
String accessId =
String accessKey =
String host =
return new Client(host, accessId, accessKey);
}
}
配置 AliLogUtil
package com.yhzy.doudoubookserver.common;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.producer.LogProducer;
import com.yhzy.doudoubookserver.global.alilog.AliLogConfig;
import com.yhzy.doudoubookserver.global.alilog.CallbackLogInfo;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Vector;
* @author zhangqinghe
* @version 1.0.0
* @email her550@dingtalk.com
* @date 2020/12/23
* @since 1.0.0
*/
@Component
public class AliLogUtil {
@Resource
private AliLogConfig aliLogConfig;
public void saveLog(String projectName,String logStore, Vector LogItem logGroup, String topic, String source,Long millis) throws InterruptedException { final LogProducer logProducer = aliLogConfig.getLogProducer();
// 并发调用 send 发送日志
logProducer.send(projectName, logStore, topic, source, logGroup,
new CallbackLogInfo(projectName, logStore, topic,null, source, logGroup, logProducer));
// 主动刷新缓存起来的还没有被发送的日志
logProducer.flush();
// 等待发送线程退出
Thread.sleep(millis);
// 关闭后台 io 线程,close 会将调用时刻内存中缓存的数据发送出去
logProducer.close();
}
}
配置 CallbackLogInfo
package com.yhzy.doudoubookserver.global.alilog;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.producer.ILogCallback;
import com.aliyun.openservices.log.producer.LogProducer;
import com.aliyun.openservices.log.response.PutLogsResponse;
import java.util.Vector;
* @author zhangqinghe
* @version 1.0.0
* @email her550@dingtalk.com
* @date 2020/12/23
* @since 1.0.0
*/
public class CallbackLogInfo extends ILogCallback {
// 保存要发送的数据,当时发生异常时,进行重试
public String project;
public String logstore;
public String topic;
public String shardHash;
public String source;
public Vector LogItem items;
public LogProducer producer;
public int retryTimes = 0;
public CallbackLogInfo(String project, String logstore, String topic, String shardHash, String source,
Vector LogItem items, LogProducer producer) { super();
this.project = project;
this.logstore = logstore;
this.topic = topic;
this.shardHash = shardHash;
this.source = source;
this.items = items;
this.producer = producer;
}
public void onCompletion(PutLogsResponse response, LogException e) { if (e != null) {
// 打印异常
System.out.println(e.GetErrorCode() + , + e.GetErrorMessage() + , + e.GetRequestId());
// 最多重试三次
if (retryTimes++ 3) { producer.send(project, logstore, topic, source, shardHash, items, this);
}
} else {
// 请求 id
System.out.println(send success, request id: + response.GetRequestId());
}
}
}
配置 SLSEnvironment
记录 sls 日志相关 project logStore 参数名配置
package com.yhzy.doudoubookserver.global.alilog;
正文完
发表至: 计算机运维
2023-08-25