Springboot小demoインスタンス
14602 ワード
package com.my.mml;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.my.mml.bean;
public class AgentCache {
private String hostname;
private String ip;
private String port;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
package com.my.mml.bean;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
public class CacheManager {
private ConcurrentHashMap cacheContainer;
private static volatile CacheManager cacheManager;
private long timeout;
private final String DELIMITER = ":";
public static CacheManager getInstance() {
if (cacheManager == null) {
synchronized (CacheManager.class) {
if (cacheManager == null) {
cacheManager = new CacheManager(1000 * 30);
}
}
}
return cacheManager;
}
private CacheManager(long timeout) {
this.timeout = timeout;
cacheContainer = new ConcurrentHashMap<>();
CleanCacheTimer cacheCleanerThread = new CleanCacheTimer();
cacheCleanerThread.start();
}
public void setCache(AgentCache agent) {
String updateTime = String.valueOf(System.currentTimeMillis());
String key = String.join(DELIMITER, agent.getHostname(), agent.getIp(), agent.getPort());
cacheContainer.put(key, updateTime);
}
public String getCache() {
return cacheContainer.toString();
}
class CleanCacheTimer {
public void start() {
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("TimerTask is called!");
doRun();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, 1000 * 3);
}
private void doRun() {
for (Map.Entry entry : cacheContainer.entrySet()) {
String hostKey = entry.getKey();
String lastTime = entry.getValue();
if (System.currentTimeMillis() - Long.parseLong(lastTime) >= timeout) {
cacheContainer.remove(hostKey);
}
}
}
}
}
package com.my.mml.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.my.mml.bean.AgentCache;
import com.my.mml.bean.CacheManager;
@RestController
@RequestMapping("/agent")
public class AgentInfoController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String sayWorld(@PathVariable("name") String name) {
return "Hello " + name + " , are you ok? you are the best!";
}
/**
* json ,spring bean , @RequestBody
*
* @param name
* @param pwd
* @return
*/
@RequestMapping(value = "/hostinfo", method = RequestMethod.POST)
public String postHostInfo(@RequestBody AgentCache hostInfo) {
CacheManager.getInstance().setCache(hostInfo);
return CacheManager.getInstance().getCache();
}
}
4.0.0
com.zetyun
my-server
0.0.1-SNAPSHOT
UTF-8
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
true
maven-ali
http://maven.aliyun.com/nexus/content/groups/public
true
true
always
fail
org.springframework.boot
spring-boot-starter-parent
1.2.4.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
Spring Bootフルログ設定
src/main/resourcesディレクトリの下にlogback.xmlファイルを作成すればいいです.
logback.xmlの内容は次のとおりです.
Logback For demo Mobile
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} -%msg%n
System.out
${LOG_HOME}/trace/%d{yyyy-MM-dd}.log
180
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n
20MB
TRACE
ACCEPT
DENY
${LOG_HOME}/debug/%d{yyyy-MM-dd}.log
180
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n
20MB
DEBUG
ACCEPT
DENY
${LOG_HOME}/info/%d{yyyy-MM-dd}.log
180
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n
20MB
INFO
ACCEPT
DENY
${LOG_HOME}/warn/%d{yyyy-MM-dd}.log
180
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n
20MB
WARN
ACCEPT
DENY
${LOG_HOME}/error/%d{yyyy-MM-dd}.log
180
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} -%msg%n
20MB
ERROR
ACCEPT
DENY
印刷されたログは、異なるレベルのディレクトリの下にあります.たとえば、構成
${LOG_HOME}/error/%d{yyyy-MM-dd}.log
直接jarパッケージで実行
java -jar ./my-server-0.0.1-SNAPSHOT.jar com.zetyun.mml.Application
参照リンク:https://blog.csdn.net/smilecall/article/details/56288972/
参照リンク:https://blog.csdn.net/u012995856/article/details/78090818
GitHub:https://github.com/lliuxiangke0210/my-server