海露的个人博客

海露的个人博客


  • 首页

  • 分类

  • 归档

  • Search

用Java对象构建Json语法树

Posted on 2021-01-25 | In 递归处理JSON
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author hailou
* @Description
* @Date on 2020/9/18 16:30
*/

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Json implements Comparable<Json> {
// There are value types
private int Type_String = 1;
private int Type_Array = 2;
private int Type_List = 3;
// Key always is String
private String key;
// There are three types of value
private int valueType;
private String valueString;
private List<Json> valueArray;// 本质一致,表现不同
private List<Json> valueList;
// indent depth
private int depth;

public Json(String key, String value) {
this.key = key;
this.valueType = Type_String;
this.valueString = value;
this.depth = 0;
}
public Json(String key, int type) {
this.key = key;

if (type == Type_List) {
this.valueType = Type_List;
this.valueList = new LinkedList<Json>();
} else if (type == Type_Array) {
this.valueType = Type_Array;
this.valueArray = new LinkedList<Json>();
}
}

public void addJsonToList(Json json) {
if (valueList != null) {
valueList.add(json);

adjustDepth();
}
}

public void addJsonToArray(Json json) {
if (valueArray != null) {
valueArray.add(json);

adjustDepth();
}
}

private void adjustDepth() {
if (valueType == Type_List) {
for (Json json : valueList) {
json.depth = this.depth + 1;
json.adjustDepth();
}
}

if (valueType == Type_Array) {
for (Json json : valueArray) {
json.depth = this.depth + 1;
json.adjustDepth();
}
}
}

public String toString() {
StringBuilder sb = new StringBuilder();
// key
String tabs = getIndentSpace();
sb.append(tabs);
//sb.append("\""+(key==null?"":key)+"\"");
if (key != null) {
sb.append("\"" + key + "\"");
sb.append(":");
} else {

}
// value
if (valueType == Type_String) {
sb.append("\"" + valueString + "\"");
} else if (valueType == Type_Array) {
sb.append("[\n");
int n = valueArray.size();
for (int i = 0; i < n; i++) {
Json json = valueArray.get(i);
if (i != n - 1) {
sb.append(json.toString() + ",\n");
} else {
sb.append(json.toString() + "\n");
}
}
sb.append(tabs + "]");
} else if (valueType == Type_List) {

sb.append("{\n");
Collections.sort(valueList);
int n = valueList.size();

for (int i = 0; i < n; i++) {
Json json = valueList.get(i);
if (i != n - 1) {
sb.append(json.toString() + ",\n");
} else {
sb.append(json.toString() + "\n");
}
}
sb.append(tabs + "}");
}
return sb.toString();
}

public int compareTo(Json other) {
return this.key.compareTo(other.key);
}

private String getIndentSpace() {
return String.join("", Collections.nCopies(this.depth, " "));
}

public static void main(String[] args) {
Json id1 = new Json("id", "001");
Json name1 = new Json("name", "白菜");

Json title = new Json("title", 3);
title.addJsonToList(id1);
title.addJsonToList(name1);

Json empty1 = new Json(null, 3);
empty1.addJsonToList(new Json("id", "001"));
empty1.addJsonToList(new Json("id", "你好白菜"));

Json empty2 = new Json(null, 3);
empty2.addJsonToList(new Json("id", "001"));
empty2.addJsonToList(new Json("id", "你好萝卜"));

Json content = new Json("content", 2);
content.addJsonToArray(empty1);
content.addJsonToArray(empty2);

Json data = new Json("data", 3);
data.addJsonToList(title);
data.addJsonToList(content);

Json status = new Json("status", "0000");
Json message = new Json("message", "success");

Json root = new Json(null, 3);
root.addJsonToList(status);
root.addJsonToList(message);
root.addJsonToList(data);

System.out.println(root.toString());
}
}

抓取json中的任意节点的数据

Posted on 2021-01-25 | In 递归处理JSON
依赖的jar
1
2
3
4
5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>

工具类

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
* @author hailou
* @Description
* @Date on 2020/9/16 15:57
*/
public class JacksonUtils {

private static final ObjectMapper MAPPER = new ObjectMapper();
private static final JacksonUtils CONVERSION = new JacksonUtils();
private static final String SEPARATOR = "\\.";

private JacksonUtils() {
init();
}

private static void init() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
MAPPER.setDateFormat(dateFormat);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}


public static JacksonUtils getInstance() {
return CONVERSION;
}

public String pojo2Json(Object obj) throws IOException {
StringWriter writer = new StringWriter();
MAPPER.writeValue(writer, obj);

return writer.toString();
}

public <T> T json2Pojo(InputStream jsonStream, Class<T> classType) throws IOException {
T t = null;

try (InputStream inputStream = jsonStream) {
t = MAPPER.readValue(inputStream, classType);
}

return t;
}

public <T> T json2Pojo(String json, Class<T> classType) {
try {
return MAPPER.readValue(json, classType);
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

/**
* 将JSON转化为List
*
* @param json 字符串
* @param elementClasses 元素类
* @return List 转换后的对象
*/
public static List<?> jsonConverList(String json, Class<?>... elementClasses) {
try {
return MAPPER.readValue(json, getCollectionType(List.class, elementClasses));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 获取泛型的Collection Type
*
* @param collectionClass 泛型的Collection
* @param elementClasses 元素类
* @return JavaType Java类型
*/
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}

public <T> T map2Pojo(Map<String, Object> map, Class<T> classType) throws IOException {
return MAPPER.convertValue(map, classType);
}

public <T> T jsonParse(String json) throws IOException {
return MAPPER.readValue(json, new TypeReference<T>() {});
}

/**
* path: Like xpath, to find the specific value via path. Use :(Colon) to separate different key
* name or index. For example: JSON content: { "name": "One Guy", "details": [
* {"education_first": "xx school"}, {"education_second": "yy school"}, {"education_third":
* "zz school"}, ... ] }
*
* To find the value of "education_second", the path="details:1:education_second".
*
* @param json
* @param path
* @return
*/
public String fetchValue(String json, String path) {
JsonNode tempNode = null;
try {
JsonNode jsonNode = MAPPER.readTree(json);
tempNode = jsonNode;
String[] paths = path.split(SEPARATOR);

for (String fieldName : paths) {
if (tempNode.isArray()) {
tempNode = fetchValueFromArray(tempNode, fieldName);
} else {
tempNode = fetchValueFromObject(tempNode, fieldName);
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (tempNode != null) {
String value = tempNode.asText();

if (value == null || value.isEmpty()) {
value = tempNode.toString();
}
return value;
}
return null;
}

private JsonNode fetchValueFromObject(JsonNode jsonNode, String fieldName) {
return jsonNode.get(fieldName);
}

static List<String> restList;

private JsonNode fetchValueFromArray(JsonNode jsonNode, String index) {
try{
return jsonNode.get(Integer.parseInt(index));
}catch (Exception e){
restList = jsonNode.findValuesAsText(index) ;
return null;
}
//return jsonNode.get(Integer.parseInt(index));
}

public JsonNode convertStr2JsonNode(String json) {
try {
return MAPPER.readTree(json);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}


public static void main(String[] args) {
String aa = "{\n" +
"\t\"org\": {\n" +
"\t\t\"id\": [\"aa\", \"bb\"],\n" +
"\t\t\"name\": \"机构树\",\n" +
"\t\t\"children\": [{\n" +
"\t\t\t\"name\": \"信息办\",\n" +
"\t\t\t\"children\": [{\n" +
"\t\t\t\t\"name\": \"网站组\",\n" +
"\t\t\t\t\"age\": \"24\"\n" +
"\t\t\t}, {\n" +
"\t\t\t\t\"name\": \"OA组\",\n" +
"\t\t\t\t\"age\": \"24\"\n" +
"\t\t\t}]\n" +
"\t\t}, {\n" +
"\t\t\t\"name\": \"造林司\",\n" +
"\t\t\t\"children\": [{\n" +
"\t\t\t\t\"name\": \"植树组\"\n" +
"\t\t\t}, {\n" +
"\t\t\t\t\"name\": \"退根组\"\n" +
"\t\t\t}]\n" +
"\t\t}]\n" +
"\t}\n" +
"}";

//String res = JacksonUtils.getInstance().fetchValue(aa, "org:children:0:children:1");
String res = JacksonUtils.getInstance().fetchValue(aa, "org.children");
//String res = JacksonUtils.getInstance().fetchValue(aa, "org.id");
System.out.println("res = " + res);
System.out.println("restList = " + restList);
}
}

对json数据key进行替换

Posted on 2021-01-25 | In 递归处理JSON
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
/**
* @author hailou
* @Description 对json数据key进行替换
* @Date on 2020/9/15 17:19
*/

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class JSONUtil {

public static JSONObject changeJsonObj(JSONObject jsonObj,Map<String, String> keyMap) {
JSONObject resJson = new JSONObject();
Set<String> keySet = jsonObj.keySet();
for (String key : keySet) {
String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
try {
JSONObject jsonobj1 = jsonObj.getJSONObject(key);
resJson.put(resKey, changeJsonObj(jsonobj1, keyMap));
} catch (Exception e) {
try {
JSONArray jsonArr = jsonObj.getJSONArray(key);
resJson.put(resKey, changeJsonArr(jsonArr, keyMap));
} catch (Exception x) {
resJson.put(resKey, jsonObj.get(key));
}
}
}
return resJson;
}

public static JSONArray changeJsonArr(JSONArray jsonArr,Map<String, String> keyMap) {
JSONArray resJson = new JSONArray();
for (int i = 0; i < jsonArr.size(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
resJson.add(changeJsonObj(jsonObj, keyMap));
}
return resJson;
}

public static void main(String[] args) {
String jsonStr = "{\"user\":{\"name\":\"张三\",\"sex\":\"男\",\"hobby\":[{\"motion\":\"足球\",\"desc\":\"任性\"},{\"game\":\"英雄联盟\",\"desc\":\"就是这么任性\"}]}}";
Map<String, String> keyMap = new HashMap<String, String>();
keyMap.put("name", "XingMing");
keyMap.put("user", "YongHu");
keyMap.put("desc", "MiaoShu");
JSONObject jsonObj = JSONUtil.changeJsonObj(JSONObject.parseObject(jsonStr),keyMap);
System.out.println("换值结果 》》 " + jsonObj.toString());
}
}

JSON增加节点或者删除指定key的值

Posted on 2021-01-25 | In 递归处理JSON
工具类
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
70
71
72
/**
* @author hailou
* @Description JSON增加节点或者删除指定key的值
* @Date on 2020/9/18 11:09
*/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class JsonAddOrRemoveUtils {
public static JSONObject addOrRemoveNodeByKeys(String json,String[] removeKeys, Map<String,Object> addNodes) {
JSONObject objTem=JSON.parseObject(json);
JSONObject objRel=JSON.parseObject(json);
return dealOrCreate(objTem,objRel,removeKeys,addNodes);
}
private static JSONObject dealOrCreate(JSONObject objTem, JSONObject objRel, String[] removeKeys, Map<String,Object> addNodes) {
/**
下面这段代码是批量增加节点时从当前的某个节点获取一个值塞进去
如:addNodes.put("key1","type");type就是当明节点的的一个key
把当前的key的值付给自定义节点的值,可能会做页面映射用
*/
/*if(null != addNodes && !addNodes.keySet().isEmpty()){
Set<String> keys = addNodes.keySet();
Iterator<String> iteratorKeys = keys.iterator();
while(iteratorKeys.hasNext()) {
String key = iteratorKeys.next();
if(objTem.get(addNodes.get(key))!=null){
objRel.put(key,objTem.get(addNodes.get(key)));
}
}
}*/

/**
* 这个就是把自定义节点插入进去,和上面代码取其一
*/
if(null != addNodes && !addNodes.keySet().isEmpty()){
objRel.putAll(addNodes);
}

Set<String> keySet = objTem.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String temp = iterator.next();//存key
Object objR = objTem.get(temp);//存value
for (int i = 0; i<removeKeys.length ; i++) {
if(removeKeys[i].equals(temp)) {
objRel.remove(temp);
continue;
}
}
if(objR instanceof JSONObject) {
JSONObject j=(JSONObject)objR;
JSONObject object2 = (JSONObject)objRel.get(temp);
dealOrCreate(j,object2,removeKeys,addNodes);
continue;
}
if(objR instanceof JSONArray) {
JSONArray jsonArray = objTem.getJSONArray(temp);
JSONArray jsonArray2 = objRel.getJSONArray(temp);
for(int i=0;i<jsonArray.size();i++) {
dealOrCreate(jsonArray.getJSONObject(i),jsonArray2.getJSONObject(i),removeKeys,addNodes);
}
}
}
return objRel;
}
}

测试
1
2
3
4
5
6
7
8
9
10
11
//原始json,注意这里的json只是对象开头的,如果是[]开头的会报错
String json = "{\"aa\":[{\"name\":\"aaa\",\"type\":\"list\",\"parentId\":\"0\",\"seat\":\"1\",\"children\":[{\"name\":\"bbb\",\"type\":\"base\",\"parentId\":\"1\",\"seat\":\"1-1\",\"children\":[]},{\"name\":\"ccc\",\"type\":\"base\",\"parentId\":\"1\",\"seat\":\"1-2\",\"children\":[]}]},{\"name\":\"ddd\",\"type\":\"map\",\"parentId\":\"0\",\"seat\":\"2\",\"children\":[{\"name\":\"eee\",\"type\":\"base\",\"parentId\":\"2\",\"seat\":\"2-1\",\"children\":[]},{\"name\":\"fff\",\"type\":\"map\",\"parentId\":\"2\",\"seat\":\"2-3\",\"children\":[{\"name\":\"ggg\",\"type\":\"base\",\"parentId\":\"2-3\",\"seat\":\"2-3-1\",\"children\":[]}]}]}]}";
System.out.println("json = " + json);
//要删除的节点
String[] removeNames = {"seat","parentId"};
//调用删除方法
Map<String,Object> addNodes = new HashMap<>();
addNodes.put("key1","key1-value");
addNodes.put("key2","key2-value");
JSONObject node = JsonAddOrRemoveUtils.addOrRemoveNodeByKeys(jsonOutput,removeNodeNames,addNodes);
System.out.println("jsonOutput = " + node);

JSON删除key或者value中的空值

Posted on 2021-01-25 | In 递归处理JSON
maven中的依赖
1
2
3
4
5
6
<!-- 阿里fastjson包JSON转换-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

工具类

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
/**
* @author hailou
* @Description JSON删除key或者value中的空值
* @Date on 2020/9/18 11:09
*/
import java.util.Iterator;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JsonDealUtils {
public static JSONObject getNoNullValue(String json) {
JSONObject objTem=JSON.parseObject(json);
JSONObject objRel=JSON.parseObject(json);
return deal(objTem,objRel);
}
private static JSONObject deal(JSONObject objTem,JSONObject objRel) {
Set<String> keySet = objTem.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String temp = iterator.next();//存key
Object objR = objTem.get(temp);//存value
if(temp==null||"".equals(temp)||"null".equals(temp)) {
objRel.remove(temp);
continue;
}
if(objR==null||"".equals(objR.toString())||"null".equals(objR.toString())||"[]".equals(objR.toString())||"{}".equals(objR.toString())) {
objRel.remove(temp);
continue;
}
if(objR instanceof JSONObject) {
JSONObject j=(JSONObject)objR;
JSONObject object2 = (JSONObject)objRel.get(temp);
deal(j,object2);
continue;
}
if(objR instanceof JSONArray) {
JSONArray jsonArray = objTem.getJSONArray(temp);
JSONArray jsonArray2 = objRel.getJSONArray(temp);
for(int i=0;i<jsonArray.size();i++) {
deal(jsonArray.getJSONObject(i),jsonArray2.getJSONObject(i));
}
}
}
return objRel;
}
}

Java删除数据库中的树形数据及下面的子数据

Posted on 2021-01-25 | In 递归处理JSON
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
public void deleteContentCategory(Long parentId, Long id) {
// 声明存放需要删除的节点的容器
List<Object> ids = new ArrayList<>();
// 把自己的id放到ids中
ids.add(id);
// 使用递归获取所有需要删除的节点的id
this.getIds(id, ids);
// 使用父类的方法,批量删除
super.deleteByIds(ids);
// 查询兄弟节点,声明查询条件
ContentCategory param = new ContentCategory();
param.setParentId(parentId);
// 执行查询
int count = super.queryCountByWhere(param);
// 判断是否没有兄弟节点
if (count == 0) {
// 如果没有兄弟节点
ContentCategory parent = new ContentCategory();
parent.setId(parentId);
// 修改父节点的isParent为false
parent.setIsParent(false);
// 执行修改
super.updateByIdSelective(parent);
}
// 如果还有兄弟节点,神马都不做
}

// 使用递归的方式查询所有的子节点的id
private void getIds(Long id, List<Object> ids) {
// 根据条件查询当前节点的所有的子节点
ContentCategory param = new ContentCategory();
param.setParentId(id);
List<ContentCategory> list = super.queryListByWhere(param);
// 使用递归的方式,必须设置递归的停止条件,否则会一直自己调用自己,直到内存溢出
// 判断是否还有子节点
if (list.size() > 0) {
// 如果有子节点,遍历结果集
for (ContentCategory son : list) {
// 1.把子节点的id放到ids容器中
ids.add(son.getId());
// 2.执行递归,自己调用自己,查询子节点的子
this.getIds(son.getId(), ids);
}
}
}

java递归list变成树结构的形式

Posted on 2020-09-20 | In 递归处理JSON
实体类
1
2
3
4
5
6
7
8
9
10
public class ObjectJSONFormat {

private String id;
private String name;
private String type;
private String parentId;

private List<ObjectJSONFormat> children = new ArrayList<>();

}
树结构的工具类
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
/**
* @author hailou
* @Description 树结构的工具类
* @Date on 2020/9/17 9:27
*/
public class ObjectJSONFormatTree {

private List<ObjectJSONFormat> list = new ArrayList<>();

public ObjectJSONFormatTree(List<ObjectJSONFormat> list) {
this.list = list;
}

//建立树形结构
public List<ObjectJSONFormat> builTree() {
List<ObjectJSONFormat> tree = new ArrayList<>();
for (ObjectJSONFormat node : getRootNode()) {
node = buildChilTree(node);
tree.add(node);
}
return tree;
}

//递归,建立子树形结构
private ObjectJSONFormat buildChilTree(ObjectJSONFormat pNode) {
List<ObjectJSONFormat> childrens = new ArrayList<>();
for (ObjectJSONFormat node : list) {
if (node.getParentId().equals(pNode.getId())) {
childrens.add(buildChilTree(node));
}
}
pNode.setChildren(childrens);
return pNode;
}

//获取根节点
private List<ObjectJSONFormat> getRootNode() {
List<ObjectJSONFormat> rootLists = new ArrayList<>();
for (ObjectJSONFormat node : list) {
if (node.getParentId().equals("0")) {
rootLists.add(node);
}
}
return rootLists;
}

}
测试
1
2
3
4
5
6
7
8
//业务代码获取formatList,这里直接new一个,生产是获取的
List<ObjectJSONFormat> formatList = new ArrayList<>();
formatList.add(...) //若干ObjectJSONFormat
//创建树
ObjectJSONFormatTree formatTree = new ObjectJSONFormatTree(formatList);
formatList = formatTree.builTree();
String jsonOutput = JacksonUtils.getInstance().pojo2Json(formatList);
System.out.println(jsonOutput);

解决nginx做反向代理后,端口丢失问题

Posted on 2020-07-30 | In 环境部署

在部署微服务项目中,发现nginx做反向代理后,端口丢失,以为项目代码出错,最后发现是nginx配置的问题

Read more »

sql语句书写小技巧

Posted on 2020-07-21 | In 开发总结
sql语句书写顺序
       select--->from--->where--->group by--->having--->order by
sql语句解析顺序
     from--->where--->group by---> having--->select--->order by
Read more »

minio利用docker安装集群环境

Posted on 2020-07-14 | In 环境部署

两台虚拟机

192.168.181.132(data,data1)

192.168.181.133(data,data1)

执行以下命令:

node1

1
docker run -d  --net=host --restart=always --name minio-node1 -e "MINIO_ACCESS_KEY=minio"   -e"MINIO_SECRET_KEY=minio123"   -v /data:/data -v /data1:/data1   minio/minio server http://192.168.181.132:9000/data/ http://192.168.181.132:9000/data1/ http://192.168.181.133:9000/data/ http://192.168.181.133:9000/data1/

node2

1
docker run -d  --net=host --restart=always --name minio-node2 -e "MINIO_ACCESS_KEY=minio"   -e"MINIO_SECRET_KEY=minio123"   -v /data:/data -v /data1:/data1   minio/minio server http://192.168.181.132:9000/data/ http://192.168.181.132:9000/data1/ http://192.168.181.133:9000/data/ http://192.168.181.133:9000/data1/

注意看这两个脚本不同之处只有一个地方:–name启的别名不一样,其它一模一样。这里我用–net=host可以创建但是改成-p 9000:9000就不通了。

这样访问:http://192.168.181.132:9000/给里面上传文件 ,另一个节点自动同步。

12…4

35 posts
6 categories
18 tags
© 2021 John Doe
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4