{"id":627,"date":"2025-12-07T15:43:40","date_gmt":"2025-12-07T07:43:40","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=627"},"modified":"2025-12-07T15:43:40","modified_gmt":"2025-12-07T07:43:40","slug":"net_core%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e8%bf%81%e7%a7%bb%e5%88%b0%e4%ba%91%e7%ab%af%e5%90%8e%e8%87%aa%e5%8a%a8%e5%8c%96%e9%85%8d%e7%bd%ae%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f%e7%9a%84%e6%96%b9","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net_core%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e8%bf%81%e7%a7%bb%e5%88%b0%e4%ba%91%e7%ab%af%e5%90%8e%e8%87%aa%e5%8a%a8%e5%8c%96%e9%85%8d%e7%bd%ae%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f%e7%9a%84%e6%96%b9\/","title":{"rendered":".net_core\u5e94\u7528\u7a0b\u5e8f\u8fc1\u79fb\u5230\u4e91\u7aef\u540e\u81ea\u52a8\u5316\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60 <strong>\u6700\u9002\u5408\u6280\u672f\u535a\u5ba2 \/ \u5c0f\u7ea2\u4e66 \/ YouTube \u6280\u672f\u5411\u77ed\u89c6\u9891\u811a\u672c\u98ce\u683c\u7684\u5185\u5bb9<\/strong>\uff0c\u56f4\u7ed5<br><strong>\u201c.NET Core \u5e94\u7528\u8fc1\u79fb\u5230\u4e91\u7aef\u540e\u81ea\u52a8\u5316\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\uff08\u6700\u65b0\uff09\u201d<\/strong><br>\u63d0\u4f9b<strong>\u7cbe\u7b80\u6613\u61c2 + \u53ef\u76f4\u63a5\u590d\u5236\u793a\u4f8b\u4ee3\u7801 + \u4e91\u73af\u5883\u901a\u7528\u65b9\u6848\uff08Linux \/ Docker \/ K8s \/ \u4e91\u670d\u52a1\u5546\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 .NET Core \u5e94\u7528\u8fc1\u79fb\u5230\u4e91\u7aef\u540e\u81ea\u52a8\u5316\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\uff08\u6700\u65b0\uff09<\/h1>\n\n\n\n<p>\u5728\u628a .NET Core \u5e94\u7528\u8fc1\u79fb\u5230\u4e91\u7aef\uff08\u963f\u91cc\u4e91\u3001\u817e\u8baf\u4e91\u3001AWS\u3001Docker\u3001K8s \u7b49\uff09\u65f6\uff0c<strong>\u81ea\u52a8\u5316\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong>\u975e\u5e38\u5173\u952e\uff0c\u5c24\u5176\u7528\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32<\/li>\n\n\n\n<li>Redis \/ Kafka \/ MQ \u5730\u5740<\/li>\n\n\n\n<li>JWT \u5bc6\u94a5<\/li>\n\n\n\n<li>API Key &amp; Secret<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u4e1a\u52a1\u914d\u7f6e\u9879<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u6574\u7406 <strong>\u6700\u65b0\u6700\u901a\u7528\u7684\u81ea\u52a8\u5316\u65b9\u6848<\/strong>\uff0c\u4ece\u5e94\u7528\u5185\u90e8 \u2192 \u90e8\u7f72\u5c42 \u2192 \u4e91\u5e73\u53f0\u5c42\u5168\u8986\u76d6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e00\u3001\u5e94\u7528\u5c42\u81ea\u52a8\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\uff08\u63a8\u8350\u5fc5\u987b\u5f00\u542f\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Program.cs\uff08.NET 6 \/ 7 \/ 8 \u901a\u7528\uff09<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar builder = WebApplication.CreateBuilder(args);\n\n\/\/ \u6dfb\u52a0\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u6e90\uff08\u9ed8\u8ba4\u5f00\u542f\uff0c\u53ef\u518d\u6b21\u786e\u8ba4\uff09\nbuilder.Configuration.AddEnvironmentVariables();\n\n\/\/ \u83b7\u53d6\u914d\u7f6e\nvar dbConn = builder.Configuration&#x5B;&quot;DB_CONNECTION&quot;];\nvar jwtKey = builder.Configuration&#x5B;&quot;JWT_KEY&quot;];\n\nConsole.WriteLine(&quot;DB=&quot; + dbConn);\nConsole.WriteLine(&quot;JWT=&quot; + jwtKey);\n\nvar app = builder.Build();\napp.Run();\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u4e91\u7aef\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u540e\uff0c\u7a0b\u5e8f\u542f\u52a8\u65f6\u81ea\u52a8\u8bfb\u53d6<br>\u2714 \u65e0\u9700\u6539\u4ee3\u7801\u7ed3\u6784<br>\u2714 \u591a\u73af\u5883\uff08dev\/test\/prod\uff09\u81ea\u52a8\u5207\u6362<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e8c\u3001Linux \u670d\u52a1\u5668\u4e0a\u81ea\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff08Systemd \u81ea\u542f\u52a8\u573a\u666f\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1\uff09\u521b\u5efa service \u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p><code>\/etc\/systemd\/system\/myapp.service<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;Service]\nEnvironment=DB_CONNECTION=Server=10.0.0.1;User=prod;Pwd=123\nEnvironment=JWT_KEY=8848abcdxxxx\n\nWorkingDirectory=\/var\/www\/myapp\nExecStart=\/usr\/bin\/dotnet \/var\/www\/myapp\/MyApp.dll\nRestart=always\n\n&#x5B;Install]\nWantedBy=multi-user.target\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2\uff09\u91cd\u8f7d\u5e76\u542f\u52a8<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo systemctl daemon-reload\nsudo systemctl enable myapp\nsudo systemctl restart myapp\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u6bcf\u6b21\u4fee\u6539\u73af\u5883\u53d8\u91cf\u65e0\u9700\u6539\u4ee3\u7801<br>\u2714 \u81ea\u52a8\u751f\u6548<br>\u2714 \u6743\u9650\u66f4\u5b89\u5168\uff0c\u4e0d\u5fc5\u5199\u5728 appsettings.json<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e09\u3001Docker \u81ea\u52a8\u6ce8\u5165\u73af\u5883\u53d8\u91cf\uff08\u6700\u5e38\u7528\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1\uff09Dockerfile \u4e2d\u65e0\u9700\u6539\u4efb\u4f55\u914d\u7f6e<\/strong><\/h3>\n\n\n\n<p>\u53ea\u8981\u4f7f\u7528\u9ed8\u8ba4\u7684\u73af\u5883\u53d8\u91cf\u52a0\u8f7d\u5668\u5c31\u53ef\u4ee5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2\uff09docker run \u81ea\u52a8\u6ce8\u5165\u53d8\u91cf<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndocker run -d \\\n  -e DB_CONNECTION=&quot;Server=db;User=prod;Pwd=123&quot; \\\n  -e JWT_KEY=&quot;jwt_prod_xxx&quot; \\\n  --name myapp \\\n  myapp:latest\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u56db\u3001docker-compose \u81ea\u52a8\u5316\uff08\u63a8\u8350\uff09<\/h1>\n\n\n\n<p><code>docker-compose.yml<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nservices:\n  web:\n    image: myapp:latest\n    environment:\n      DB_CONNECTION: &quot;Server=db.internal;User=prod;Pwd=123&quot;\n      JWT_KEY: &quot;jwt_prod_key_xxx&quot;\n    ports:\n      - &quot;80:80&quot;\n\n<\/pre><\/div>\n\n\n<p>\u4e00\u6761\u547d\u4ee4\u90e8\u7f72\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndocker compose up -d\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e94\u3001Kubernetes \u81ea\u52a8\u5316\uff08\u751f\u4ea7\u7ea7\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e00\u822c\u4f7f\u7528 ConfigMap + Secret<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1\uff09\u521b\u5efa ConfigMap\uff08\u666e\u901a\u914d\u7f6e\uff09<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: myapp-config\ndata:\n  DB_CONNECTION: &quot;Server=10.0.0.2;User=prod;Pwd=123&quot;\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">2\uff09\u521b\u5efa Secret\uff08\u654f\u611f\u4fe1\u606f\uff09<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napiVersion: v1\nkind: Secret\nmetadata:\n  name: myapp-secret\nstringData:\n  JWT_KEY: &quot;jwt_prod_key_xxx&quot;\n\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">3\uff09Deployment \u81ea\u52a8\u6ce8\u5165<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nenv:\n  - name: DB_CONNECTION\n    valueFrom:\n      configMapKeyRef:\n        name: myapp-config\n        key: DB_CONNECTION\n\n  - name: JWT_KEY\n    valueFrom:\n      secretKeyRef:\n        name: myapp-secret\n        key: JWT_KEY\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u516d\u3001\u963f\u91cc\u4e91 \/ \u817e\u8baf\u4e91 \/ AWS \u81ea\u52a8\u5316\u65b9\u5f0f<\/h1>\n\n\n\n<p>\u6240\u6709\u4e91\u5382\u5546\u90fd\u652f\u6301\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4e91\u5e73\u53f0<\/th><th>\u81ea\u52a8\u6ce8\u5165\u65b9\u5f0f<\/th><th>\u662f\u5426\u63a8\u8350<\/th><\/tr><\/thead><tbody><tr><td>\u963f\u91cc\u4e91 ECS<\/td><td>Systemd \u73af\u5883\u53d8\u91cf<\/td><td>\u2b50\u2b50\u2b50\u2b50<\/td><\/tr><tr><td>\u817e\u8baf\u4e91 Lighthouse<\/td><td>Systemd \u73af\u5883\u53d8\u91cf<\/td><td>\u2b50\u2b50\u2b50\u2b50<\/td><\/tr><tr><td>\u963f\u91cc\u4e91\u51fd\u6570\u8ba1\u7b97 FC<\/td><td>\u73af\u5883\u53d8\u91cf\u754c\u9762\u6ce8\u5165<\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50<\/td><\/tr><tr><td>AWS Lambda<\/td><td>Managed Environment Variables<\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50<\/td><\/tr><tr><td>\u5bb9\u5668\u670d\u52a1\uff08ACK\/EKS\/TKE\uff09<\/td><td>ConfigMap + Secret<\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2714 \u4e0d\u9700\u8981\u5199\u5165 appsettings.json<br>\u2714 \u81ea\u52a8\u7ba1\u7406\u73af\u5883<br>\u2714 \u591a\u73af\u5883\u914d\u7f6e\u4e00\u952e\u5207\u6362<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e03\u3001\u81ea\u52a8\u68c0\u6d4b\u5e76\u4fee\u590d\u73af\u5883\u53d8\u91cf\uff08.NET \u5185\u7f6e\u65b9\u6848\uff09<\/h1>\n\n\n\n<p>\u53ef\u5728\u542f\u52a8\u65f6\u81ea\u52a8\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u5b58\u5728\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nstring&#x5B;] requiredKeys = { &quot;DB_CONNECTION&quot;, &quot;JWT_KEY&quot; };\n\nforeach (var key in requiredKeys)\n{\n    if (string.IsNullOrWhiteSpace(builder.Configuration&#x5B;key]))\n    {\n        Console.WriteLine($&quot;&#x5B;ERROR] \u7f3a\u5c11\u73af\u5883\u53d8\u91cf\uff1a{key}&quot;);\n        throw new Exception($&quot;\u7f3a\u5c11\u5fc5\u987b\u7684\u73af\u5883\u53d8\u91cf {key}&quot;);\n    }\n}\n\nvar app = builder.Build();\napp.Run();\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u7f3a\u5565\u62a5\u9519<br>\u2714 \u9632\u6b62\u90e8\u7f72\u540e\u51fa\u95ee\u9898<br>\u2714 \u4e91\u7aef CI\/CD \u81ea\u52a8\u68c0\u67e5<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 \u603b\u7ed3\uff08\u9002\u5408\u77ed\u89c6\u9891 15 \u79d2\u6587\u6848\uff09<\/h1>\n\n\n\n<p><strong>\u201c.NET Core \u8fc1\u79fb\u4e91\u7aef\uff0c\u73af\u5883\u53d8\u91cf\u4e00\u5b9a\u4e0d\u8981\u5199\u6b7b\uff01\u201d<\/strong><br>\u6700\u63a8\u8350\u7684\u505a\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5e94\u7528\u5185\u90e8\u8bfb\u53d6 Environment<\/strong><\/li>\n\n\n\n<li><strong>Linux \u7528 systemd \u6ce8\u5165<\/strong><\/li>\n\n\n\n<li><strong>Docker \u7528 -e \u6216 compose \u6ce8\u5165<\/strong><\/li>\n\n\n\n<li><strong>K8s \u7528 ConfigMap + Secret \u6ce8\u5165<\/strong><\/li>\n\n\n\n<li><strong>\u542f\u52a8\u65f6\u81ea\u52a8\u68c0\u67e5\u7f3a\u5931\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u6837\u4f60\u7684\u4e91\u7aef\u90e8\u7f72\uff1a<\/p>\n\n\n\n<p>\u2714 \u65e0\u9700\u4fee\u6539 appsettings<br>\u2714 \u968f\u6539\u968f\u751f\u6548<br>\u2714 \u4fdd\u5bc6\u5b89\u5168<br>\u2714 \u652f\u6301\u5927\u89c4\u6a21\u6269\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60 \u6700\u9002\u5408\u6280\u672f\u535a\u5ba2 \/ \u5c0f\u7ea2\u4e66 \/ YouTube \u6280\u672f\u5411\u77ed\u89c6\u9891\u811a\u672c\u98ce\u683c\u7684&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/net_core%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e8%bf%81%e7%a7%bb%e5%88%b0%e4%ba%91%e7%ab%af%e5%90%8e%e8%87%aa%e5%8a%a8%e5%8c%96%e9%85%8d%e7%bd%ae%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f%e7%9a%84%e6%96%b9\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-627","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":628,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/627\/revisions\/628"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}