{"id":225352,"date":"2021-09-14T08:00:27","date_gmt":"2021-09-14T00:00:27","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=225352"},"modified":"2021-08-26T09:01:27","modified_gmt":"2021-08-26T01:01:27","slug":"nginx-consumer-docker","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-consumer-docker.html","title":{"rendered":"nginx+consul\u505a\u52a8\u6001\u8d1f\u8f7d\u5747\u8861(docker)"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8d1f\u8f7d\u5747\u8861\u5efa\u7acb\u5728\u73b0\u6709\u7f51\u7edc\u7ed3\u6784\u4e4b\u4e0a\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u5ec9\u4ef7\u6709\u6548\u900f\u660e\u7684\u65b9\u6cd5\u6269\u5c55\u7f51\u7edc\u8bbe\u5907\u548c\u670d\u52a1\u5668\u7684\u5e26\u5bbd\u3001\u589e\u52a0\u541e\u5410\u91cf\u3001\u52a0\u5f3a\u7f51\u7edc\u6570\u636e\u5904\u7406\u80fd\u529b\u3001\u63d0\u9ad8\u7f51\u7edc\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7528\u6027\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1 consul<\/strong><\/div>\n
1.1 consul\u7b80\u4ecb<\/strong><\/span><\/div>\n

Consul \u662f\u4e00\u4e2a\u652f\u6301\u591a\u6570\u636e\u4e2d\u5fc3\u5206\u5e03\u5f0f\u9ad8\u53ef\u7528\u7684\u670d\u52a1\u53d1\u73b0\u548c\u914d\u7f6e\u5171\u4eab\u7684\u670d\u52a1\u8f6f\u4ef6\u3002 \u670d\u52a1\u53d1\u73b0\u4ee5\u53ca\u6ce8\u518c\uff1a\u5f53\u670d\u52a1Producer \u542f\u52a8\u65f6\uff0c\u4f1a\u5c06\u81ea\u5df1\u7684Ip\/host\u7b49\u4fe1\u606f\u901a\u8fc7\u53d1\u9001\u8bf7\u6c42\u544a\u77e5 Consul\uff0cConsul \u63a5\u6536\u5230 Producer \u7684\u6ce8\u518c\u4fe1\u606f\u540e\uff0c\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u4f1a\u5411 Producer \u53d1\u9001\u4e00\u4e2a\u5065\u5eb7\u68c0\u67e5\u7684\u8bf7\u6c42\uff0c\u68c0\u9a8cProducer\u662f \u5426\u5065\u5eb7\u3002
\n\u670d\u52a1\u8c03\u7528\uff1a\u5f53 Consumer \u8bf7\u6c42Product\u65f6\uff0c\u4f1a\u5148\u4ece Consul \u4e2d\u62ff\u5230\u5b58\u50a8Product\u670d\u52a1\u7684 IP \u548c Port \u7684\u4e34\u65f6\u8868 (temp table)\uff0c\u4ecetemp table\u8868\u4e2d\u4efb\u9009\u4e00\u4e2a\u00b7 Producer \u7684 IP \u548c Port\uff0c \u7136\u540e\u6839\u636e\u8fd9\u4e2aIP\u548cPort\uff0c\u53d1\u9001\u8bbf\u95ee\u8bf7 \u6c42\uff1btemp table\u8868\u53ea\u5305\u542b\u901a\u8fc7\u4e86\u5065\u5eb7\u68c0\u67e5\u7684 Producer \u4fe1\u606f\uff0c\u5e76\u4e14\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u66f4\u65b0\u3002<\/p>\n

1.2 consul\u5b89\u88c5<\/strong><\/span><\/div>\n

\u62c9\u53d6consul\u955c\u50cf<\/p>\n

\r\ndocker pull consul\r\n<\/pre>\n
2.\u6784\u5efaconsul\u5bb9\u5668<\/strong><\/div>\n
\r\ndocker run -p 8500:8500 -d --name consul -v \/docker\/consul\/data:\/consul\/data -- privileged=true \u2013e CONSUL_BIND_INTERFACE='eth0'consul agent -server -bootstrap-expect 1 -data-dir \/consul\/data -node=ali -ui -client='0.0.0.0'\r\n<\/pre>\n

\u53c2\u6570\u542b\u4e49\uff1a
\nagent -server\u8868\u793a\u542f\u52a8\u7684\u662f\u4e00\u4e2a\u670d\u52a1
\n-bootstrap-expect 1 \u8868\u793a\u7b49\u5f85\u591a\u5c11\u4e2a\u8282\u70b9\u518d\u542f\u52a8\uff0c\u8fd9\u91cc1\u4e2a\uff0c\u5c31\u662f\u81ea\u5df1\u4e00\u4e2a\u5c31\u542f\u52a8\u4e86
\n-node=texun_1 \u5c31\u662f\u7ed9consul\u670d\u52a1\u8d77\u4e2a\u522b\u540d\u4e3aali
\n-data-dir \/opt\/data \u6570\u636e\u5b58\u50a8\u76ee\u5f55\u4e3a\/opt\/data
\n-ui \u542f\u52a8\u9ed8\u8ba4ui\u754c\u9762
\n-client consul\u7ed1\u5b9a\u5728\u54ea\u4e2aclient\u5730\u5740\u4e0a\uff0c\u8fd9\u4e2a\u5730\u5740\u63d0\u4f9bHTTP\u3001DNS\u3001RPC\u7b49\u670d\u52a1\uff0c\u9ed8\u8ba4\u662f 127.0.0.1\uff0c\u53ef\u6307\u5b9a\u5141\u8bb8\u5ba2\u6237\u7aef\u4f7f\u7528\u4ec0\u4e48ip\u53bb\u8bbf\u95ee<\/p>\n

2 nginx\u5b89\u88c5nginx-upsync-module\u6a21\u5757<\/strong><\/span><\/div>\n

1.\u5c06\u51c6\u5907\u7684\u7d20\u6750copy\u5230nginx\u5bb9\u5668<\/p>\n

\r\ndocker cp \/home\/nginx-upsync-module-master.zip nginx:\/home\/ \r\ndocker cp \/home\/nginx-1.21.0.tar.gz nginx:\/home\/\r\n<\/pre>\n

2.\u8fdb\u5165\u5bb9\u5668\u89e3\u538b\u5b89\u88c5\u5305<\/p>\n

\r\nunzip nginx-upsync-module-master.zip \r\ntar -zxvf nginx-1.21.0.tar.gz\r\n<\/pre>\n

3.\u5b89\u88c5\u76f8\u5173\u6269\u5c55<\/p>\n

\r\napt-get install -y gcc autoconf automake make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev\r\n<\/pre>\n

4.\u8fdb\u5165\u89e3\u538b\u7684nginx\u5b89\u88c5\u5305\uff0c\u8fdb\u884c\u7f16\u8bd1\u5b89\u88c5<\/p>\n

\r\ncd \/home\/nginx-1.21.0\r\n\r\n\r\n#\u901a\u8fc7\u547d\u4ee4\u67e5\u770bnginx\u7248\u672c\u4e0e\u7f16\u8bd1\u4fe1\u606f\r\nnginx -V\r\n\r\n#\u590d\u5236\u76f8\u5173\u7f16\u8bd1\u4fe1\u606f\uff0c\u6dfb\u52a0nginx- upsync-module-master\u6a21\u5757\r\n.\/configure --prefix=\/etc\/nginx --sbin-path=\/usr\/sbin\/nginx --modules-path=\/usr\/lib\/nginx\/modules --conf-path=\/etc\/nginx\/nginx.conf --error-log-path=\/var\/log\/nginx\/error.log --http-log-path=\/var\/log\/nginx\/access.log --pid-path=\/var\/run\/nginx.pid --lock-path=\/var\/run\/nginx.lock --http-client-body-temp-path=\/var\/cache\/nginx\/client_temp --http-proxy-temp-path=\/var\/cache\/nginx\/proxy_temp --http-fastcgi-temp-path=\/var\/cache\/nginx\/fastcgi_temp --http-uwsgi-temp-path=\/var\/cache\/nginx\/uwsgi_temp --http-scgi-temp-path=\/var\/cache\/nginx\/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=\/data\/builder\/debuild\/nginx-1.21.0\/debian\/debuild-base\/nginx-1.21.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=\/home\/nginx-upsync-module-master\r\n<\/pre>\n

5.\u7f16\u8bd1\u5b89\u88c5<\/p>\n

\r\nmake && make install\r\n<\/pre>\n
3 nginx\u52a8\u6001\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e<\/strong><\/div>\n
\r\nupstream edu{\r\n    server 192.168.35.138;\r\n    upsync 172.17.0.3:8500\/v1\/kv\/upstreams\/nginx_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;\r\n    upsync_dump_path \/etc\/nginx\/conf.d\/server_test.conf;\r\n    include \/etc\/nginx\/conf.d\/server_test.conf;\r\n}\r\nserver {\r\n    listen       80;\r\n    listen  [::]:80;\r\n    server_name  localhost;\r\n    root \/docker\/www\/;\r\n    index index.php index.html;\r\n\r\n    location \/ {\r\n      proxy_pass http:\/\/edu;\r\n    }\r\n}\r\n<\/pre>\n

\u5f80consul\u52a0\u5165nginx\u670d\u52a1<\/p>\n

\r\ncurl -X PUT -d '{\"weight\":1,\"max_fails\":2,\"fail_timeout\":10}' http:\/\/192.168.35.139:8500\/v1\/kv\/upstreams\/nginx_test\/192.168.35.131:81\r\n\r\ncurl -X PUT -d '{\"weight\":1,\"max_fails\":2,\"fail_timeout\":10}' http:\/\/192.168.35.139:8500\/v1\/kv\/upstreams\/nginx_test\/192.168.35.131:80\r\n<\/pre>\n

\u63a5\u7740\u67e5\u770bserver_test.conf\u6587\u4ef6,\u770b\u770bconsul\u662f\u5426\u6709\u5c06\u6211\u4eec\u65b0\u589e\u7684nginx\u670d\u52a1\u52a0\u5165\u5230\u8d1f\u8f7d\u5747\u8861\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Consul \u662f\u4e00\u4e2a\u652f\u6301\u591a\u6570\u636e\u4e2d\u5fc3\u5206\u5e03\u5f0f\u9ad8\u53ef\u7528\u7684\u670d\u52a1\u53d1\u73b0\u548c\u914d\u7f6e\u5171\u4eab\u7684\u670d\u52a1\u8f6f\u4ef6\u3002 \u670d\u52a1\u53d1\u73b0\u4ee5\u53ca\u6ce8\u518c\uff1a\u5f53\u670d\u52a1Pro […]<\/p>\n","protected":false},"author":1903,"featured_media":225362,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-225352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=225352"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225352\/revisions"}],"predecessor-version":[{"id":225397,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/225352\/revisions\/225397"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/225362"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=225352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=225352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=225352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}