├── .gitignore
├── LICENSE
├── README.md
├── alipay.png
├── flycache-core
├── .gitignore
├── doc
│ └── sql
│ │ └── node_group.sql
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── io
│ │ └── github
│ │ └── flylib
│ │ └── flycache
│ │ ├── FlycacheCoreApp.java
│ │ ├── config
│ │ ├── Config.java
│ │ ├── MyBatisConfig.java
│ │ └── MyBatisMapperScannerConfig.java
│ │ ├── controller
│ │ └── IndexController.java
│ │ ├── entity
│ │ └── NodeGroupEntity.java
│ │ ├── hash
│ │ ├── ByteUtils.java
│ │ ├── ConsistentHash.java
│ │ ├── HashAlgorithm.java
│ │ └── Node.java
│ │ ├── postgre
│ │ └── JSONTypeHandler.java
│ │ ├── redis
│ │ ├── JedisPoolShardFactory.java
│ │ ├── JedisSettings.java
│ │ ├── MyJedisPoolConfig.java
│ │ └── RedisClient.java
│ │ ├── repository
│ │ └── NodeRepositery.java
│ │ ├── service
│ │ └── NodeGroupService.java
│ │ ├── spring
│ │ └── AppContextHolder.java
│ │ └── util
│ │ └── ShardUtil.java
│ └── resources
│ └── application.properties
├── redPacket.png
└── wechat.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # maven ignore
2 | target/
3 | *.jar
4 | *.war
5 | *.zip
6 | *.tar
7 |
8 | # eclipse ignore
9 | .settings/
10 | .project
11 | .classpath
12 |
13 | # idea ignore
14 | .idea/
15 | *.ipr
16 | *.iml
17 | *.iws
18 |
19 | # temp ignore
20 | logs/
21 | *.doc
22 | *.log
23 | *.cache
24 | *.diff
25 | *.patch
26 | *.tmp
27 |
28 | # system ignore
29 | .DS_Store
30 | Thumbs.db
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # flycache
2 |
3 | ## 简介
4 | flycache是redis分布式分片存储的一个系统,支持弹性伸缩。 基于Java实现的一致性hash
5 |
6 |
7 | flycache: Based on Java consistent-hashing Redis elastic shard storage.
8 |
9 | ## 功能
10 | 1. 弹性伸缩,提供控制面板,管理员可以增加和删除Redis节点
11 | 2. Redis运行状态监控,报警
12 | 3. Redis故障或者网络故障的灾难应对
13 |
14 | ## 原理
15 | 1. consistent-hashing一致性hash
16 | 2. zookeeper保持一致性和监听
17 |
18 | ## 文章
19 | http://blog.csdn.net/lsm135/article/details/79081776
20 |
21 | ## 测试
22 | [http://localhost:20200/test1](http://localhost:20200/test1)
23 |
24 | [http://localhost:20200/nodeGroup](http://localhost:20200/nodeGroup)
25 |
26 | 如果您愿意捐助一下项目,可以通过微信/支付宝捐助哟~
27 |
28 | |支付宝 | 微信 | 余额宝领红包|
29 | | ------------- |:-------------:|:-------------:|
30 | |  |  |  |
31 |
32 |
--------------------------------------------------------------------------------
/alipay.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bootsrc/flycache/632bbd824f0a549f7703703b7d2a60016527f95a/alipay.png
--------------------------------------------------------------------------------
/flycache-core/.gitignore:
--------------------------------------------------------------------------------
1 | # maven ignore
2 | target/
3 | *.jar
4 | *.war
5 | *.zip
6 | *.tar
7 |
8 | # eclipse ignore
9 | .settings/
10 | .project
11 | .classpath
12 |
13 | # idea ignore
14 | .idea/
15 | *.ipr
16 | *.iml
17 | *.iws
18 |
19 | # temp ignore
20 | logs/
21 | *.doc
22 | *.log
23 | *.cache
24 | *.diff
25 | *.patch
26 | *.tmp
27 |
28 | # system ignore
29 | .DS_Store
30 | Thumbs.db
31 |
--------------------------------------------------------------------------------
/flycache-core/doc/sql/node_group.sql:
--------------------------------------------------------------------------------
1 | /*
2 | Navicat PGSQL Data Transfer
3 |
4 | Source Server : local-postgres
5 | Source Server Version : 100100
6 | Source Host : localhost:5432
7 | Source Database : flycache
8 | Source Schema : public
9 |
10 | Target Server Type : PGSQL
11 | Target Server Version : 100100
12 | File Encoding : 65001
13 |
14 | Date: 2018-01-23 18:30:55
15 | */
16 |
17 |
18 | -- ----------------------------
19 | -- Table structure for node_group
20 | -- ----------------------------
21 | DROP TABLE IF EXISTS "public"."node_group";
22 | CREATE TABLE "public"."node_group" (
23 | "id" varchar(60) COLLATE "default" NOT NULL,
24 | "creater" varchar(60) COLLATE "default",
25 | "node" varchar COLLATE "default",
26 | "time" varchar(60) COLLATE "default"
27 | )
28 | WITH (OIDS=FALSE)
29 |
30 | ;
31 |
32 | -- ----------------------------
33 | -- Records of node_group
34 | -- ----------------------------
35 | INSERT INTO "public"."node_group" VALUES ('999', 'lsm', '[{"host": "localhost", "port": "6379", "remark": ""}, {"host": "localhost", "port": "6380", "remark": ""}, {"host": "localhost", "port": "6381", "remark": ""}]', '2018-01-23 00:00:01');
36 |
37 | -- ----------------------------
38 | -- Alter Sequences Owned By
39 | -- ----------------------------
40 |
41 | -- ----------------------------
42 | -- Primary Key structure for table node_group
43 | -- ----------------------------
44 | ALTER TABLE "public"."node_group" ADD PRIMARY KEY ("id");
45 |
--------------------------------------------------------------------------------
/flycache-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | io.github.flylib
8 | flycache-core
9 | 1.0-SNAPSHOT
10 |
11 |
12 | org.springframework.boot
13 | spring-boot-starter-parent
14 | 1.5.8.RELEASE
15 |
16 |
17 |
18 |
19 | UTF-8
20 | UTF-8
21 | 1.8
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-maven-plugin
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-web
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-actuator
45 |
46 |
47 | org.apache.tomcat.embed
48 | tomcat-embed-jasper
49 | provided
50 |
51 |
52 | javax.servlet
53 | jstl
54 |
55 |
56 | com.alibaba
57 | fastjson
58 | 1.2.41
59 |
60 |
61 | org.apache.commons
62 | commons-lang3
63 | 3.7
64 |
65 |
66 | redis.clients
67 | jedis
68 | 2.9.0
69 |
70 |
71 | org.springframework.boot
72 | spring-boot-starter-jdbc
73 |
74 |
75 | org.springframework.boot
76 | spring-boot-starter-jdbc
77 |
78 |
79 | org.apache.tomcat
80 | tomcat-jdbc
81 |
82 |
83 |
84 |
85 | com.alibaba
86 | druid
87 | 1.1.6
88 |
89 |
90 | org.mybatis.spring.boot
91 | mybatis-spring-boot-starter
92 | 1.3.1
93 |
94 |
95 | org.postgresql
96 | postgresql
97 | 42.1.4
98 |
99 |
100 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/FlycacheCoreApp.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author liushaoming
8 | * @Package io.github.flylib.flycache
9 | * @Description:
10 | * @date 2018-1-19 9:53
11 | */
12 | @SpringBootApplication
13 | public class FlycacheCoreApp {
14 | public static void main(String[] args){
15 | SpringApplication.run(FlycacheCoreApp.class, args);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/config/Config.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.config;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.core.env.Environment;
8 |
9 | import javax.sql.DataSource;
10 |
11 | /**
12 | * @author liushaoming
13 | * @Package io.github.flylib.flycache.config
14 | * @Description:
15 | * @date 2018-1-23 14:34
16 | */
17 | @Configuration
18 | public class Config {
19 | @Autowired
20 | private Environment env;
21 |
22 | @Bean
23 | public DataSource dataSource() {
24 | DruidDataSource dataSource = new DruidDataSource();
25 | dataSource.setUrl(env.getProperty("spring.datasource.url"));
26 | dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
27 | dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
28 | dataSource.setInitialSize(2);
29 | dataSource.setMaxActive(20);
30 | dataSource.setMinIdle(0);
31 | dataSource.setMaxWait(60000);
32 | dataSource.setValidationQuery("SELECT 1");
33 | dataSource.setTestOnBorrow(false);
34 | dataSource.setTestWhileIdle(true);
35 | dataSource.setPoolPreparedStatements(false);
36 | return dataSource;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/config/MyBatisConfig.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.config;
2 |
3 | import org.apache.ibatis.session.SqlSessionFactory;
4 | import org.mybatis.spring.SqlSessionFactoryBean;
5 | import org.mybatis.spring.SqlSessionTemplate;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
10 | import org.springframework.transaction.PlatformTransactionManager;
11 | import org.springframework.transaction.annotation.EnableTransactionManagement;
12 | import org.springframework.transaction.annotation.TransactionManagementConfigurer;
13 |
14 | import javax.sql.DataSource;
15 |
16 | /**
17 | * @author liushaoming
18 | * @Package io.github.flylib.flycache.config
19 | * @Description:
20 | * @date 2018-1-23 16:48
21 | */
22 | @Configuration
23 | //加上这个注解,使得支持事务
24 | @EnableTransactionManagement
25 | public class MyBatisConfig implements TransactionManagementConfigurer {
26 | @Autowired
27 | private DataSource dataSource;
28 |
29 | @Override
30 | public PlatformTransactionManager annotationDrivenTransactionManager() {
31 | return new DataSourceTransactionManager(dataSource);
32 | }
33 |
34 | @Bean(name = "sqlSessionFactory")
35 | public SqlSessionFactory sqlSessionFactoryBean() {
36 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
37 | bean.setDataSource(dataSource);
38 |
39 | try {
40 | return bean.getObject();
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | throw new RuntimeException(e);
44 | }
45 | }
46 |
47 | @Bean
48 | public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
49 | return new SqlSessionTemplate(sqlSessionFactory);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/config/MyBatisMapperScannerConfig.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.config;
2 |
3 | import org.mybatis.spring.mapper.MapperScannerConfigurer;
4 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 |
8 | /**
9 | * @author liushaoming
10 | * @Package io.github.flylib.flycache.config
11 | * @Description:
12 | * @date 2018-1-23 16:51
13 | *
14 | * 扫描mybatis的接口
15 | */
16 | @Configuration
17 | // 因为这个对象的扫描,需要在MyBatisConfig的后面注入,所以加上下面的注解
18 | @AutoConfigureAfter(MyBatisConfig.class)
19 | public class MyBatisMapperScannerConfig {
20 | @Bean
21 | public MapperScannerConfigurer mapperScannerConfigurer() {
22 | MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
23 | //获取之前注入的beanName为sqlSessionFactory的对象
24 | mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
25 | //指定xml配置文件的路径
26 | // mapperScannerConfigurer.setBasePackage("com.framework.msg.mapper");
27 | mapperScannerConfigurer.setBasePackage("io.github.flylib.flycache.repository");
28 | return mapperScannerConfigurer;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/controller/IndexController.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.controller;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.alibaba.fastjson.JSONArray;
5 | import io.github.flylib.flycache.entity.NodeGroupEntity;
6 | import io.github.flylib.flycache.redis.JedisPoolShardFactory;
7 | import io.github.flylib.flycache.redis.RedisClient;
8 | import io.github.flylib.flycache.service.NodeGroupService;
9 | import io.github.flylib.flycache.spring.AppContextHolder;
10 | import io.github.flylib.flycache.hash.Node;
11 | import io.github.flylib.flycache.util.ShardUtil;
12 | import org.slf4j.Logger;
13 | import org.slf4j.LoggerFactory;
14 | import org.springframework.beans.factory.annotation.Autowired;
15 | import org.springframework.stereotype.Controller;
16 | import org.springframework.web.bind.annotation.RequestMapping;
17 | import org.springframework.web.bind.annotation.ResponseBody;
18 | import redis.clients.jedis.JedisPool;
19 |
20 | import java.util.List;
21 |
22 | /**
23 | * @author liushaoming
24 | * @Package io.github.flylib.flycache.controller
25 | * @Description:
26 | * @date 2018-1-19 9:55
27 | */
28 | @Controller
29 | public class IndexController {
30 | private final Logger log = LoggerFactory.getLogger(IndexController.class);
31 |
32 | @Autowired
33 | private NodeGroupService nodeGroupService;
34 |
35 | @ResponseBody
36 | @RequestMapping("")
37 | public String demo() {
38 | return "demo response from flycache-core.-------";
39 | }
40 |
41 | @ResponseBody
42 | @RequestMapping("testRedis")
43 | public String testRedis() {
44 | JedisPoolShardFactory jedisPoolShardFactory = AppContextHolder.getBean(JedisPoolShardFactory.class);
45 | Node node = new Node();
46 | node.setHost("127.0.0.1");
47 | node.setPort("6379");
48 | node.setRemark("");
49 | JedisPool jedisPool = jedisPoolShardFactory.getJedisPool(node);
50 | RedisClient redisClient = new RedisClient();
51 | redisClient.setJedisPool(jedisPool);
52 | String key = "testCache";
53 | redisClient.set(key, "mmmmmmm");
54 | String value = redisClient.get(key);
55 | System.out.println(value);
56 | return value;
57 | }
58 |
59 | @ResponseBody
60 | @RequestMapping("test1")
61 | public String test1() {
62 | ShardUtil.initNodes();
63 | return "test1 done";
64 | }
65 |
66 | @ResponseBody
67 | @RequestMapping("nodeGroup")
68 | public String nodeGroup() {
69 | List entityList = nodeGroupService.all();
70 |
71 |
72 | if (entityList != null && entityList.size() > 0) {
73 | for (NodeGroupEntity entity : entityList) {
74 | String jsonStr = entity.getNode();
75 | List nodeList = JSON.parseArray(jsonStr, Node.class);
76 | log.info("nodeList={}", nodeList);
77 | }
78 | }
79 |
80 | String listStr = JSONArray.toJSONString(entityList);
81 | return listStr;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/entity/NodeGroupEntity.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.entity;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | /**
7 | * @author liushaoming
8 | * @Package io.github.flylib.flycache.entity
9 | * @Description:
10 | * @date 2018-1-23 17:08
11 | */
12 | public class NodeGroupEntity {
13 | private String id;
14 | private String creater;
15 | private String node;
16 | private String time;
17 |
18 | public String getId() {
19 | return id;
20 | }
21 |
22 | public void setId(String id) {
23 | this.id = id;
24 | }
25 |
26 | public String getCreater() {
27 | return creater;
28 | }
29 |
30 | public void setCreater(String creater) {
31 | this.creater = creater;
32 | }
33 |
34 | public String getNode() {
35 | return node;
36 | }
37 |
38 | public void setNode(String node) {
39 | this.node = node;
40 | }
41 |
42 | public String getTime() {
43 | return time;
44 | }
45 |
46 | public void setTime(String time) {
47 | this.time = time;
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "NodeGroupEntity{" +
53 | "id='" + id + '\'' +
54 | ", creater='" + creater + '\'' +
55 | ", node=" + node +
56 | ", time='" + time + '\'' +
57 | '}';
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/flycache-core/src/main/java/io/github/flylib/flycache/hash/ByteUtils.java:
--------------------------------------------------------------------------------
1 | package io.github.flylib.flycache.hash;
2 |
3 | import java.io.UnsupportedEncodingException;
4 | import java.nio.ByteBuffer;
5 | import java.nio.charset.Charset;
6 |
7 |
8 | /**
9 | * @author liushaoming
10 | *
11 | * Utilities for byte process
12 | */
13 | public final class ByteUtils {
14 | public static final String DEFAULT_CHARSET_NAME = "utf-8";
15 | public static final Charset DEFAULT_CHARSET = Charset
16 | .forName(DEFAULT_CHARSET_NAME);
17 | /**
18 | * if it is testing,check key argument even if use binary protocol. The user
19 | * must never change this value at all.
20 | */
21 | public static boolean testing;
22 |
23 | private ByteUtils() {
24 | }
25 |
26 | public static boolean isNumber(String string) {
27 | if (string == null || string.isEmpty()) {
28 | return false;
29 | }
30 | int i = 0;
31 | if (string.charAt(0) == '-') {
32 | if (string.length() > 1) {
33 | i++;
34 | } else {
35 | return false;
36 | }
37 | }
38 | for (; i < string.length(); i++) {
39 | if (!Character.isDigit(string.charAt(i))) {
40 | return false;
41 | }
42 | }
43 | return true;
44 | }
45 |
46 | public static final byte[] getBytes(String k) {
47 | if (k == null || k.length() == 0) {
48 | throw new IllegalArgumentException("Key must not be blank");
49 | }
50 | try {
51 | return k.getBytes(DEFAULT_CHARSET_NAME);
52 | } catch (UnsupportedEncodingException e) {
53 | throw new RuntimeException(e);
54 | }
55 | }
56 |
57 |
58 | private static int maxKeyLength = 250;
59 |
60 |
61 | public static final int normalizeCapacity(int requestedCapacity) {
62 | switch (requestedCapacity) {
63 | case 0:
64 | case 1 << 0:
65 | case 1 << 1:
66 | case 1 << 2:
67 | case 1 << 3:
68 | case 1 << 4:
69 | case 1 << 5:
70 | case 1 << 6:
71 | case 1 << 7:
72 | case 1 << 8:
73 | case 1 << 9:
74 | case 1 << 10:
75 | case 1 << 11:
76 | case 1 << 12:
77 | case 1 << 13:
78 | case 1 << 14:
79 | case 1 << 15:
80 | case 1 << 16:
81 | case 1 << 17:
82 | case 1 << 18:
83 | case 1 << 19:
84 | case 1 << 21:
85 | case 1 << 22:
86 | case 1 << 23:
87 | case 1 << 24:
88 | case 1 << 25:
89 | case 1 << 26:
90 | case 1 << 27:
91 | case 1 << 28:
92 | case 1 << 29:
93 | case 1 << 30:
94 | case Integer.MAX_VALUE:
95 | return requestedCapacity;
96 | }
97 |
98 | int newCapacity = 1;
99 | while (newCapacity < requestedCapacity) {
100 | newCapacity <<= 1;
101 | if (newCapacity < 0) {
102 | return Integer.MAX_VALUE;
103 | }
104 | }
105 | return newCapacity;
106 | }
107 |
108 | public static final boolean stepBuffer(ByteBuffer buffer, int remaining) {
109 | if (buffer.remaining() >= remaining) {
110 | buffer.position(buffer.position() + remaining);
111 | return true;
112 | } else {
113 | return false;
114 | }
115 | }
116 |
117 | public static String getString(byte[] bytes) {
118 | try {
119 | return new String(bytes, DEFAULT_CHARSET_NAME);
120 | } catch (UnsupportedEncodingException e) {
121 | throw new RuntimeException(e);
122 | }
123 | }
124 |
125 | public static void byte2hex(byte b, StringBuffer buf) {
126 | char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
127 | 'A', 'B', 'C', 'D', 'E', 'F'};
128 | int high = ((b & 0xf0) >> 4);
129 | int low = (b & 0x0f);
130 | buf.append(hexChars[high]);
131 | buf.append(hexChars[low]);
132 | }
133 |
134 | public static void int2hex(int a, StringBuffer str) {
135 | str.append(Integer.toHexString(a));
136 | }
137 |
138 | public static void short2hex(int a, StringBuffer str) {
139 | str.append(Integer.toHexString(a));
140 | }
141 |
142 | public static void getBytes(long i, int index, byte[] buf) {
143 | long q;
144 | int r;
145 | int pos = index;
146 | byte sign = 0;
147 |
148 | if (i < 0) {
149 | sign = '-';
150 | i = -i;
151 | }
152 |
153 | // Get 2 digits/iteration using longs until quotient fits into an int
154 | while (i > Integer.MAX_VALUE) {
155 | q = i / 100;
156 | // really: r = i - (q * 100);
157 | r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
158 | i = q;
159 | buf[--pos] = DigitOnes[r];
160 | buf[--pos] = DigitTens[r];
161 | }
162 |
163 | // Get 2 digits/iteration using ints
164 | int q2;
165 | int i2 = (int) i;
166 | while (i2 >= 65536) {
167 | q2 = i2 / 100;
168 | // really: r = i2 - (q * 100);
169 | r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
170 | i2 = q2;
171 | buf[--pos] = DigitOnes[r];
172 | buf[--pos] = DigitTens[r];
173 | }
174 |
175 | // Fall thru to fast mode for smaller numbers
176 | // assert(i2 <= 65536, i2);
177 | for (; ; ) {
178 | q2 = (i2 * 52429) >>> (16 + 3);
179 | r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
180 | buf[--pos] = digits[r];
181 | i2 = q2;
182 | if (i2 == 0)
183 | break;
184 | }
185 | if (sign != 0) {
186 | buf[--pos] = sign;
187 | }
188 | }
189 |
190 | /**
191 | * Places characters representing the integer i into the character array
192 | * buf. The characters are placed into the buffer backwards starting with
193 | * the least significant digit at the specified index (exclusive), and
194 | * working backwards from there.
195 | *