存档

‘工作’ 分类的存档

黑科技:HBase之绑定Host问题

2016年4月28日 没有评论

访问HBase时,必须绑定host,否则那种hostname没办法解析。
作为致力于降低生产环境运维难度的选手,挖到一个办法来解决这个问题。

hbase的hostname判断位于org.apache.hadoop.hbase.ipc.RpcClientImpl的第299行

    Connection(ConnectionId remoteId, final Codec codec, final CompressionCodec compressor)
    throws IOException {
      if (remoteId.getAddress().isUnresolved()) {
        throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName());
      }

其中remoteId.getAddress()和定义如下

  public InetSocketAddress getAddress() {
    return address;
  }

InetSocketAddress的.isUnresolved()如下

    private final transient InetSocketAddressHolder holder;
...
    public final boolean isUnresolved() {
        return holder.isUnresolved();
    }

InetSocketAddressHolder的isUnresolved实现

        private boolean isUnresolved() {
            return addr == null;
        }

也就是说:只要InetSocketAddressHolder中的addr值不为null(为正常值),即解析成功。
更多内容…

java语言中常用时间戳的值域

2016年4月20日 没有评论

经常使用下面这两个时间值当唯一标识,却总记不住它们的时间范围,记录之。

System.currentTimeMillis();
System.nanoTime();

其中currentTimeMillis的时间范围可以简单代码测试得到。
先看描述

 long java.lang.System.currentTimeMillis()


Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. 

See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).

Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
See Also:
java.util.Date

再看代码

Calendar c = Calendar.getInstance();
c.setTimeInMillis(Long.MAX_VALUE);
System.out.println(c.get(Calendar.YEAR));

输入结果为:292278994,加上几千分符292,278,994 = 0.29billion
单位是年,将三亿年,咱们肯定都活不到那时候了~ 人类那时候还在不在也不好说了~ 应该够用了~~

再看nanoTime的描述

 long java.lang.System.nanoTime()


Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds. 

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin. 

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis(). 

Differences in successive calls that span greater than approximately 292 years (2^63 nanoseconds) will not correctly compute elapsed time due to numerical overflow. 

The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed. 

For example, to measure how long some code takes to execute: 

 long startTime = System.nanoTime();
 // ... the code being measured ...
 long estimatedTime = System.nanoTime() - startTime;
To compare two nanoTime values 

 long t0 = System.nanoTime();
 ...
 long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
Returns:
the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds
Since:
1.5

人家已经说得很清楚,区间为292年。还是算一下能用到哪一年吧。

long nanosToMax = Long.MAX_VALUE - System.nanoTime();// 还剩下的nanos数
long millisToMax = TimeUnit.NANOSECONDS.toMillis(nanosToMax);// 转为剩下的毫秒数
long maxMillis = System.currentTimeMillis() + millisToMax;// 根据当前时间算出的最大nano对应的millis时间
Calendar c = Calendar.getInstance();
c.setTimeInMillis(maxMillis);
System.out.println(c.get(Calendar.YEAR));

运行结果是:2308
2308 – 2016 = 292
这个数字居然跟人家描述里说得一样,总觉得哪不对~ 好吧,还有两百多年可以用,我们也都不会见到那一刻了~

postgresql用于序列号生成器备忘

2016年4月6日 没有评论

第一步,安装:

yum install postgresql-server

第二步,指定并初始化目录

cd /data/
mkdir psqldata
chown postgres:postgres psqldata
su - postgres
initdb /data/psqldata/

第三步,修改基本配置postgresql.conf

vi /data/psqldata/postgresql.conf
#改几个基本配置listen_addresses、port、max_connections

vi /data/psqldata/pg_hba.conf
#在IPv4 local connections:下增加相应IP的授权

更多内容…

Spring with tomcat中文乱码问题

2016年3月11日 没有评论

spring with tomcat,中文乱码,相关版本:

<spring-framework.version>4.0.8.RELEASE</spring-framework.version>
<spring-boot.version>1.1.9.RELEASE</spring-boot.version>

在使用request之前对其设置utf-8编码没效果,debug时发现,其charset为iso-8859-1。

更多内容…

分类: 工作 标签: , ,

MySQL的max_user_connections限制

2016年3月11日 没有评论

用了某山云服务的RDS服务,其实就是MySQL服务。扩容时碰到连接超限的错误。

User xxx already has more than 'max_user_connections' active connections

查到的结果是总连接限制数是200,单用户限制为20

mysql> select @@max_user_connections;
+------------------------+
| @@max_user_connections |
+------------------------+
|                    20 |
+------------------------+
1 row in set (0.00 sec)

mysql> select @@max_connections;
+-------------------+
| @@max_connections |
+-------------------+
|               200 |
+-------------------+
1 row in set (0.00 sec)

用show processlist;查到的当前用户连接数已经超出了20,所以受限了。
更多内容…

Spring集成tomcat之Cookie禁用

2016年2月22日 没有评论

线上服务器发现有Cookie的解析异常,而我们又不需要用Cookie(我们自己管理会话,提供无状态http api服务),准备禁用cookie。异常信息如下:

2016-02-21 13:37:21.248 ERROR 7114 [http-nio-xxxx-exec-121] --- o.a.coyote.http11.Http11NioProcessor     : Error processing request
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
        at org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:193)
        at org.apache.tomcat.util.http.Cookies.getTokenEndPosition(Cookies.java:502)
        at org.apache.tomcat.util.http.Cookies.processCookieHeader(Cookies.java:349)
        at org.apache.tomcat.util.http.Cookies.processCookies(Cookies.java:168)
        at org.apache.tomcat.util.http.Cookies.getCookieCount(Cookies.java:106)
        at org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId(CoyoteAdapter.java:1010)
        at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:764)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:416)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)

Tomcat禁用cookie相对简单,改conf/context.xml中的Context配置就成:

<Context cookies = "false">...</Context>

我们用的是Spring集成的Tomcat,对tomcat的可配置参数非常有限,当然了,没有这个禁用cookie的选项:

server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.

完整参数配置参见:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix
更多内容…

分类: 工作 标签: , ,