周记 2017.12.4 - 12.10

Eureka 主机名还是ip

我们把每个java进程部署在一个Docker容器中,子服务通过注册中心找到配置中心,然后访问配置中心拿到对应的配置信息。
之前子服务在物理机部署是没有问题的,但是在容器中出错,日志信息是:

2017-12-04 19:54:44.213  INFO [xxx,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://iZ2zehfslfx46hf71lp8zzZ:9910/

可以看到访问地址是别名(iZ2zehfslfx46hf71lp8zzZ),而这个别名可以在配置中心所在容器的hosts文件中查到。

ip_address iZ2zehfslfx46hf71lp8zzZ

子服务所在容器hosts里面并没有这个对应关系,所以访问不到配置中心。
解决方案是让子服务优先使用IP而不是主机名来访问。

eureka.instance.prefer-ip-address=true

在添加配置之后问题解决。

Mysql分页查询优化

内容节选自阿里<逆流而上:阿里巴巴技术成长之路>

table info

CREATE TABLE `t_test_buyer` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(10) DEFAULT NULL,
  `sellerid` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `selleridIndex` (`sellerid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

这张表有200万条记录,sellerid从1-10,每个id大约在20万左右数据。

select * from t_test_buyer where sellerid=1 limit 200000,5000

查询耗时280ms左右。

如何对这条sql进行优化呢?

Mysql翻页是LIMIT M, N,即查询出M+N条记录,然后取后N条记录,所以M越大性能越差。

查看下sql执行计划

explain select * from t_test_buyer where sellerid=1 limit 200000,5000

1   SIMPLE  t_test_buyer    ref selleridIndex   selleridIndex   4   const   528736  NULL

如果把 * 改为 id

explain select SQL_NO_CACHE id from t_test_buyer where sellerid=1 limit 200000,5000

1   SIMPLE  t_test_buyer    ref selleridIndex   selleridIndex   4   const   528736  Using index

可以发现当把 * 改为 id时,直接通过索引获取结果,不再需要拿到所有字段,可以加快查询速度。

那么优化后

select SQL_NO_CACHE t1.* from t_test_buyer t1, (select id from t_test_buyer where sellerid=1 limit 200000,5000) t2 where t1.id=t2.id

1   PRIMARY <derived2>  ALL NULL    NULL    NULL    NULL    205000  NULL
1   PRIMARY t1  eq_ref  PRIMARY PRIMARY 4   t2.id   1   NULL
2   DERIVED t_test_buyer    ref selleridIndex   selleridIndex   4   const   528736  Using index

优化后的sql查询耗时在52ms左右,性能有了明显的提升。

其实在数据量不大的情况下,2种方式看起来差别不大,但是当数据量上去之后,好的方法和差的方法变有了非常显著的区别;所以平时开发时要注意细节,着眼于更好的性能,提升自己的开发水准。

Nginx vs Zuul性能比较

参考: 国外测评 | 国内翻译文章

决定自己测试下看看2者性能之间差异。

Web

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class TestMvcApplication {

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

    @RestController
    class HelloController {

        @RequestMapping("/")
        public String hello() {
            return "hello";
        }
    }
}

仅仅提供一个最简单的接口供外部访问。

Zuul

POM文件中需要引入Cloud依赖。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class TestZuulApplication {

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

application.yml 配置转发

 zuul:
  routes:
    users:
      path: /**
      url: http://ip:8081
 server:
   port: 8082

Nginx

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  1024;
}


http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

 server {
   listen 8083 default_server;
   listen [::]:8083 default_server ipv6only=on;

   # Make site accessible from http://localhost/
   server_name localhost;

   # allow file upload
   client_max_body_size 10M;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_pass http://ip:8081;
   }
  }
}

仅仅配置简单的转发功能。

测试

使用ab来对接口进行简单测试。

ab -n 10000 -c 200 http://ip:port/

Nginx

Server Software:        nginx/1.13.7
Server Hostname:        59.110.14.254
Server Port:            8083

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      200
Time taken for tests:   51.452 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1593657 bytes
HTML transferred:       50115 bytes
Requests per second:    194.36 [#/sec] (mean)
Time per request:       1029.043 [ms] (mean)
Time per request:       5.145 [ms] (mean, across all concurrent requests)
Transfer rate:          30.25 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  134 419.9      1    5524
Processing:     2  285 1993.2      2   51389
Waiting:        2  264 1994.7      2   51389
Total:          3  420 2041.7      5   51390

Percentage of the requests served within a certain time (ms)
  50%      5
  66%     28
  75%    207
  80%    209
  90%   1006
  95%   1424
  98%   3043
  99%   6280
 100%  51390 (longest request)

Zuul

Server Software:
Server Hostname:        59.110.14.254
Server Port:            8082

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      200
Time taken for tests:   51.454 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1590636 bytes
HTML transferred:       50020 bytes
Requests per second:    194.35 [#/sec] (mean)
Time per request:       1029.074 [ms] (mean)
Time per request:       5.145 [ms] (mean, across all concurrent requests)
Transfer rate:          30.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  105 346.3      1    5267
Processing:     2  406 3231.0      2   51403
Waiting:        2  389 3232.5      2   51403
Total:          3  512 3247.6      4   51405

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      7
  75%    206
  80%    208
  90%   1003
  95%   1209
  98%   3042
  99%   7288
 100%  51405 (longest request)

对比发现,两者性能已经很接近了;比较如下,Nginx优点是占用内存小,Zuul优点是功能强大,可以进行鉴权等操作,同时基于Spring Cloud生态圈,所以完全可以使用Zuul代替Nginx。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,563评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,531评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,159评论 11 349
  • 15年那是第几辈子不计算了,但我记得这是我人生好像找到方向得一年,当时还不清楚也是随大流跟风,对一夜暴富的...
    未来LIVE阅读 214评论 0 0
  • 书 必须完成Swift与Cocoa框架开发iOS开发进阶國史大綱(上下)古拉格群岛(上中下)UNIX 入门经典教父...
    惟有进步值得信仰阅读 226评论 0 0