一、actuator監(jiān)控
springboot自帶actuator監(jiān)控,開啟配置后,訪問(wèn)相關(guān)鏈接就可以返回服務(wù)運(yùn)行相關(guān)信息,使用方法如下:
1、pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、application.yml:
spring:
security:
user:
name: admin
password: admin
management:
# 端點(diǎn)信息接口使用的端口,為了和主系統(tǒng)接口使用的端口進(jìn)行分離
server:
port: 8090
servlet:
context-path: /sys
# 端點(diǎn)健康情況,默認(rèn)值"never",設(shè)置為"always"可以顯示硬盤使用情況和線程情況
endpoint:
health:
show-details: always
# 設(shè)置端點(diǎn)暴露的哪些內(nèi)容,默認(rèn)["health","info"],設(shè)置"*"代表暴露所有可訪問(wèn)的端點(diǎn)
endpoints:
web:
exposure:
include: '*'
完成上述兩步,actuator監(jiān)控就配置好了,然后訪問(wèn)localhost:8090/sys/actuator/beans
,登錄的時(shí)候用戶名密碼為yml中配置的,然后就可以看到spring中管理的所有bean了。具體有哪些路徑可以訪問(wèn),可以參考如下的文檔:
actuator文檔:https://docs./spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#production-ready
二、springboot-admin(SBA)
上面說(shuō)到了actuator監(jiān)控,雖然方便,但是返回的是json,不太友好。springboot-admin就提供了ui界面展示這些信息。用法也很簡(jiǎn)單,我們需要新建一個(gè)springboot-admin-server項(xiàng)目,用來(lái)做服務(wù)端,其他所有需要被監(jiān)控的項(xiàng)目去連接這個(gè)服務(wù)端就可以。
1、springboot-admin-server:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
server:
port: 666
// 開啟adminserver
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
}
服務(wù)端這樣就可以了。
2、springboot-admin-client:
admin-client就是需要監(jiān)控的項(xiàng)目,在需要監(jiān)控的項(xiàng)目里做如下的改造:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
server:
port: 80
spring:
application:
name: util
boot:
admin:
client:
url:
- "http://localhost:666/"
instance:
metadata:
# 客戶端端點(diǎn)信息的用戶名、密碼
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
security:
user:
name: admin
password: admin
management:
# 端點(diǎn)健康情況,默認(rèn)值"never",設(shè)置為"always"可以顯示硬盤使用情況和線程情況
endpoint:
health:
show-details: always
# 設(shè)置端點(diǎn)暴露的哪些內(nèi)容,默認(rèn)["health","info"],設(shè)置"*"代表暴露所有可訪問(wèn)的端點(diǎn)
endpoints:
web:
exposure:
include: '*'
這樣就配置好了,啟動(dòng)上面的springboot-admin-server,再啟動(dòng)客戶端,然后訪問(wèn):http://localhost:666/
,就可以看到被admin-server監(jiān)控的應(yīng)用了。
三、springboot-https
我們自己擼的代碼發(fā)布到tomcat后,都是用http訪問(wèn)的,如果想用https訪問(wèn)怎么搞??jī)煞N辦法,一種是在nginx配置證書,然后反向代理我們的項(xiàng)目,這種方式只需申請(qǐng)證書,在nginx中配置,項(xiàng)目不需要做任何修改;另一種是不需要用nginx,在項(xiàng)目中配置證書。下面將講的是第二種方式。
1、生成證書:
生產(chǎn)環(huán)境,這個(gè)證書是在那些認(rèn)證機(jī)構(gòu)買來(lái)的,這里為了節(jié)省money,利用jdk的keytools來(lái)生成證書(瀏覽器會(huì)提示不安全)。
- cmd進(jìn)入jdk的bin目錄,執(zhí)行如下命令:
keytool -genkey -alias test -keyalg RSA -keystore ./server.keystore
-alias
是別名,要記住,等下項(xiàng)目的配置文件中要配置;server.keystore
是證書的文件名。
回車后會(huì)要求你輸入一些信息,如下:
輸入密鑰庫(kù)口令:
再次輸入新口令:
您的名字與姓氏是什么?
[Unknown]: zhusl
您的組織單位名稱是什么?
[Unknown]: company
您的組織名稱是什么?
[Unknown]: company
您所在的城市或區(qū)域名稱是什么?
[Unknown]: sz
您所在的省/市/自治區(qū)名稱是什么?
[Unknown]: sz
該單位的雙字母國(guó)家/地區(qū)代碼是什么?
[Unknown]: CN
CN=zhusl, OU=company, O=company, L=sz, ST=sz, C=CN是否正確?
[否]: y
輸入 <test> 的密鑰口令
(如果和密鑰庫(kù)口令相同, 按回車):
Warning:
JKS 密鑰庫(kù)使用專用格式。建議使用 "keytool -importkeystore -srckeystore ./server.keystore -destkeystore ./server.keystore -deststoretype pkcs12" 遷移到行業(yè)標(biāo)準(zhǔn)格式 PKCS12。
這里要記住輸入的<test>密鑰口令
,等下項(xiàng)目配置文件中要配置的。執(zhí)行完后,會(huì)有個(gè)warning,直接執(zhí)行它建議使用的那段命令就好了,然后會(huì)重新生成一個(gè)server.keystore
證書。執(zhí)行了那段命令后,會(huì)返回如下提示信息:
輸入源密鑰庫(kù)口令:
已成功導(dǎo)入別名 test 的條目。
已完成導(dǎo)入命令: 1 個(gè)條目成功導(dǎo)入, 0 個(gè)條目失敗或取消
Warning:
已將 "./server.keystore" 遷移到 Non JKS/JCEKS。將 JKS 密鑰庫(kù)作為 "./server.keystore.old" 進(jìn)行了備份。
- 這就表示生成證書成功了,就在jdk的bin目錄下。找到該證書,復(fù)制到項(xiàng)目的resources目錄下。
2、配置證書:
server:
port: 443
ssl:
key-alias: test # 剛才設(shè)置的別名
key-store: classpath:server.keystore
enabled: true
key-store-type: PKCS12
key-store-password: 123456 # 剛才設(shè)置的密碼
@Configuration
public class TomcatConfig {
/**
* 配置將 80 轉(zhuǎn)到 443 端口
* @return
*/
@Bean
public Connector connector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
/**
* 將connector設(shè)置到tomcat中
* @param connector
* @return
*/
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
接下來(lái)啟動(dòng)項(xiàng)目,訪問(wèn)項(xiàng)目的controller,你就會(huì)發(fā)現(xiàn),即使用http訪問(wèn),也會(huì)自動(dòng)轉(zhuǎn)到https。
https訪問(wèn)