鍦ㄥ疄闄呴」鐩腑鎴戜滑闇€瑕佹墜鍔ㄦ潵鎵嬪啓Starter缁勪欢鐨勫満鏅浉瀵规潵璇磋繕鏄瘮杈冨皯鐨勶紝浣嗘槸瀵逛簬鑷畾涔塖tarter缁勪欢鐨勫師鐞嗗ぇ瀹惰繕鏄渶瑕佹帉鎻℃竻妤氾紝绗竴涓槸澧炲姞鑷繁鐨勮嚜淇″績锛岀浜屼釜鏄帉鎻$殑鑷姩瑁呴厤鐨勫師鐞嗗悗Starter缁勪欢鐨勫疄鐜板叾瀹炲氨鏄潪甯哥畝鍗曠殑浜嬫儏浜嗐€傛垜浠€氳繃Redis鐨勫鎴风绋嬪簭鏉ョ粰澶у浠嬬粛銆傚浜嶳edis鏈嶅姟鎴戜滑鍙互浣跨敤鐨凧ava瀹㈡埛绔繕鏄瘮杈冨鐨勶紝浣嗘槸鍦⊿pringBoot椤圭洰涓垜浠€庝箞鏉ヤ娇鐢ㄥ憿锛?/p>
鎵嬪啓SpringBoot Starter缁勪欢
1.鑷畾涔塖tarter
鍒涘缓涓€涓櫘閫氱殑Maven椤圭洰锛屽悕绉颁负redisson-spring-boot-starter
,鍛藉悕瑕佹敞鎰忥紝绗笁鏂箂tarter绾﹀畾鏄?xxxx
+spring-boot-starter
.
娣诲姞鐩稿叧鐨勪緷璧?/p>
<dependencies>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.15.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.8</version>
<optional>true</optional>
</dependency>
</dependencies>
娣诲姞灞炴€х被
/**
* 瀹氫箟灞炴€х被
* 鎸囧畾鍓嶇紑涓?bobo.redisson
*/
@ConfigurationProperties(prefix = "bobo.redisson")
public class RedissonProperties {
private String host = "localhost";
private int port = 6379;
private int timeout = 0;
private boolean ssl = false;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public boolean isSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
}
鐒跺悗鍒涘缓瀵瑰簲鐨勯厤缃被銆?/p>
@ConditionalOnClass(Redisson.class) // 鏉′欢瑁呴厤
// 鍏宠仈 灞炴€ч厤缃被
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration {
@Bean
public RedissonClient redissonClient(RedissonProperties redissonProperties){
Config config = new Config();
String prefix = "redis://";
if(redissonProperties.isSsl()){
prefix = "rediss://";
}
// 鍗曡妭鐐硅繛鎺ラ厤缃?
config.useSingleServer()
.setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort())
.setConnectTimeout(redissonProperties.getTimeout());
return Redisson.create(config);
}
}
杩欐牱鎴戜滑鐨勮嚜瀹氫箟Starter鐨勫姛鑳藉氨宸笉澶氫簡锛岀劧鍚庢槸瑕佸垱寤簊pring.factories鏂囦欢銆傛潵瀹炵幇鍜孲pringBoot椤圭洰鐨勮嚜鍔ㄨ閰嶃€?/p>
鏈€鍚庝负浜嗗鍔犲湪娣诲姞灞炴€ф枃浠剁殑鏃跺€欐湁鎻愮ず淇℃伅銆傛垜浠彲浠ュ厛瀵煎叆渚濊禆
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.4.8</version>
</dependency>
鐒跺悗娣诲姞json鏂囦欢锛屽悕绉板繀椤讳负additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "bobo.redisson.host",
"type": "java.lang.String",
"description": "Redis鐨勬湇鍔″櫒鍦板潃",
"defaultValue": "localhost"
},{
"name": "bobo.redisson.port",
"type": "java.lang.Integer",
"description": "Redis鐨勬湇鍔″櫒绔彛",
"defaultValue": 6379
}
]
}
鍒拌繖鍎垮氨鍙互浜唅nstall浜嗭紝鐒跺悗灏卞彲浠ヤ娇鐢ㄤ簡銆?/p>
2.SpringBoot椤圭洰浣跨敤
鑷畾涔塖tarter椤圭洰鍒涘缓瀹屾垚鍚庯紝鎴戜滑灏卞彲浠ュ湪SpringBoot椤圭洰涓潵浣跨敤浜嗐€傚厛瀵煎叆瀵瑰簲鐨勪緷璧栥€?/p>
鐒跺悗鎴戜滑鍦ㄥ睘鎬ф枃浠朵腑閰嶇疆Redis鐨勮繛鎺ヤ俊鎭紝棣栧厛鍙互鐪嬪埌鎻愮ず淇℃伅浜?/p>
瀹屾暣鐨勯厤缃俊鎭?/p>
# 娣诲姞MyBatis 鐨勭浉鍏抽厤缃俊鎭?
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
# 杩炴帴姹犵殑閰嶇疆
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
## 鎸囧畾鏄犲皠鏂囦欢鐨勪綅缃?
mybatis.mapper-locations=classpath:mapper/*.xml
## 鑷畾涔塖tart 鐨勫睘鎬ч厤缃?Redis 閰嶇疆
bobo.redisson.host=192.168.100.120
bobo.redisson.port=6379
bobo.redisson.timeout=5000
鐒跺悗鍒涘缓鎺у埗鍣ㄦ祴璇曘€?/p>
@RestController
public class UserController {
@Autowired
private IUserService userService;
@Autowired
private RedissonClient redissonClient;
@GetMapping("/hello")
public List<User> hello(){
return userService.query();
}
@GetMapping("/query")
public String query(){
RBucket<Object> name = redissonClient.getBucket("name");
if(name.get() == null){
name.set("BOBO");
}
return name.get().toString();
}
}
鍚姩鏈嶅姟娴嬭瘯锛?/p>
鐪嬪畬涓変欢浜嬧潳锔?/h2>
濡傛灉浣犺寰楄繖绡囧唴瀹瑰浣犺繕铔湁甯姪锛屾垜鎯抽個璇蜂綘甯垜涓変釜灏忓繖锛?/p>
鍏虫敞鍏紬鍙?銆?java鐑傜尓鐨?/strong> 銆忥紝涓嶅畾鏈熷垎浜師鍒涚煡璇嗐€?/p>
鍚屾椂鍙互鏈熷緟鍚庣画鏂囩珷ing馃殌
.鍏虫敞鍚庡洖澶嶃€?66銆戞壂鐮佸嵆鍙幏鍙栧涔犺祫鏂欏寘