周記 2017.12.4 - 12.10

Eureka 主機(jī)名還是ip

我們把每個(gè)java進(jìn)程部署在一個(gè)Docker容器中,子服務(wù)通過(guò)注冊(cè)中心找到配置中心,然后訪問(wèn)配置中心拿到對(duì)應(yīng)的配置信息。
之前子服務(wù)在物理機(jī)部署是沒(méi)有問(wèn)題的,但是在容器中出錯(cuò),日志信息是:

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

可以看到訪問(wèn)地址是別名(iZ2zehfslfx46hf71lp8zzZ),而這個(gè)別名可以在配置中心所在容器的hosts文件中查到。

ip_address iZ2zehfslfx46hf71lp8zzZ

子服務(wù)所在容器hosts里面并沒(méi)有這個(gè)對(duì)應(yīng)關(guān)系,所以訪問(wèn)不到配置中心。
解決方案是讓子服務(wù)優(yōu)先使用IP而不是主機(jī)名來(lái)訪問(wèn)。

eureka.instance.prefer-ip-address=true

在添加配置之后問(wèn)題解決。

Mysql分頁(yè)查詢優(yōu)化

內(nèi)容節(jié)選自阿里<逆流而上:阿里巴巴技術(shù)成長(zhǎng)之路>

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萬(wàn)條記錄,sellerid從1-10,每個(gè)id大約在20萬(wàn)左右數(shù)據(jù)。

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

查詢耗時(shí)280ms左右。

如何對(duì)這條sql進(jìn)行優(yōu)化呢?

Mysql翻頁(yè)是LIMIT M, N,即查詢出M+N條記錄,然后取后N條記錄,所以M越大性能越差。

查看下sql執(zhí)行計(jì)劃

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

可以發(fā)現(xiàn)當(dāng)把 * 改為 id時(shí),直接通過(guò)索引獲取結(jié)果,不再需要拿到所有字段,可以加快查詢速度。

那么優(yōu)化后

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

優(yōu)化后的sql查詢耗時(shí)在52ms左右,性能有了明顯的提升。

其實(shí)在數(shù)據(jù)量不大的情況下,2種方式看起來(lái)差別不大,但是當(dāng)數(shù)據(jù)量上去之后,好的方法和差的方法變有了非常顯著的區(qū)別;所以平時(shí)開(kāi)發(fā)時(shí)要注意細(xì)節(jié),著眼于更好的性能,提升自己的開(kāi)發(fā)水準(zhǔn)。

Nginx vs Zuul性能比較

參考: 國(guó)外測(cè)評(píng) | 國(guó)內(nèi)翻譯文章

決定自己測(cè)試下看看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";
        }
    }
}

僅僅提供一個(gè)最簡(jiǎn)單的接口供外部訪問(wèn)。

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 配置轉(zhuǎn)發(fā)

 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;
   }
  }
}

僅僅配置簡(jiǎn)單的轉(zhuǎn)發(fā)功能。

測(cè)試

使用ab來(lái)對(duì)接口進(jìn)行簡(jiǎn)單測(cè)試。

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)

對(duì)比發(fā)現(xiàn),兩者性能已經(jīng)很接近了;比較如下,Nginx優(yōu)點(diǎn)是占用內(nèi)存小,Zuul優(yōu)點(diǎn)是功能強(qiáng)大,可以進(jìn)行鑒權(quán)等操作,同時(shí)基于Spring Cloud生態(tài)圈,所以完全可以使用Zuul代替Nginx。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,765評(píng)論 18 399
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂(lè)視家的研發(fā)崗...
    時(shí)芥藍(lán)閱讀 42,366評(píng)論 11 349
  • 15年那是第幾輩子不計(jì)算了,但我記得這是我人生好像找到方向得一年,當(dāng)時(shí)還不清楚也是隨大流跟風(fēng),對(duì)一夜暴富的...
    未來(lái)LIVE閱讀 239評(píng)論 0 0
  • 書(shū) 必須完成Swift與Cocoa框架開(kāi)發(fā)iOS開(kāi)發(fā)進(jìn)階國(guó)史大綱(上下)古拉格群島(上中下)UNIX 入門(mén)經(jīng)典教父...
    惟有進(jìn)步值得信仰閱讀 236評(píng)論 0 0