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性能比較
決定自己測(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。