# 创建缓存服务提供者
# 创建缓存服务接口项目
创建一个名为 myshop-service-redis-api
项目,该项目只负责定义接口
# POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cmcc</groupId>
<artifactId>myshop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../myshop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>myshop-service-redis-api</artifactId>
<packaging>jar</packaging>
<url>http://www.cmcc.com</url>
<inceptionYear>2018-Now</inceptionYear>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# RedisService
package com.cmcc.myshop.service.redis.api;
public interface RedisService {
/**
* 设置缓存
* @param key
* @param value
*/
void set(String key, Object value);
/**
* 设置缓存
* @param key
* @param value
* @param seconds 缓存有效期
*/
void set(String key, Object value, int seconds);
/**
* 获取缓存
* @param key
* @return
*/
Object get(String key);
/**
* 删除缓存
* @param key
*/
void delete(String key);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 创建缓存服务提供者
创建一个名为 myshop-service-redis-provider
项目
# POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cmcc</groupId>
<artifactId>myshop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../myshop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>myshop-service-redis-provider</artifactId>
<packaging>jar</packaging>
<url>http://www.cmcc.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Starter Settings -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Commons Settings -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
</dependency>
<!-- Projects Settings -->
<dependency>
<groupId>com.cmcc</groupId>
<artifactId>myshop-commons-dubbo</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.cmcc</groupId>
<artifactId>myshop-service-redis-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.cmcc.myshop.service.redis.provider.MyShopServiceRedisProviderApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
主要增加了 org.springframework.boot:spring-boot-starter-data-redis
,org.apache.commons:commons-pool2
两个依赖
# Application
package com.cmcc.myshop.service.redis.provider;
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication
public class MyShopServiceRedisProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MyShopServiceRedisProviderApplication.class, args);
Main.main(args);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# RedisServiceImpl
package com.cmcc.myshop.service.redis.provider.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.cmcc.myshop.service.redis.api.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
@Service(version = "${services.versions.redis.v1}")
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public void set(String key, Object value, int seconds) {
redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
}
@Override
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public void delete(String key) {
redisTemplate.delete(key);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# application.yml
# Spring boot application
spring:
application:
name: myshop-service-redis-provider
redis:
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
sentinel:
master: mymaster
nodes: 192.168.10.131:26379, 192.168.10.131:26380, 192.168.10.131:26381
server:
port: 8503
# Services Versions
services:
versions:
redis:
v1: 1.0.0
# Dubbo Config properties
dubbo:
## Base packages to scan Dubbo Component:@com.alibaba.dubbo.config.annotation.Service
scan:
basePackages: com.cmcc.myshop.service.redis.provider.api.impl
## ApplicationConfig Bean
application:
id: myshop-service-redis-provider
name: myshop-service-redis-provider
qos-port: 22224
qos-enable: true
## ProtocolConfig Bean
protocol:
id: dubbo
name: dubbo
port: 20883
status: server
serialization: kryo
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://192.168.10.131:2181?backup=192.168.10.131:2182,192.168.10.131:2183
# Enables Dubbo All Endpoints
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
## StatusChecker Name extras (default : empty )
extras: load,threadpool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 缓存服务消费者
在需要 Redis 服务的项目中增加 myshop-service-redis-api
依赖,直接远程调用即可