問題 選項、返回結果、結果彙總報告
<!-- fastjson 儘量保持更新,預防bug-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.74</version>
</dependency>
<!-- guava支援-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<!-- 程式碼生成lombok,需要配合idea外掛-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
封裝統一返回,泛型
跨域問題
com.bennyrhys.kafka_study1.wechat.common.BaseResponseVO
package com.bennyrhys.kafka_study1.wechat.common;
import java.util.UUID;
import lombok.Data;
/**
* @author : bennyrhys
* @description : 公共返回物件
**/
@Data
public class BaseResponseVO<M> {
private String requestId;
private M result;
public static<M> BaseResponseVO success(){
BaseResponseVO baseResponseVO = new BaseResponseVO();
baseResponseVO.setRequestId(genRequestId());
return baseResponseVO;
}
public static<M> BaseResponseVO success(M result){
BaseResponseVO baseResponseVO = new BaseResponseVO();
baseResponseVO.setRequestId(genRequestId());
baseResponseVO.setResult(result);
return baseResponseVO;
}
private static String genRequestId(){
return UUID.randomUUID().toString();
}
}
com.bennyrhys.kafka_study1.wechat.common.CorsFilter
package com.bennyrhys.kafka_study1.wechat.common;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
/**
* @author : bennyryhs
* @description : 跨域問題解決
**/
@WebFilter(filterName = "CorsFilter")
@Configuration
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
}
json檔案讀取(object、array)型別
com.bennyrhys.kafka_study1.wechat.utils.FileUtils
package com.bennyrhys.kafka_study1.wechat.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Optional;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
/**
* @author : bennyrhys
* @description : 檔案工具類
**/
@Slf4j
public class FileUtils {
public static String readFile(String filePath) throws IOException {
@Cleanup
BufferedReader reader = new BufferedReader(
new FileReader(new File(filePath))
);
String lineStr = "";
StringBuffer stringBuffer = new StringBuffer();
while ((lineStr = reader.readLine()) != null) {
stringBuffer.append(lineStr);
}
return stringBuffer.toString();
}
public static Optional<JSONObject> readFile2JsonObject(String filePath){
try {
String fileContent = readFile(filePath);
log.info("readFile2Json fileContent: [{}]" , fileContent);
return Optional.ofNullable(JSON.parseObject(fileContent));
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}
public static Optional<JSONArray> readFile2JsonArray(String filePath){
try {
String fileContent = readFile(filePath);
log.info("readFile2JsonArray fileContent: [{}]" , fileContent);
return Optional.ofNullable(JSON.parseArray(fileContent));
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}
}
實體類
com.bennyrhys.kafka_study1.wechat.conf.WeChatTemplateProperties
package com.bennyrhys.kafka_study1.wechat.conf;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Data
@Configuration
@ConfigurationProperties(prefix = "template")
/**
* 儲存模板調查結果
*/
public class WeChatTemplateProperties {
private List<WechatTemplate> templates;
private int templateResultType; // 結果型別 0-檔案 1-資料庫 2-es
private String templateResultFilePath; // 結果檔案路徑
/**
* 儲存模板
*/
@Data
public static class WechatTemplate {
private String templateId;
private String templateFilePath;
private boolean active;
}
}
template
[{
"questionId": "1",
"question": "今天幾號",
"answer": "",
"options": [
{"label": "1號", "value": "A"},
{"label": "2號", "value": "B"},
{"label": "3號", "value": "C"},
{"label": "4號", "value": "D"}
]},
{
"questionId": "2",
"question": "你喜愛的顏色",
"answer": "",
"options": [
{"label": "紅色", "value": "A"},
{"label": "黃色", "value": "B"},
{"label": "綠色", "value": "C"},
{"label": "紫色", "value": "D"}
]}
]
kafka_study1/src/main/resources/templates/templateResult.json
{
"templateId": "001",
"totalNumber": "102",
"statistics": [
{
"questionId": "1",
"question": "今天幾號",
"answers": [
{"label": "A", "value": 10},
{"label": "B", "value": 50},
{"label": "C", "value": 12},
{"label": "D", "value": 17}
]
},
{
"questionId": "2",
"question": "你喜愛的顏色",
"answers": [
{"label": "A", "value": 12},
{"label": "B", "value": 52},
{"label": "C", "value": 17},
{"label": "D", "value": 17}
]
}
]
}
application.yml
server:
port: 8080
template:
templates:
- {"templateId":"1", "templateFilePath":"E:/JAVA/kafka_study1/src/main/resources/templates/template.json", "active":true}
- {"templateId":"2", "templateFilePath":"E:/JAVA/kafka_study1/src/main/resources/templates/template.json", "active":false}
template-result-type: 0 # 結果型別 0-檔案 1-資料庫 2-es(注意駝峰命名改橫線小寫連結)
template-result-file-path: "E:/JAVA/kafka_study1/src/main/resources/templates/templateResult.json"
https://github.com/bennyrhys/kafka_study.git
查詢模板資訊
http://localhost:8080/v1/template
查詢模板統計結果
http://localhost:8080/v1/template/result
提交調查問卷結果
windows通過gitbash模擬linux環境 post
返回結果
requestId":「0725ef59-52fb-432c-aef5-4276cc25ae37」,「result」:null}
-- 查詢模板資訊
curl -XGET http://localhost:8080/v1/template
-- 查詢模板統計結果
curl -XGET http://localhost:8080/v1/template/result
-- 傳入調查問卷結果
curl -XPOST -H "Content-Type:application/json; charset=UTF-8" http://localhost:8080/v1/template/report -d \
'{
templateId:"001",
result:[
{"questionId":"1","question":"今天幾號","answer":"A"},
{"questionId":"2","question":"你喜愛的顏色","answer":"B"}
]
}'
netstat -ano | findstr 443
kafka的Producer是執行緒安全的,實際生成環境不建議Producer建立多回。
如果多執行緒情況下建立多個producer,效能消耗比想象中消耗要大。甚至不如單執行緒。
因為producer的send是 往中間佇列中存入資料, 後臺執行緒將資料批次的刷給伺服器。建立多個producer使頻繁的上下文爭搶,上下文切換
而每次進入controller->service就相當於建立一個新範例,為了防止producer被建立多次,應該單獨把producer封裝到common或utils或conf
SSL的AC證書認證
域名備案20個工作日左右
同一個證書只能繫結一個域名
繫結域名不包含二三級子域名
證書下載
因為springBoot內建的tomcat
檢驗證書+新增二級域名:域名解析會多出一行txt,
下載出 的證書及密碼
啟動協定變為https
server:
port: 443
ssl:
key-store: 3974629_www.996cloud.work.pfx # 證書的檔名
key-store-password: O4d8Fl8t
準備上傳伺服器的檔案
windows檢視埠號
C:\Users\rxguo>netstat -ano |findstr "443"
赫然發現443被一個pid為7296的程序佔用著.
檢視該pid對應的程式:
tasklist |findstr "程序id號"
於是我嘗試殺掉該程序:被拒絕
taskkill /f /t /im "程序id或者程序名稱"
開啟VM虛擬機器器,點選選單中的「編輯-偏好設定」,把共用虛擬機器器給關閉;
https://localhost/v1/template
查logbak.xml
tar -zcvf 檔案.tgz 檔名
yum install -y lrzsz
地址和ip地址
root@iZ2ze21075lo3b5h7k3wozZ wechat]# ll
total 29468
-rw-r--r-- 1 root root 30175119 Oct 21 17:29 wechat-template.tgz
[root@iZ2ze21075lo3b5h7k3wozZ wechat]# tar -zxf wechat-template.tgz
[root@iZ2ze21075lo3b5h7k3wozZ wechat]# ll
total 29472
drwxr-xr-x 3 1173931 1049089 4096 Oct 21 17:27 wechat-template
-rw-r--r-- 1 root root 30175119 Oct 21 17:29 wechat-template.tgz
[root@iZ2ze21075lo3b5h7k3wozZ wechat]# cd wechat-template
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# ll
total 32076
-rw-r--r-- 1 1173931 1049089 4554 Oct 21 16:57 3974629_www.996cloud.work.pfx
-rw-r--r-- 1 1173931 1049089 819 Oct 21 17:07 application.yml
-rw-r--r-- 1 1173931 1049089 32825383 Oct 21 17:25 kafka-template.jar
drwxr-xr-x 2 1173931 1049089 4096 Oct 21 17:21 templates
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# vi application.yml
停掉之前的專案
cd /usr/local/java/tomcat/apache-tomcat-8.5.40/bin/
netstat -nap | grep 443
https://www.996cloud.work/v1/template
注意因為線上環境缺少kafka所以會報warn
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# vi start.sh
# !/bin/bash
nohub java -jar wechat-template.jar &
// 賦予指令碼執行許可權
-rw-r--r-- 1 root root 51 Oct 22 10:36 start.sh
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# chmod u+x start.sh
// 有了許可權檔名變為綠色
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# ll
total 32080
-rw-r--r-- 1 1173931 1049089 4554 Oct 21 16:57 3974629_www.996cloud.work.pfx
-rw-r--r-- 1 1173931 1049089 786 Oct 22 10:21 application.yml
-rwxr--r-- 1 root root 51 Oct 22 10:36 start.sh
不要錯寫成nohub是nohup
https://blog.csdn.net/qq_40241957/article/details/98584207
1 安全組的埠新增綠卡443
2 centos的防火牆關閉
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# systemctl status fillwalld
Unit fillwalld.service could not be found.
[root@iZ2ze21075lo3b5h7k3wozZ wechat-template]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
小程式
https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html
nodejs
https://nodejs.org/en/
設定node環境變數
檢驗node
C:\Users\rxguo>node -v
v12.19.0
appid:wx9018260c5501cfb1238
解壓qe檔案
刪除自動生成的dist檔案
關注package.json,其中config.ts在src目錄下
修改config.ts,修改後端對應地址
export const baseUrl: string = 'https://www.996cloud.work'
git bash右鍵,命令列編譯
注意:install只會會產生node_moudle 模板檔案特別大
// 檢驗是否有cnpm
cnpm -v
// 沒有則安裝cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g @tarojs/cli@2.0.6
cnpm install
npm run build:weapp
注意如果taro 出現問題
taro info
檢視版本資訊
生成dist檔案,就是要釋出到微信程式裡的內容
見博主的【訊息佇列】kafka 文章
Java HotSpot™ 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000794500000, 576716800, 0)
停止當前後臺執行的jar
// 找到java對應的pid
netstat -anp
// 殺死程序pid
kill -9 程序pid
重新修改啟動指令碼
nohup java -Xms256m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=256m -jar wechat-template.jar &
JAVA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=256m"
java -Xms512m -Xmx512m -XX:+UseG1GC -XX:+PrintGCDetails -jar 專案.jar
命令列檢視記憶體佔用的pid
top
選著性
kill -9 pid號