没有引言
文章目录- 引言
- 自定义starter-步骤分析
- 创建 qunyakeji-redis-spring-boot-starter 模块,添加依赖
- 添加配置属性类
- 添加配置类(创建 Jedis Bean和RedisTemplate Bean)
- 定义META-INF/spring.factories 文件中,让SpringBoot自动加载
- 安装到本地仓库,即可使用
- 自定义starter-集成
- 使用redis
- 健康监控服务
- 引入依赖
- 监控-Admin可视化
- 搭建Server端
- 开启注解支持
- 搭建Client端
- 当导入redis坐标时,SpringBoot自动创建Jedis的Bean。
- 创建RedisTemplate Bean, 并完成序列化和反序列化操作
步骤:
- 创建 heima-redis-spring-boot-starter 模块,添加依赖
- 添加配置属性类
- 添加配置类(创建 Jedis Bean和RedisTemplate Bean)
- 并定义META-INF/spring.factories 文件中,让SpringBoot自动加载
- 在测试模块中引入自定义的 heima-redis-spring-boot-starter 依赖,测试获取 Jedis 的Bean,操作 redis。
添加配置属性类org.springframework.boot spring-boot-starter-parent 2.3.10.RELEASE 1.8 2.3.9.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-configuration-processor true org.springframework.data spring-data-redis compile redis.clients jedis org.projectlombok lombok com.fasterxml.jackson.core jackson-databind 2.11.4
package com.crow.sh.autoconfigure;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.time.Duration;
@Data
@ConfigurationProperties(prefix = "qunyakeji.redis")
public class RedisProperties {
private int database = 0;
private String host = "localhost";
private String password;
private int port = 6379;
private Duration timeout = Duration.ofSeconds(2);
}
添加配置类(创建 Jedis Bean和RedisTemplate Bean)
package com.crow.sh.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class QunyakejiRedisAutoConfiguration {
private RedisProperties redisProperties;
public QunyakejiRedisAutoConfiguration(RedisProperties redisProperties) {
this.redisProperties = redisProperties;
}
@Bean
public RedisSerializer
定义META-INF/spring.factories 文件中,让SpringBoot自动加载
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.crow.sh.autoconfigure.qunyakejiRedisAutoConfiguration安装到本地仓库,即可使用 自定义starter-集成
目的:验证自定义starter是否可以使用
实现:新建 boot_qunyakejiredis_test 项目中引入依赖
使用redisorg.springframework.boot spring-boot-starter-parent 2.3.10.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test com.crow.sh qunyakeji-redis-spring-boot-starter 1.0-SNAPSHOT
package com.crow.sh;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class RedisTest {
@Autowired
RedisTemplate redisTemplate;
@Test
public void testRedis() {
redisTemplate.boundValueOps("hello").set("123456");
Object hello = redisTemplate.boundValueOps("hello").get();
System.out.println(hello);
}
}
健康监控服务
引入依赖
org.springframework.boot spring-boot-starter-actuator
- 启动项目访问: http://localhost:{port}/actuator/**
暴露所有监控信息为HTTP
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
enabled: true # 开启健康检查详细信息
show-details: always
访问 http://localhost:{port}/actuator 会发现内容多了,里面的地址分别都可以访问,记录的是对应的健康监测的信息。
随便访问一个
Spring Boot Admin 有两个角色,客户端(Client)和服务端(Server)。
Spring Boot Admin为注册的应用程序提供以下功能:
- 显示健康状况
- 显示详细信息,例如
- JVM和内存指标
- micrometer.io指标
- 数据源指标
- 缓存指标
- 显示内部信息
- 关注并下载日志文件
- 查看JVM系统和环境属性
- 查看Spring Boot配置属性
- 支持Spring Cloud的可发布/ env-和// refresh-endpoint
- 轻松的日志级别管理
- 与JMX-beans交互
- 查看线程转储
- 查看http-traces
- 查看审核事件
- 查看http端点
- 查看预定的任务
- 查看和删除活动会话(使用spring-session)
- 查看Flyway / Liquibase数据库迁移
- 下载heapdump
- 状态更改通知(通过电子邮件,Slack,Hipchat等)
- 状态更改的事件日志(非持久性)
官方文档都有就不阐述了:传送
搭建Server端创建 qunyakeji_admin_server 模块,引入依赖
开启注解支持org.springframework.boot spring-boot-starter-parent 2.3.10.RELEASE de.codecentric spring-boot-admin-starter-server 2.3.1 org.springframework.boot spring-boot-starter-web
package com.crow.sh;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
修改个性端口,我这里是18999
搭建Client端在任意服务里面引入依赖
de.codecentric spring-boot-admin-starter-client 2.3.1
配置文件
# 执行admin.server地址
spring:
boot:
admin:
client:
url: http://localhost:18999 # admin 服务地址
instance:
prefer-ip: true # 显示IP
application:
name: boot_data # 项目名称
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
enabled: true # 开启健康检查详细信息
show-details: always
启动服务,访问admin Server http://localhost:18999/



