[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"solution-scenes-menu":3,"tech-detail-2360":4},true,{"id":5,"title":6,"content":7,"picUrl":8,"tagIds":9,"keyWords":10,"htmlDescribe":11,"htmlUrl":12,"gmtCreate":13,"gmtModified":13,"articleInfoId":14},"2360","美畅物联丨Java 后端程序指定运行环境配置","\u003Cp style=\"text-align:justify\">畅联云平台 Java 后端环境配置技术文档\u003C\u002Fp>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>一、概述\u003C\u002Fstrong>\u003C\u002Fh1>\u003Cp style=\"text-align:center\">\u003Cimg src=\"https:\u002F\u002Fqu-link.oss-cn-hangzhou.aliyuncs.com\u002F2024\u002F11\u002F25\u002F9b441c1f-cabf-4320-91ee-7651c2b8b599.png\" alt=\"\" loading=\"lazy\" \u002F>\u003C\u002Fp>\u003Cp style=\"text-indent:2em;text-align:left\">在畅联云平台的部署中，Java 后端程序需要在不同的环境（如开发、生产）下运行，为了方便区分和管理环境，我们使用 Spring Profiles 的机制来设置程序的运行环境。通过激活不同的 profile，系统可以加载对应环境的配置文件，使环境隔离，并实现配置管理的自动化。\u003C\u002Fp>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>二、环境激活需求\u003C\u002Fstrong>\u003C\u002Fh1>\u003Cp style=\"text-indent:2em;text-align:justify\">后端程序在运行时需要指定 active 环境，通常为 dev（开发环境）或 prod（生产环境）。传统方式是直接在 application.yml 文件中指定 active 参数，但这种方式要求每次部署到不同环境前都要修改 application.yml 文件中的 active 参数，容易导致人为错误并增加部署复杂性。\u003C\u002Fp>\u003Cp style=\"text-align:justify\">\u003Cspan style=\"font-size:19px\">\u003Cstrong>优化方案：\u003C\u002Fstrong>\u003C\u002Fspan>在服务文件中设置环境变量\u003C\u002Fp>\u003Cp style=\"text-indent:2em;text-align:justify\">为了避免每次打包和部署时都手动修改 application.yml 文件中的 active 参数，我们可以在服务器上直接配置环境变量，使得在不同环境中无须重新打包，即可切换所需的运行环境。\u003C\u002Fp>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>三、配置步骤\u003C\u002Fstrong>\u003C\u002Fh1>\u003Ch2 style=\"text-align:justify\">\u003Cstrong>1、 移除 application.yml 中的 active 设置：\u003C\u002Fstrong>\u003C\u002Fh2>\u003Cp style=\"text-indent:2em;text-align:justify\">在 application.yml 文件中不再手动指定 active 参数，而是通过环境变量配置来控制。\u003C\u002Fp>\u003Cpre>\u003Ccode>application.yml\nspring:\nprofiles:\n \nactive: dev # 移除此行\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2 style=\"text-align:justify\">\u003Cstrong>2、 在部署环境中配置环境变量：\u003C\u002Fstrong>\u003C\u002Fh2>\u003Cp style=\"text-indent:2em;text-align:justify\">编辑服务的 service 文件，在其中增加 SPRING_PROFILES_ACTIVE 的环境变量配置，以指定当前环境的 profile。\u003C\u002Fp>\u003Cp style=\"text-indent:2em;text-align:justify\">假设服务文件路径为 \u002Fetc\u002Fsystemd\u002Fsystem\u002Fvideo-monitoring.service，添加如下配置：\u003C\u002Fp>\u003Cpre>\u003Ccode>\u002Fetc\u002Fsystemd\u002Fsystem\u002Fvideo-monitoring.service\n[Unit]\nDescription=Video Monitoring Platform Java Backend\n \n[Service]\nType=simple\nUser=video_user\nExecStart=\u002Fusr\u002Fbin\u002Fjava -jar \u002Fpath\u002Fto\u002Fvideo-monitoring.jar\nEnvironment=“SPRING_PROFILES_ACTIVE=prod”\nWorkingDirectory=\u002Fpath\u002Fto\u002Fapplication\nRestart=on-failure\n \n[Install]\nWantedBy=multi-user.target\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp style=\"text-indent:2em;text-align:justify\">在上面的 service 文件中，通过 Environment=“SPRING_PROFILES_ACTIVE=prod”，我们将 Spring 的 profile 激活为 prod（生产环境）。当系统启动此服务时，程序会自动识别并加载 application-prod.yml 配置文件。\u003C\u002Fp>\u003Ch2 style=\"text-align:justify\">\u003Cstrong>3. 重新加载服务并启动：\u003C\u002Fstrong>\u003C\u002Fh2>\u003Cp style=\"text-align:justify\">配置完 service 文件后，需要重新加载并启动服务以生效。\u003C\u002Fp>\u003Cp style=\"text-align:justify\">重新加载 Systemd 配置\u003C\u002Fp>\u003Cpre style=\"text-align:justify\">\u003Ccode style=\"text-align:left\">sudo systemctl daemon-reload\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp style=\"text-align:justify\">\u003Cbr \u002F>\u003C\u002Fp>\u003Cp style=\"text-align:justify\">启动畅联云平台服务\u003C\u002Fp>\u003Cpre style=\"text-align:justify\">\u003Ccode style=\"text-align:left\">sudo systemctl start video-monitoring.service\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp style=\"text-align:justify\">\u003Cbr \u002F>\u003C\u002Fp>\u003Cp style=\"text-align:justify\">设置开机自启动\u003C\u002Fp>\u003Cpre style=\"text-align:justify\">\u003Ccode style=\"text-align:left\">sudo systemctl enable video-monitoring.service\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp style=\"text-align:justify\">\u003Cbr \u002F>\u003C\u002Fp>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>四、配置生效验证\u003C\u002Fstrong>\u003C\u002Fh1>\u003Ch2 style=\"text-align:justify\">\u003Cstrong>1、 检查服务状态，确保服务正常启动：\u003C\u002Fstrong>\u003C\u002Fh2>\u003Cpre style=\"text-align:justify\">\u003Ccode style=\"text-align:left\">sudo systemctl status video-monitoring.service\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp style=\"text-align:justify\">\u003Cbr \u002F>\u003C\u002Fp>\u003Ch2 style=\"text-align:justify\">\u003Cstrong>2. 在应用日志中确认加载的配置文件是否为生产配置（application-prod.yml），如果正常，日志中将显示当前激活的 profile。\u003C\u002Fstrong>\u003C\u002Fh2>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>五、优势\u003C\u002Fstrong>\u003C\u002Fh1>\u003Cp style=\"text-align:justify\">\u003Cspan style=\"font-size:19px\">\u003Cstrong>1、 提高部署效率：\u003C\u002Fstrong>\u003C\u002Fspan>无需为每个环境单独打包，可实现“一次打包，到处部署”。\u003C\u002Fp>\u003Cp style=\"text-align:justify\">\u003Cspan style=\"font-size:19px\">\u003Cstrong>2、 避免人为错误：\u003C\u002Fstrong>\u003C\u002Fspan>减少修改配置文件的步骤，避免因打包时遗漏配置导致的问题。\u003C\u002Fp>\u003Cp style=\"text-align:justify\">\u003Cspan style=\"font-size:19px\">\u003Cstrong>3、 环境隔离：\u003C\u002Fstrong>\u003C\u002Fspan>通过 Systemd 服务文件中的环境变量来设置 profile，确保不同环境下的配置独立。\u003C\u002Fp>\u003Ch1 style=\"text-align:justify\">\u003Cstrong>六、结论\u003C\u002Fstrong>\u003C\u002Fh1>\u003Cp style=\"text-indent:2em;text-align:justify\">通过在服务文件中添加 SPRING_PROFILES_ACTIVE 环境变量，我们可以方便地管理 Java 后端程序在不同环境中的配置。这种方式简化了打包和部署流程，同时也提升了系统配置的安全性和可靠性。\u003C\u002Fp>\u003Cp style=\"text-align:center\">\u003Cimg src=\"https:\u002F\u002Fqu-link.oss-cn-hangzhou.aliyuncs.com\u002F2024\u002F11\u002F25\u002Fd88092a4-9eaf-40e7-8196-9dc4772c5709.png\" alt=\"\" loading=\"lazy\" \u002F>\u003C\u002Fp>\u003Cp style=\"text-indent:2em;text-align:left\">以上是畅联云平台 Java 后端环境配置的技术方案说明。如有任何疑问，欢迎联系技术支持团队。\u003C\u002Fp>\u003Cp style=\"text-align:left;line-height:2\">————————————————\u003C\u002Fp>\u003Cp style=\"text-align:left;line-height:2\">\u003Cspan style=\"color:rgb(0, 0, 0);background-color:rgb(255, 255, 255);font-size:16px\">关注\u003C\u002Fspan>\u003Cspan style=\"color:rgb(66, 144, 247);background-color:rgb(255, 255, 255);font-size:16px\">\u003Cstrong>“美畅物联”\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cspan style=\"color:rgb(0, 0, 0);background-color:rgb(255, 255, 255);font-size:16px\">，了解更多视频汇聚及AIoT底座解决方案。\u003C\u002Fspan>\u003C\u002Fp>","https:\u002F\u002Fqu-link.oss-cn-hangzhou.aliyuncs.com\u002F2024\u002F11\u002F25\u002Fc4b1306b-cbaf-457f-9473-3b95bc7c6937.jpg","9","美畅，美畅物联，畅联，畅联云平台，视频监控云平台，云视频监控，视频云平台，视频开放平台，视频感知云，视频接入网关，AIoT综合接入网关，视频中台，物联网中台，无插件播放，H265播放，Ehome，ISUP， GB28181，GB35114，gat1400，JT808，JT1078","原创技术分享丨美畅物联周工","http:\u002F\u002Fwww.24hlink.cn\u002Fweb-index\u002F6KKMEU6.html","2024-11-25 15:43:13","792"]