springboot监控&springboot配置https

时间:2022-07-23
本文章向大家介绍springboot监控&springboot配置https,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、actuator监控

springboot自带actuator监控,开启配置后,访问相关链接就可以返回服务运行相关信息,使用方法如下: 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>

java开发那些事


2、application.yml:

spring:
  security:
    user:
      name: admin
      password: admin
management:
  # 端点信息接口使用的端口,为了和主系统接口使用的端口进行分离
  server:
    port: 8090
    servlet:
      context-path: /sys
  # 端点健康情况,默认值"never",设置为"always"可以显示硬盘使用情况和线程情况
  endpoint:
    health:
      show-details: always
  # 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
  endpoints:
    web:
      exposure:
        include: '*'

完成上述两步,actuator监控就配置好了,然后访问localhost:8090/sys/actuator/beans,登录的时候用户名密码为yml中配置的,然后就可以看到spring中管理的所有bean了。具体有哪些路径可以访问,可以参考如下的两个文档:

二、springboot-admin(SBA)

上面说到了actuator监控,虽然方便,但是返回的是json,不太友好。springboot-admin就提供了ui界面展示这些信息。用法也很简单,我们需要新建一个springboot-admin-server项目,用来做服务端,其他所有需要被监控的项目去连接这个服务端就可以。

1、springboot-admin-server:

  • pom.xml:
<dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
  • application.yml:
server:
  port: 666
  • 主启动类:
// 开启adminserver
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAdminServerApplication.class, args);
    }
}

服务端这样就可以了。

2、springboot-admin-client: admin-client就是需要监控的项目,在需要监控的项目里做如下的改造:

  • pom.xml:
<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>
  • appication.yml:
server:
  port: 80
spring:
  application:
    name: util
  boot:
    admin:
      client:
        url:
        - "http://localhost:666/"
        instance:
          metadata:
            # 客户端端点信息的用户名、密码
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
  security:
    user:
      name: admin
      password: admin
management:
  # 端点健康情况,默认值"never",设置为"always"可以显示硬盘使用情况和线程情况
  endpoint:
    health:
      show-details: always
  # 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
  endpoints:
    web:
      exposure:
        include: '*'

这样就配置好了,启动上面的springboot-admin-server,再启动客户端,然后访问:http://localhost:666/,就可以看到被admin-server监控的应用了。

三、springboot-https

我们自己撸的代码发布到tomcat后,都是用http访问的,如果想用https访问怎么搞?两种办法,一种是在nginx配置证书,然后反向代理我们的项目,这种方式只需申请证书,在nginx中配置,项目不需要做任何修改;另一种是不需要用nginx,在项目中配置证书。下面将的是第二种方式。

1、生成证书: 生产环境,这个证书是在那些认证机构买来的,这里为了节省money,利用jdk的keytools来生成证书(浏览器会提示不安全)。

  • cmd进入jdk的bin目录,执行如下命令:
keytool -genkey -alias test -keyalg RSA -keystore ./server.keystore

-alias是别名,要记住,等下项目的配置文件中要配置;server.keystore是证书的文件名。

回车后会要求你输入一些信息,如下:

输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
  [Unknown]:  zhusl
您的组织单位名称是什么?
  [Unknown]:  company
您的组织名称是什么?
  [Unknown]:  company
您所在的城市或区域名称是什么?
  [Unknown]:  sz
您所在的省/市/自治区名称是什么?
  [Unknown]:  sz
该单位的双字母国家/地区代码是什么?
  [Unknown]:  CN
CN=zhusl, OU=company, O=company, L=sz, ST=sz, C=CN是否正确?
  [否]:  y

输入 <test> 的密钥口令
        (如果和密钥库口令相同, 按回车):

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore ./server.keystore -destkeystore ./server.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

这里要记住输入的<test>密钥口令,等下项目配置文件中要配置的。执行完后,会有个warning,直接执行它建议使用的那段命令就好了,然后会重新生成一个server.keystore证书。执行了那段命令后,会返回如下提示信息:

输入源密钥库口令:
已成功导入别名 test 的条目。
已完成导入命令: 1 个条目成功导入, 0 个条目失败或取消

Warning:
已将 "./server.keystore" 迁移到 Non JKS/JCEKS。将 JKS 密钥库作为 "./server.keystore.old" 进行了备份。
  • 这就表示生成证书成功了,就在jdk的bin目录下。找到该证书,复制到项目的resources目录下。

2、配置证书:

  • application.yml:
server:
  port: 443
  ssl:
    key-alias: test # 刚才设置的别名
    key-store: classpath:server.keystore
    enabled: true
    key-store-type: PKCS12
    key-store-password: 123456 # 刚才设置的密码
  • 配置tomcat:
@Configuration
public class TomcatConfig {

    /**
     * 配置将 80 转到 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设置到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;
    }
}

接下来启动项目,访问项目的controller,你就会发现,即使用http访问,也会自动转到https。

https访问