Skip to content

Commit 88f815c

Browse files
lizhen's commit
add the zookeeper to manange the config of mysql
1 parent 7ae9f39 commit 88f815c

File tree

6 files changed

+288
-17
lines changed

6 files changed

+288
-17
lines changed

pom.xml

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-data-jpa</artifactId>
3535
</dependency>
36-
36+
3737
<dependency>
3838
<groupId>org.springframework.boot</groupId>
3939
<artifactId>spring-boot-starter-data-redis</artifactId>
40-
</dependency>
41-
40+
</dependency>
41+
4242
<dependency>
4343
<groupId>org.springframework.boot</groupId>
4444
<artifactId>spring-boot-starter-web</artifactId>
@@ -49,31 +49,56 @@
4949
<artifactId>mysql-connector-java</artifactId>
5050
<scope>runtime</scope>
5151
</dependency>
52-
52+
5353
<!--这里是增加的cache依赖 -->
5454
<dependency>
55-
<groupId>org.springframework.boot</groupId>
56-
<artifactId>spring-boot-starter-cache</artifactId>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-cache</artifactId>
5757
</dependency>
5858

5959
<dependency>
6060
<groupId>org.springframework.boot</groupId>
6161
<artifactId>spring-boot-starter-thymeleaf</artifactId>
6262
</dependency>
63-
63+
6464
<dependency>
65-
<groupId>org.springframework.boot</groupId>
66-
<artifactId>spring-boot-starter-security</artifactId>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-security</artifactId>
6767
</dependency>
68-
68+
6969
<dependency>
7070
<groupId>org.springframework.boot</groupId>
7171
<artifactId>spring-boot-starter-test</artifactId>
7272
<scope>test</scope>
7373
</dependency>
74+
75+
<dependency>
76+
<groupId>com.vpgame.vendor</groupId>
77+
<artifactId>core</artifactId>
78+
<version>1.0.2-SNAPSHOT</version>
79+
</dependency>
80+
81+
82+
<dependency>
83+
<groupId>org.apache.zookeeper</groupId>
84+
<artifactId>zookeeper</artifactId>
85+
<version>3.4.8</version>
86+
</dependency>
87+
7488
</dependencies>
7589

7690
<build>
91+
<resources>
92+
<resource>
93+
<filtering>true</filtering>
94+
<!-- 这里的${project.basedir}可能有问题,等测试后看看有没有问题 -->
95+
<directory>${project.basedir}/src/main/resources</directory>
96+
<includes>
97+
<include>*.properties</include>
98+
</includes>
99+
</resource>
100+
</resources>
101+
77102
<plugins>
78103
<plugin>
79104
<groupId>org.springframework.boot</groupId>
@@ -82,5 +107,14 @@
82107
</plugins>
83108
</build>
84109

110+
<!-- 这里定义统一的多环境开发,我在这里值弄了一个本地的,现实中会有测试的和product的 -->
111+
<profiles>
112+
<profile>
113+
<id>local</id>
114+
<properties>
115+
<zookeeper-url>127.0.0.1:2181</zookeeper-url>
116+
</properties>
117+
</profile>
118+
</profiles>
85119

86120
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.springboot.demo.config;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ConfigurationProperties(prefix = "com.springboot.demo")
8+
public class CustomProperty {
9+
private String zookeeperUrl;
10+
11+
public String getZookeeperUrl() {
12+
return zookeeperUrl;
13+
}
14+
15+
public void setZookeeperUrl(String zookeeperUrl) {
16+
this.zookeeperUrl = zookeeperUrl;
17+
}
18+
19+
}
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.springboot.demo.config;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.annotation.Resource;
37
import javax.sql.DataSource;
48

59
import org.springframework.beans.factory.annotation.Qualifier;
@@ -9,14 +13,35 @@
913
import org.springframework.context.annotation.Configuration;
1014
import org.springframework.context.annotation.Primary;
1115

16+
import com.vpgame.core.config.MysqlDbConfig;
17+
import com.vpgame.core.utils.ZkConfigClient;
18+
1219
@Configuration
1320
public class DataSourceConfig {
1421

22+
@Resource
23+
private CustomProperty customProperty;
24+
1525
@Bean(name="primaryDataSource")
1626
@Qualifier("primaryDataSource")
1727
@ConfigurationProperties(prefix="spring.datasource.primary")
1828
public DataSource primaryDataSource(){
19-
return (DataSource) DataSourceBuilder.create().build();
29+
//这里的数据我想直接从zookeeper中的节点中获取,这里我使用峻峰已经封装好的zkclient类来链接本地的zk
30+
//这里获取一个新的zk客户端连接zk
31+
ZkConfigClient zkConfigClient = new ZkConfigClient(customProperty.getZookeeperUrl());
32+
//下面通过ZkConfigClient中的getMysqlConfig
33+
MysqlDbConfig mysqlConfig = zkConfigClient.getMysqlDbConfig("springboot");
34+
35+
//下面的配置是什么我暂时还不知道
36+
Map<String, String> dbUriParam = new HashMap<>();
37+
dbUriParam.put("zeroDateTimeBehavior", "convertToNull");
38+
//下面开始生成相应的datasource
39+
return (DataSource) DataSourceBuilder.create()
40+
//这里创建DataSource时,可以在这里面加入获取的数据库的url,username,password等配置数据
41+
.url(MysqlDbUtil.generateDbUrl(mysqlConfig,dbUriParam))
42+
.username(mysqlConfig.getUsername())
43+
.password(mysqlConfig.getPassword())
44+
.build();
2045
}
2146

2247

@@ -25,7 +50,7 @@ public DataSource primaryDataSource(){
2550
@Primary
2651
@ConfigurationProperties(prefix="spring.datasource.secondary")
2752
public DataSource secondaryDataSource(){
28-
return (DataSource) DataSourceBuilder.create().build();
53+
return (DataSource) DataSourceBuilder.create()
54+
.build();
2955
}
30-
3156
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//package com.springboot.demo.config;
2+
//
3+
//import java.io.IOException;
4+
//import java.util.List;
5+
//import java.util.concurrent.CountDownLatch;
6+
//
7+
//import org.apache.zookeeper.CreateMode;
8+
//import org.apache.zookeeper.KeeperException;
9+
//import org.apache.zookeeper.WatchedEvent;
10+
//import org.apache.zookeeper.Watcher;
11+
//import org.apache.zookeeper.Watcher.Event.KeeperState;
12+
//import org.apache.zookeeper.ZooDefs.Ids;
13+
//import org.apache.zookeeper.ZooKeeper;
14+
//import org.apache.zookeeper.data.Stat;
15+
//
16+
///**
17+
// * ]
18+
// *
19+
// * @author <a href="mailto:[email protected]>LZ</a>
20+
// * @data 2017年7月5号
21+
// *
22+
// */
23+
//
24+
//public class GenerateZKNodeAndData implements Watcher {
25+
// /**
26+
// * 生成本地的ZKnode和相应的数据库数据
27+
// */
28+
// public ZooKeeper zookeeper;
29+
// private static int SESSION_TIME_OUT = 2000;
30+
// private CountDownLatch countDownLatch = new CountDownLatch(1);
31+
//
32+
// /**
33+
// * 连接zookeeper
34+
// *
35+
// * @param host
36+
// * @throws IOException
37+
// * @throws InterruptedException
38+
// */
39+
// public void connectZookeeper(String host) throws IOException, InterruptedException {
40+
// zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
41+
// countDownLatch.await();
42+
// System.out.println("zookeeper connect ok");
43+
// }
44+
//
45+
// /**
46+
// * 实现watcher的接口方法,当连接zookeeper成功后,zookeeper会通过此方法通知watcher
47+
// * 此处为如果接受到连接成功的event,则countDown,让当前线程继续其他事情。
48+
// */
49+
// @Override
50+
// public void process(WatchedEvent event) {
51+
// if (event.getState() == KeeperState.SyncConnected) {
52+
// System.out.println("watcher receiver event");
53+
// countDownLatch.countDown();
54+
// }
55+
// }
56+
//
57+
// /**
58+
// * 根据路径创建节点,并且设置节点数据
59+
// *
60+
// * @param path
61+
// * @param data
62+
// * @return
63+
// * @throws KeeperException
64+
// * @throws InterruptedException
65+
// */
66+
// public String createNode(String path, byte[] data) throws KeeperException, InterruptedException {
67+
// return this.zookeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
68+
// }
69+
//
70+
// /**
71+
// * 根据路径获取所有孩子节点
72+
// *
73+
// * @param path
74+
// * @return
75+
// * @throws KeeperException
76+
// * @throws InterruptedException
77+
// */
78+
// public List<String> getChildren(String path) throws KeeperException, InterruptedException {
79+
// return this.zookeeper.getChildren(path, false);
80+
// }
81+
//
82+
// public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
83+
// return this.zookeeper.setData(path, data, version);
84+
// }
85+
//
86+
// /**
87+
// * 根据路径获取节点数据
88+
// *
89+
// * @param path
90+
// * @return
91+
// * @throws KeeperException
92+
// * @throws InterruptedException
93+
// */
94+
// public byte[] getData(String path) throws KeeperException, InterruptedException {
95+
// return this.zookeeper.getData(path, false, null);
96+
// }
97+
//
98+
// /**
99+
// * 删除节点
100+
// *
101+
// * @param path
102+
// * @param version
103+
// * @throws InterruptedException
104+
// * @throws KeeperException
105+
// */
106+
// public void deleteNode(String path, int version) throws InterruptedException, KeeperException {
107+
// this.zookeeper.delete(path, version);
108+
// }
109+
//
110+
// /**
111+
// * 关闭zookeeper连接
112+
// *
113+
// * @throws InterruptedException
114+
// */
115+
// public void closeConnect() throws InterruptedException {
116+
// if (null != zookeeper) {
117+
// zookeeper.close();
118+
// }
119+
// }
120+
//
121+
// // 下面是定义主方法,然后运行该类去生成相应的ZKnode和相应的数据
122+
// public static void main(String args[]) throws IOException, InterruptedException, KeeperException {
123+
// // 建立一个客户端连接ZK
124+
// GenerateZKNodeAndData client = new GenerateZKNodeAndData();
125+
// String host = "localhost:2181";
126+
//
127+
// // 下面开始连接ZK
128+
// client.connectZookeeper(host);
129+
//
130+
// // 连接好了以后现在开始创建节点和数据,这里的数据我在这里先定义好,这里给出的接口里必须放byte类型的数组,
131+
// //这里创建一次过后,就不要在创建了,否则会报异常,所以这里我将创建节点的代码注释了
132+
// String str = "{\"username\":\"lizhen\",\"password\":\"lizhen\",\"host\":\"127.0.0.1\",\"dbname\":\"springboot\",\"port\":3306}";
133+
// byte[] data = str.getBytes();
134+
// //String isCreate = client.createNode("/config/mysqldb/springboot", data);
135+
// //因为之前弄的数据库密码弄成lizhen1992815了,正确是lizhen,所以这里需要将节点的数据set一下,经过测试,目前数据已经修改过来了,现在将更新数据的代码注释掉,否则每更新一次对应的dataversion就会增加一次
136+
//// Stat stat = client.setData("/config/mysqldb/springboot", data, 0);
137+
//// // 这里返回节点的路径
138+
//// System.out.println(stat);
139+
//
140+
// // 下面是从zk中获取数据
141+
// byte[] mydata = client.getData("/config/mysqldb/springboot");
142+
// String result = new String(mydata);
143+
// System.out.println(result);
144+
// }
145+
//}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.springboot.demo.config;
2+
3+
import java.util.Map;
4+
import java.util.Map.Entry;
5+
6+
import com.vpgame.core.config.MysqlDbConfig;
7+
8+
/**
9+
*
10+
* @author <a href="mailto:[email protected]">LZ</a>
11+
*@data 2017年7月5号
12+
*/
13+
14+
public class MysqlDbUtil {
15+
/**
16+
* 生成mysql dburl
17+
*
18+
* @param dbConfig zk中的mysql配置项
19+
* @param extraParams 额外的数据库配置参数
20+
* @return mysql连接url
21+
*/
22+
public static String generateDbUrl(MysqlDbConfig mysqlConfig,Map<String,String> extraParams){
23+
//下面定义一个变量用来存放url
24+
StringBuilder url = new StringBuilder("jdbc:mysql://");
25+
url.append(mysqlConfig.getHost());
26+
url.append(":");
27+
url.append(mysqlConfig.getPort());
28+
url.append("/");
29+
url.append(mysqlConfig.getDbname());
30+
url.append("?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
31+
32+
if(null != extraParams){
33+
for(Entry<String, String> entry : extraParams.entrySet()) {
34+
url.append("&").append(entry.getKey()).append("=").append(entry.getValue());
35+
}
36+
}
37+
return url.toString();
38+
}
39+
40+
public static String generateDbUrl(MysqlDbConfig mysqlDbConfig){
41+
return generateDbUrl(mysqlDbConfig,null);
42+
}
43+
}

src/main/resources/application.properties

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
server.port=8080
22

33
#使用多数据库
4-
#第一个数据哭链接的参数
5-
spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
6-
spring.datasource.primary.username=lizhen
7-
spring.datasource.primary.password=lizhen
4+
#第一个数据库链接的参数
5+
#spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
6+
#spring.datasource.primary.username=lizhen
7+
#spring.datasource.primary.password=lizhen
8+
9+
10+
#这里使用zookeeper中配置信息代替上面的信息,这里的前缀在相应的Customproperty的配置类上制定
11+
com.springboot.demo.zookeeper-url = @zookeeper-url@
812
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
913

1014

15+
1116
#第二个数据库连接的参数
1217
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/springboot1?useUnicode=true&characterEncoding=UTF-8&useSSL=false
1318
spring.datasource.secondary.username=lizhen

0 commit comments

Comments
 (0)