上篇文章JAVA单服务应用拆分成多个服务的实践(2)--服务的dubbo化已经将部分模块微服务化了,但我们怎么测试?
我们的目标是支持ALL In One,又要支持多个微服务,但前端怎么处理,前端代码又只有一个版本,但又要怎么面向多个微服务呢。
这样的话,我们只能引入伟大的nginx。
前端只认一个,就使用nginx的转发,将特定的请求转发到微服务的接口里,让前端无感请求到到另一服务中。
nginx的配置如下:
upstream auth {
server 127.0.0.1:9082;
}
upstream org {
server 127.0.0.1:9081;
}
upstream log {
server 127.0.0.1:9083;
}
upstream dict {
server 127.0.0.1:9084;
}
upstream form {
server 127.0.0.1:9085;
}
upstream workflow {
server 127.0.0.1:9086;
}
upstream attachment {
server 127.0.0.1:9087;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://workflow/oa_workflow/;
}
location ^~ /sys/auth {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://auth/sys_auth/sys/auth;
}
location ^~ /sys/org {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://org/sys_org/sys/org;
}
location ^~ /sys/log {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://log/sys_log/sys/log;
}
location ^~ /sys/dict {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://dict/sys_dict/sys/dict;
}
location ^~ /oa/form {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://form/oa_form/oa/form;
}
location ^~ /oa/workflow {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://workflow/oa_workflow/oa/workflow;
}
location ^~ /sys/attachment {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://attachment/sys_attachment/sys/attachment;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
按上所示,按前端请求的URL地址转发到各个服务中,有一点要特别提到的,关于/的转发,后面一定要有/
如下:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://workflow/oa_workflow/;
}
oa_workflow后面一定要有/,这个是默认转发,之前缺了这个,折腾了我很久。
至此,前端调整完成。
【未完待续】
网友评论