java代码中静态读取文件信息进行初始化

java代码中静态读取文件信息进行初始化

1
2
3
Properties pro = new Properties();//专门读取properties文件
pro.load(ClassA.class.getClassLoader().getResourcesAsStream("classA.properties"));// 文件位于src目录下
String info = pro.getProperty("info");//获取properties文件中的info属性

基于分布式架构的旅游平台开发日志

基于分布式架构的旅游平台开发日志


2020.4.3

开始写开发日志

感觉有必要将问题都整理起来,不然以后遇到还是会捉急。

开发的进程也要写清楚,这样以后总结起来也会方便很多。

完成内容:

  • Feign的基本使用,并将Region Service和Hotel Service

  • Elastic Search的搜索功能方法基础实现完成,关键词搜索和地域搜索都完成了。

Kibana学习

Kibana学习

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
#获取所有索引库
GET _cat/indices?v

GET _search
{
"query": {
"match_all": {}
}
}

#get specific
GET /user/userinfo/6

#get all
GET /user/userinfo/_search

#Sort AND Page
GET /user/userinfo/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 3
}

#Specific Search
GET /user/userinfo/_search
{
"query": {
"match_all": {

}
}
}

#Create new indic
PUT /user

#Create mapping
PUT /user/userinfo/_mapping
{
"properties":{
"name":{
"type":"text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"store": false
},
"city":{
"type":"text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"store": false
},
"age":{
"type":"long",
"store": false
},
"description":{
"type":"text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"store": false
}
}
}

#Insert Info
PUT /user/userinfo/1
{
"name":"zhangsan",
"city":"Guangdong",
"age":20,
"description":"zhangsan is a boy."
}
#Insert Info
PUT /user/userinfo/2
{
"name":"lisi",
"city":"Shanghai",
"age":19,
"description":"lisi is a shanghai boy."
}
#Insert Info
PUT /user/userinfo/3
{
"name":"wangwu",
"city":"Guangdong",
"age":25,
"description":"wangwu is a guangdong handsome man."
}
#Insert Info
PUT /user/userinfo/4
{
"name":"xiaohong",
"city":"Beijing",
"age":15,
"description":"xiaohong is a Beijing girl."
}


#Insert Info
PUT /user/userinfo/5
{
"name":"kris",
"city":"American",
"age":17,
"description":"kris is a sicuan cool boy.",
"sex":"boy"
}

#Insert Info
#Or replace the original info
PUT /user/userinfo/6
{
"name":"xiaomei",
"city":"sicuan",
"age":18,
"description":"xiaomei is a sicuan hot girl."
}

#update the original info
POST /user/userinfo/6/_update
{
"doc": {
"name":"rio",
"age":23,
"description":"rio is a sicuan smart woman"
}
}

#deleted
DELETE /user/userinfo/6
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
#nested 查询
GET /hotel/_search
{
"query": {
"nested" : {
"path": "rooms",
"query": {
"match": {
"rooms.roomId": 1
}
}
}
}
}

GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "rooms",
"query": {
"match": {
"rooms.roomBedAdd": -1
}
}
}
}
]
}
}
}
GET /hotel/_search
{
"query": {
"match_all": {}
}
}

Java面试准备

Java面试题

  1. 操作数栈的题:
1
2
3
4
5
6
7
8
9
public static void main(String[] args){
int i = 1;
i = i++;
int j = i++;
ink k = i + ++i * i++;
System.out.println("i=" + i);
System.out.println("j=" + j);
System.out.println("k=" + k);
}

重点:字节码,i++,操作数栈,运算符优先级

=运算符是最后运算的

操作数栈压入参数的顺序是=后从左到右依次压入,而四则运算何时进行并不是依次的,而是要考虑运算符优先级。

+-*/四则运算需要将变量先压入操作数栈在取出操作数栈中的数进行运算后再将临时结果压入操作数栈中,等待取出结果。

++运算符是直接对局部变量表中的变量直接操作的,不经过操作数栈。

结果输出:

1
2
3
i = 4
j = 1
k = 11
  1. 单例模式Singleton

特点

  • 只能有一个实例
    • 构造器
  • 必须自行创建实例
    • 使用一个静态变量来保存这个唯一的实例
  • 必须自行向系统提供该实例
    • 直接暴露
    • 静态变量的get方法获取

常见形式:

  • 饿汉式:直接创建对象,不存在线程安全问题

    - 直接实例化饿汉式(简洁直观)
    
    1
    2
    3
    4
    5
    6
    7
    // 不考虑是否需要实例该单例
    // 静态变量
    public static final Singleton instance = new Singleton();
    // 构造器
    private Singleton(){

    }
    - 枚举式(最简洁)
    
    1
    2
    3
    4
    5
    // 表示该类型的对象是有限的几个
    public enum Singleton{
    INSTANCE1,
    INSTANCE2
    }
    - 静态代码块饿汉式(适合复杂实例化)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static final Singleton INSTANCE;
    static{
    INSTANCE = new Singleton();
    ...
    //需要进行一定的初始化
    //或者需要从文件中获取属性值
    }
    private Singleton(){

    }
  • 懒汉式:延迟创建对象

    - 线程不安全(适用于单线程)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    private static Singleton instance;
    private Singleton(){

    }
    public static Singleton getInstance(){
    if(instance == null){
    instance = new Singleton();
    }
    return instance;
    }
    - 线程安全(适用于多线程)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    private static Singleton instance;
    private Singleton(){

    }
    public static Singleton getInstance(){
    if(instance == null){
    synchronized (Singleton.class){
    if (instance == null){
    try{
    Thread.sleep(100);
    }catch(Exception e){
    e.printStackTrace();
    }

    instance = new Singleton();
    }
    }
    }
    return instance;
    }
    - 静态内部类形式(适用于多线程)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    private Singleton(){

    }
    // 在内部类加载和初始化时创建的,因此是线程安全的
    private static class Inner{
    private static final Singleton INSTANCE = new Singleton();
    }
    public static Singleton getInstance(){
    return Inner.INSTANCE;
    }