Android Dialog弹出时不显示导航栏(沉浸式)

我们的app是全屏沉浸式的,发现activity在设置了全屏后,弹出dialog底部会跳出导航栏虚拟键。具体原因是因为Dialog的Window抢走了焦点,Window 中的 DecorView 状态改变导致了退出。 解决方法,在Dialog中:

private void fullScreenImmersive(View view) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
             | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
             | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
             | View.SYSTEM_UI_FLAG_FULLSCREEN;
         view.setSystemUiVisibility(uiOptions);
     }
 }

 @Override
 public void show() {
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
     super.show();
     fullScreenImmersive(getWindow().getDecorView());
     this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
 }

Docker私服Sonatype Nexus的搭建与使用

为什么要建docker私服

  • 官服在国外,速度慢
  • 创建的镜像是私有的,不想公开传到外部服务器上

Sonatype Nexus介绍

Sonatype Nexus是一个仓库管理软件,支持Maven、Docker、Bower、PyPI、Yum等类型仓库。使用Maven管理依赖的Java程序员一定不陌生,搭建Maven私服基本是用这个。注意:3.x版本才支持docker。 今天我们就用Nexus作Docker的私服,管理我们自己的私有镜像。

Sonatype Nexus安装

很简单,下载,解压,我的路径是
/usr/local/nexus-3.11.0-01
创建开机启动脚本
vim /etc/systemd/system/nexus.service
内容如下:

[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/nexus-3.11.0-01/bin/nexus start
ExecStop=/usr/local/nexus-3.11.0-01/bin/nexus stop
Restart=on-abort

[Install]
WantedBy=multi-user.target

设置开机启动
systemctl enable nexus
启动
systemctl start nexus nexus默认端口是8081, 现在打开浏览器,访问http://ip:8081, 应该能看到如下界面
 点击右上角Sign in,账号admin 密码admin123,登录
 点击admin ,把默认的密码改掉,安装完成。

创建仓库

点击顶部配置图标,切到配置页面,左侧菜单选Repositories, 打开仓库配置,点击Create repository 创建仓库。
 我之前已经创建好,配置如下图
 注意,这里HTTP设置了一个10008端口,仅供docker使用,找个不冲突的就行。

docker配置

docker仓库默认需要https支持,但nexus没有配ssl,需要将服务器加到insecure-registries。
vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://registry.docker-cn.com"],
  "insecure-registries":["192.168.0.201:10008"]
}

我的nexus部署在192.168.0.201 后面的端口10008与创建仓库时对应。 登录仓库:
docker login 192.169.0.201:10008
按提示输入nexus的账号密码。登录一次以后,会自动保存,以后pull/push都不需要再登录。

docker 提交镜像

  1. 将正在运行的container打包成image

    如图,nginx2容器基于nginx镜像,但是改了一些配置, 现在我们将其打包成image
    docker commit -m "nginx modified" -a "Exception" cb4cbbcde646 nginx-modified -m 描述
    -a 作者
    cb4cbbcde646 container id
    nginx-modified 生成的image名
    现在再看镜像列表:
    
    发现我们刚刚commit的image已经在里面了。

  2. 给image打标签
    docker tag f3b408f36cf7 192.168.0.201:10008/nginx-modified f3b408f36cf7 image id
    192.168.0.201:10008/nginx-modified 仓库地址和保存路径

  3. 推送镜像
    docker push 192.168.0.201:10008/nginx-modified

  4. 下载镜像
    在另一台服务器上,如果要下载镜像,重复上面的docker配置步骤,然后pull
    docker pull 192.168.0.201:10008/nginx-modified

systemctl开机启动zookeeper

用systemctl使zookeeper开机启动,网上看了好多章,没一个好使,自己琢磨折腾了一个下午总算把它搞定。
直接上脚本:/etc/systemd/system/zookeeper.service

[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
Environment=ZOO_LOG_DIR=/usr/local/zookeeper-3.4.11/
Environment=PATH=/usr/local/jdk1.8.0_152/bin:/usr/local/jdk1.8.0_152/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
ExecStart=/usr/local/zookeeper-3.4.11/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper-3.4.11/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper-3.4.11/bin/zkServer.sh restart
PIDFile=/tmp/zookeeper/zookeeper_server.pid
User=www
[Install]
WantedBy=multi-user.target

重点在两行Environment。

  • 第一行设置日志目录,如果没有设置,默认是当前目录,对www用户来说,可能没有权限。
  • 第二行是配置环境变量,systemd用户实例不会继承类似.bashrc中定义的环境变量,所以是找不到jdk目录的,而zookeeper又必须有。

保存后,reload

systemctl daemon-reload

启用开机自启

systemctl enable zookeeper

启动服务

systemctl start zookeeper

Springboot 启动脚本

Springboot项目可以直接通过

java -jar xxx.jar

命令启动,但是开启关闭还是有个脚本会比较方便。自己写了个shell脚本,功能是查找当前目录下的jar文件,然后启动,也可以关闭,重启。经过几次修改,个人觉得比较完美了,放出源码:

#! /bin/bash
# springboot的jar放同级目录下即可,只能有一个jar文件
export PATH=$JAVA_HOME/bin:$PATH
CURRENT_PATH=$(cd "$(dirname "$0")"; pwd)
JAR=$(find $CURRENT_PATH -maxdepth 1 -name "*.jar")
PID=$(ps -ef | grep $JAR | grep -v grep | awk '{ print $2 }')

case "$1" in
    start)
        if [ ! -z "$PID" ]; then
            echo "$JAR 已经启动,进程号: $PID"
        else
            echo -n -e "启动 $JAR ... \n"
            cd $CURRENT_PATH
        nohup java -jar $JAR >/dev/null 2>&1 &
            if [ "$?"="0" ]; then
                echo "启动完成,请查看日志确保成功"
            else
                echo "启动失败"
            fi
        fi
        ;;
    stop)
        if [ -z "$PID" ]; then
            echo "$JAR 没有在运行,无需关闭"
        else
            echo "关闭 $JAR ..."
              kill -9 $PID
            if [ "$?"="0" ]; then
                echo "服务已关闭"
            else
                echo "服务关闭失败"
            fi
        fi
        ;;
    restart)
        ${0} stop
        ${0} start
        ;;
    kill)
        echo "强制关闭 $JAR"
        killall $JAR
        if [ "$?"="0" ]; then
            echo "成功"
        else
            echo "失败"
        fi
        ;;
    status)
        if [ ! -z "$PID" ]; then
            echo "$JAR 正在运行"
        else
            echo "$JAR 未在运行"
        fi
        ;;
  *)
    echo "Usage: ./springboot {start|stop|restart|status|kill}" >&2
        exit 1
esac

脚本会找到当前目录下的jar文件,所以,只能放一个jar文件。 开机启动脚本:

[Unit]
Description=Dubbo-admin
Wants=network.target
[Service]
Environment=JAVA_HOME=/usr/local/jdk1.8.0_171
Type=forking
ExecStart=/usr/local/xxx/springboot start
ExecStop=/usr/local/xxx/springboot stop
[Install]
WantedBy=multi-user.target

使用Logstash同步mysql到Elasticsearch 6.0.1

Logstash是一个数据收集管道,配置输入输出,可将数据从一个地方传到另一个地方。同步mysql到Elasticsearch,这里的输入,指的是mysql,输出就是Elasticsearch。 新版本的Logstash和Elasticsearch跟之前老版本的有些不同,所以我也是自己折腾了小半天,总算成功。

下面的操作,Logstash和Elasticsearch的版本都是6.0.1


  1. 下载安装 Logstash和Elasticsearch就不介绍了,下载后解压即可使用。 默认Logstash不包含读取数据库的jdbc插件,需要手动下载。进入Logstash的bin目录,执行:

    ./logstash-plugin install logstash-input-jdbc
因为网络原因,安装可能费点时间。
  1. 在某处建一个目录,放置配置文件(哪不重要,因为执行时会配置路径),我这放到bin下的mysql目录里。

  2. 复制mysql jdbc驱动(mysql-connector-java-5.1.35.jar)到该目录下

  3. 编写导出数据的sql,放到sql.sql(文件名自己取)里,内容类似这样:

    select *, id as car_id 
    from violation_car car 
    where car.gmt_modified>= :sql_last_value
这里需要介绍下,我的表里有个gmt\_modified字段,用于记录该条记录的最后修改时间,sql\_last_value是Logstash查询后,保存的上次查询时间,第一次查询时,该值是1970/1/1,所以第一次导入,如果你的表现有数据很多,可能会有点问题,后面会根据最后修改时间,更新修改过的数据。
  1. 编写输入输出配置文件jdbc.conf

    input {
      jdbc {
        jdbc_driver_library => "/xxx/xxx/logstash-6.0.1/bin/mysql/mysql-connector-java-5.1.35.jar"
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string => "jdbc:mysql://localhost:3306/violation"
        jdbc_user => "root"
        jdbc_password => ""
        schedule => "* * * * *"
        jdbc_default_timezone => "Asia/Shanghai"
        statement_filepath => "/xxx/xxx/logstash-6.0.1/bin/mysql/sql.sql"
        use_column_value  => false
        last_run_metadata_path => "/xxx/xxx/logstash-6.0.1/bin/mysql/last_run.txt"
      }
    }
    output {
        elasticsearch {
            hosts => ["127.0.0.1:9200"]
            index => "violation"
            document_id => "%{car_id}"
            document_type => "car"
        }
        stdout {
            codec => json_lines
        }
    }
字段介绍:

    input.jdbc.jdbc_driver_library  jdbc驱动的位置
    input.jdbc.jdbc_driver_class    驱动类名
    input.jdbc.jdbc_connection_string   数据库连接字符串
    input.jdbc.jdbc_user    用户名
    input.jdbc.jdbc_password  密码
    input.jdbc.schedule   更新计划(参考linux crontab)
    input.jdbc.jdbc_default_timezone   时区,默认没有时区,日志里时间差8小时,中国需要用Asia/Shanghai
    input.jdbc.statement_filepath 导出数据的sql文件,就是上面写的
    input.jdbc.use_column_value  如果是true,sql_last_value是tracking_column指定字段的数字值,false就是时间,默认是false
    input.jdbc.last_run_metadata_path  保存sql_last_value值文件的位置
    output.elasticsearch.hosts elasticsearch服务器,填多个,请求会负载均衡。
    output.elasticsearch.index 索引名
    output.elasticsearch.document_id   生成文件的id,这里使用sql产生的car_id
    output.elasticsearch.document_type  文档类型 
    output.stdout 配置的是命令行输出,使用json
  1. 配置完以后,启动

    ./logstash -f mysql/jdbc.conf
按上面的配置,logstash会每分钟查询一次表,看是否会更新,有更新则提交到Elasticsearch

Java读取RSA pem公钥 使用公钥验证签名

之所以要记录是因为网上找了好久,试了好多方法,基本上都不好使. 签名sign做了base64

    BASE64Decoder base64decoder = new BASE64Decoder();
try {
    //读取pem证书
    BufferedReader br = new BufferedReader(new FileReader("xxx.pem"));
    String s = br.readLine();
    StringBuffer publickey = new StringBuffer();
    while (s.charAt(0) != '-') {
        publickey.append(s + "\r");
        s = br.readLine();
    }
    byte[] keybyte = base64decoder.decodeBuffer(publickey.toString());
    KeyFactory kf = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keybyte);
    PublicKey publicKey = kf.generatePublic(keySpec);
    BASE64Encoder bse=new BASE64Encoder();
    System.out.println("pk:"+bse.encode(publicKey.getEncoded()));

    //被签的原文
    String toSign="sxxxsasdsss";
    //生成的签名
    String sign="xxxxx";

    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initVerify(publicKey);
    signature.update(toSign.getBytes());
    boolean verify = signature.verify(base64decoder
        .decodeBuffer(
            sign));
    System.out.println(verify);
} catch (Exception e) {
    e.printStackTrace();
}

Springboot 禁用数据库自动配置

如果我们用SpringBoot实现一个简单的微服务,不需要数据库,你会发现在写完代码启动时会报 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties' 之类的错误。这是因为SpringBoot默认会自动配置数据库,如果业务不需要,就要手动禁用数据库自动配置,在Application的SpringBootApplication注解里加上 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

Spring boot+mybatis+druid+SQL监控配置

之前写过一篇搭建Spring boot + mybatis + freemarker项目,Spring boot默认的数据库连接池是tomcat-jdbc,今天我们要用druid替换它。

什么是druid

druid是阿里巴巴开源的数据库连接池,自称是Java语言中最好的数据库连接池,提供强大的监控和扩展功能。

为什么用druid

  1. 性能

    官方数据Benchmark_aliyun,druid在响应时间上优于其他几个线程池。非官方的测试数据可能差距没这么明显,但仍然高于其他几个线程池。

  2. 自带监控功能

    自带监控,可帮助开发者找出慢查询,查看并发数等。

配置步骤:

  1. 基于干净的spring boot web项目,添加mysql、mybatis、druid库:

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.31</version>
    </dependency>
  1. 配置数据库连接

    application.properties:

    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=
这里key叫什么其实并不重要,因为后面配置数据源,我们会自己读取配置。
  1. Mybatis、druid配置
    新建MyBatisConfig.java:

    package com.pocketdigi.config.database;
    
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    
    import javax.sql.DataSource;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    @Configuration
    @MapperScan("com.pocketdigi.dal.mapper")
    public class MyBatisConfig {
        @Value("${spring.datasource.url}")
        String dbUrl;
        @Value("${spring.datasource.username}")
        String userName;
        @Value("${spring.datasource.password}")
        String password;
    
        @Value("${spring.datasource.driverClassName}")
        String driverClassName;
        private static String MYBATIS_CONFIG = "mybatis_config.xml";
        private static String MAPPER_PATH = "/mapper/*.xml";
    
        @Bean
        public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            //设置mybatis configuration 扫描路径
            sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));
            //添加mapper 扫描路径
            PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
            String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + MAPPER_PATH;
            sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
            //设置datasource
            sqlSessionFactoryBean.setDataSource(dataSource());
            return sqlSessionFactoryBean;
        }
    
        private DataSource dataSource() {
            Map<String,Object> properties=new HashMap<>();
            properties.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME,driverClassName);
            properties.put(DruidDataSourceFactory.PROP_URL,dbUrl);
            properties.put(DruidDataSourceFactory.PROP_USERNAME,userName);
            properties.put(DruidDataSourceFactory.PROP_PASSWORD,password);
            //添加统计、SQL注入、日志过滤器
            properties.put(DruidDataSourceFactory.PROP_FILTERS,"stat,wall,log4j2");
            //sql合并,慢查询定义为5s
            properties.put(DruidDataSourceFactory.PROP_CONNECTIONPROPERTIES,"druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
            try {
                return DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }
mybatis的mapper接口在com.pocketdigi.dal.mapper,mybatis_config.xml在resources目录下,xml mapper文件在resources/mapper/目录下。

mybatis_config.xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <!-- 所有映射器中配置的缓存的全局开关-->
            <setting name="cacheEnabled" value="false"/>
            <!-- 延迟加载的全局开关 配置二级缓存时将此属性关闭-->
            <setting name="lazyLoadingEnabled" value="false"/>
            <!-- 关联对象加载 配置二级缓存时将此属性关闭-->
            <setting name="aggressiveLazyLoading" value="false"/>
            <!-- 是否允许单一语句返回多结果集-->
            <setting name="multipleResultSetsEnabled" value="true"/>
            <!-- 使用列标签代替列名-->
            <setting name="useColumnLabel" value="true"/>
            <!-- 允许 JDBC 支持自动生成主键,需要驱动兼容 -->
            <setting name="useGeneratedKeys" value="false"/>
            <!-- 指定 MyBatis 是否以及如何自动映射指定的列到字段或属性-->
            <setting name="autoMappingBehavior" value="PARTIAL"/>
            <!-- 配置默认的执行器-->
            <setting name="defaultExecutorType" value="SIMPLE"/>
            <!-- 设置超时时间,它决定驱动等待数据库响应的秒数-->
            <setting name="defaultStatementTimeout" value="30"/>
            <!-- 允许在嵌套语句中使用行分界 -->
            <setting name="safeRowBoundsEnabled" value="false"/>
            <!-- 是否开启自动驼峰命名规则映射 -->
            <setting name="mapUnderscoreToCamelCase" value="false"/>
            <!-- 利用本地缓存机制防止循环引用和加速重复嵌套查询 默认值为 SESSION,这种情况下会缓存一个会话中执行的所有查询-->
            <setting name="localCacheScope" value="SESSION"/>
            <!-- 当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型 -->
            <setting name="jdbcTypeForNull" value="OTHER"/>
            <!-- 指定哪些对象的方法触发一次延迟加载-->
            <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
        </settings>

        <typeAliases>
            <package name="com.pocketdigi.dal.po"/>
        </typeAliases>
    </configuration>


数据库对应的POJO类在com.pocketdigi.dal.po
  1. druid监控后台配置

    druid监控后台需要配置一个Filter和一个Servlet,将指定的路径转发到com.alibaba.druid.support.http.StatViewServlet

    ServletConfiguration.java:

    @Configuration
    public class ServletConfiguration {
        @Bean
        public ServletRegistrationBean druidStatViewServletBean() {
            //后台的路径
            ServletRegistrationBean statViewServletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            Map<String,String> params = new HashMap<>();
            //账号密码,是否允许重置数据
            params.put("loginUsername","admin");
            params.put("loginPassword","admin");
            params.put("resetEnable","true");
            statViewServletRegistrationBean.setInitParameters(params);
            return statViewServletRegistrationBean;
        }
    }
FilterConfiguration.java

    @Configuration
    public class FilterConfiguration {
        @Bean
        public FilterRegistrationBean druidStatFilterBean() {
            FilterRegistrationBean druidStatFilterBean=new FilterRegistrationBean(new WebStatFilter());
            List<String> urlPattern=new ArrayList<>();
            urlPattern.add("/*");
            druidStatFilterBean.setUrlPatterns(urlPattern);
            Map<String,String> initParams=new HashMap<>();
            initParams.put("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
            druidStatFilterBean.setInitParameters(initParams);
            return druidStatFilterBean;
        }
    }


配置完成,启动应用,打开[http://localhost:8000/druid/](http://localhost:8000/druid/) 用ServletConfiguration.java配置的账号密码登录,即可进入druid监控后台。

代码已传github https://github.com/pocketdigi/springboot-demo

Spring boot 注册Servlet和Filter

使用Spring开发web程序,在大部分情况下,都是通过Spring默认的DispatcherServlet,转发请求到Controller,我们在Controller里处理请求。但有时候,可能有些请求我们不希望通过Spring,而是通过其他Servlet处理。如果是普通的Spring项目,注册Filter和Servlet只需在web.xml时添加filter、filter-mapping、servlet、servlet-mapping,但Spring boot项目里没有web.xml。 Spring boot有两种方法注册Servlet和Filter:代码注册、注解注册。 先写个简单的Servlet:

@Slf4j
public class TestServlet extends HttpServlet{
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String value=config.getInitParameter("param1");
        log.info("param1:{}",value);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Hello");
        out.println("</body>");
        out.println("</html>");
    }
}

代码注册:

代码注册通过ServletRegistrationBean和FilterRegistrationBean两个类,注册Servlet和Filter。 在Application类里添加注册代码:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Bean
    public ServletRegistrationBean testServletBean() {
        ServletRegistrationBean testServletRegistration = new ServletRegistrationBean(new TestServlet(), "/test1/");
        Map<String,String> params = new HashMap<>();
        params.put("param1","value2");
        testServletRegistration.setInitParameters(params);
        return testServletRegistration;

    }

}

三个参数,servlet实例、url mapping、init-param, Filter也类似,只是用FilterRegistrationBean。 或者,放到一个单独的配置类里:

@Configuration
public class ServletConfiguration {
    @Bean
    public ServletRegistrationBean testServletBean() {
        ServletRegistrationBean testServletRegistration = new ServletRegistrationBean(new TestServlet(), "/test1/");
        Map<String,String> params = new HashMap<>();
        params.put("param1","value2");
        testServletRegistration.setInitParameters(params);
        return testServletRegistration;
    }
}

建议拆开放到单独的类里,如果所有配置都放Application里,乱。

注解注册

注解注册需要在Servlet类上加上@WebServlet,@WebFilter注解,Application里加上@ServletComponentScan开启Servlet注解扫描。 修改TestServlet

@Slf4j
@WebServlet(urlPatterns = "/test/*",initParams = {
        @WebInitParam(name = "param1", value = "value1"),
})
public class TestServlet extends HttpServlet{
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String value=config.getInitParameter("param1");
        log.info("param1:{}",value);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("Hello");
        out.println("</body>");
        out.println("</html>");
    }
}

如果是Filter:

@WebFilter(filterName = "webFilter", urlPatterns = "/*",
        initParams = {@WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico")
})
public class WebFilter extends Filter {
}

Application:

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

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

怎么选择?

如果是自己实现的Servlet,那么直接加个注解,即可注册。但Servlet不是自己实现的话,用代码注册比较好,否则需要继承原来的Servlet加一个子类。

Java jstack用法

jstack是jdk中自带的用于查看进程内线程栈的工具。当程序出现死锁时,我们可以通过jstack打印线程栈找到问题。

找出代码中的死锁

学习从一段简单的代码开始:

public class Main {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        final Object lock1 = new Object();
        final Object lock2 = new Object();
        Thread thread=new Thread(new Runnable() {
            public void run() {
                synchronized (lock1) {
                    try {
                        for(int i=0;i<20;i++) {
                            Thread.sleep(1000);
                            System.out.println(Thread.currentThread().getName()+" "+i);
                        }
                        synchronized (lock2) {
                            for(int i=20;i<40;i++) {
                                Thread.sleep(1000);
                                System.out.println(Thread.currentThread().getName()+" "+i);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        synchronized (lock2) {
            try {
                for(int i=0;i<20;i++) {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+" "+i);
                }
                synchronized (lock1) {
                    for(int i=20;i<40;i++) {
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName()+" "+i);
                    }
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

肉眼都能看出来的死锁,程序运行20s后进入死锁状态。
首先我们先找出这个程序的进程id,建议用jps,而不是ps,原因是jps只会显示当前用户下面的java进程,等于自动帮我们过滤了。

jps -l

Main进程,id 5632,这时出动我们的主角jstack.

jstack 5632

结果如下

2017-05-23 13:57:22
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):

"Attach Listener" #12 daemon prio=9 os_prio=31 tid=0x00007ff0f780c800 nid=0x4507 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Thread-0" #11 prio=5 os_prio=31 tid=0x00007ff0f80c0000 nid=0x5a03 waiting for monitor entry [0x0000700002310000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at Main$1.run(Main.java:33)
    - waiting to lock <0x000000076adb1a98> (a java.lang.Object)
    - locked <0x000000076adb1a88> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:745)

"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007ff0f980b800 nid=0x5603 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #9 daemon prio=9 os_prio=31 tid=0x00007ff0f6803000 nid=0x5403 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #8 daemon prio=9 os_prio=31 tid=0x00007ff0f6802800 nid=0x5203 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #7 daemon prio=9 os_prio=31 tid=0x00007ff0f6801800 nid=0x5003 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=31 tid=0x00007ff0f90de800 nid=0x4e03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #5 daemon prio=5 os_prio=31 tid=0x00007ff0f90dc800 nid=0x4c03 runnable [0x0000700001bfb000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    - locked <0x000000076af12b68> (a java.io.InputStreamReader)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    - locked <0x000000076af12b68> (a java.io.InputStreamReader)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
    at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007ff0f8039800 nid=0x4a03 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007ff0f9803000 nid=0x3903 in Object.wait() [0x0000700001972000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076ab06f58> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
    - locked <0x000000076ab06f58> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007ff0f9802000 nid=0x3703 in Object.wait() [0x000070000186f000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076ab06998> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:502)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
    - locked <0x000000076ab06998> (a java.lang.ref.Reference$Lock)

"main" #1 prio=5 os_prio=31 tid=0x00007ff0f8002000 nid=0x1c03 waiting for monitor entry [0x0000700000e51000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at Main.test1(Main.java:52)
    - waiting to lock <0x000000076adb1a88> (a java.lang.Object)
    - locked <0x000000076adb1a98> (a java.lang.Object)
    at Main.main(Main.java:18)

"VM Thread" os_prio=31 tid=0x00007ff0f801f800 nid=0x3503 runnable

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007ff0f9003800 nid=0x2503 runnable

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007ff0f9004000 nid=0x2703 runnable

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007ff0f7801000 nid=0x2903 runnable

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007ff0f600e800 nid=0x2b03 runnable

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007ff0f600f000 nid=0x2d03 runnable

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007ff0f6010000 nid=0x2f03 runnable

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007ff0f6010800 nid=0x3103 runnable

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007ff0f6011000 nid=0x3303 runnable

"VM Periodic Task Thread" os_prio=31 tid=0x00007ff0f980c000 nid=0x5803 waiting on condition

JNI global references: 26


Found one Java-level deadlock:
=============================
"Thread-0":
  waiting to lock monitor 0x00007ff0f8024f38 (object 0x000000076adb1a98, a java.lang.Object),
  which is held by "main"
"main":
  waiting to lock monitor 0x00007ff0f8023bf8 (object 0x000000076adb1a88, a java.lang.Object),
  which is held by "Thread-0"

Java stack information for the threads listed above:
===================================================
"Thread-0":
    at Main$1.run(Main.java:33)
    - waiting to lock <0x000000076adb1a98> (a java.lang.Object)
    - locked <0x000000076adb1a88> (a java.lang.Object)
    at java.lang.Thread.run(Thread.java:745)
"main":
    at Main.test1(Main.java:52)
    - waiting to lock <0x000000076adb1a88> (a java.lang.Object)
    - locked <0x000000076adb1a98> (a java.lang.Object)
    at Main.main(Main.java:18)

Found 1 deadlock.

jstack直接帮我们找出来了deadlock。真实场景下,如果代码比较复杂,可能需要我们自己分析找出死锁。
如果我们自己分析,该怎么找出死锁呢?重点分析java.lang.Thread.State: BLOCKED的进程,找到
waiting to lock,看看这个锁的持有者,是不是也被锁着,沿着这个链路找下去,是不是死锁就能找出来 了。比如上面的例子中,main线程持有0x000000076adb1a98锁,等待0x000000076adb1a88锁释放,而Thread-0持有0x000000076adb1a88锁,等待0x000000076adb1a98锁释放,两个线程互相等待,而进入死锁状态。

找出CPU消耗多的代码

如果程序cpu占用很高,我们需要找到问题优化,可以配合top命令,找出最耗cpu的进程,从而找到相应代码解决问题。

  1. 先用jps找出程序pid,这里是23034

  2. 用top命令找出该进程最耗cpu的线程。下面的top是linux中的,mac里的不一样。

    top -Hp 23034
结果如下图  
![](https://img.pocketdigi.com/blog/1495522309.jpg)

23046线程占了93.8的cpu,就是它.
  1. 将23046转成16进制。因为top里的pid是10进制,而jstack里是16进制,叫nid。
    可以用printf命令转换

    printf "%x\n" 23046
得到`5a06`
  1. jstack出场

    jstack 23034 | grep 5a06
结果如下:  
![](https://img.pocketdigi.com/blog/1495522365.jpg)

writeFileThread占用了最多的cpu资源。找到后,可以优化代码了.