小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot nginx 负载均衡环境搭建

行业知识

SpringBoot nginx 负载均衡环境搭建

2026-11-12 17:32:09 来源:互联网转载

1: 下载并解压 nginx.zip

修改配置文件 nginx.conf

添加

upstream distribute {

     server localhost:10010;

     server localhost:10086;

    }

修改原有的 location 为

 location / {

            proxy_pass    http://distribute;

        }

进入 cmd 使用命令 start nginx 启动服务

nginx -t 检查配置是否错误 

其他命令

nginx -s stop 关闭服务

nginx -s reload 在运行状态下修改了配置之后 使用该命令重新加载配置,不需要重启服务

2:  在idea 中创建一个 springBoot 项目 ,我使用的是社区版 没有 init 工具 ,可以先创建一个 空的maven 工程手动添加 对应的 代码和配置文件路径

点  create 之后会去下载对应的 插件

3: 修改maven 路径 修改settings.xml 配置本地仓库路径和阿里云仓库路径

    

        aliyunmaven

        *

        maven aliyun

        https://maven.aliyun.com/repository/public

   

重新reimport 一下

4: 编写代码 修改客户端为支持运行多实例以及集成日志

项目的子包要放在和启动类同一级别的目录下 这是springboot默认的包扫描规则,不在同一级别要手动扫描

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class LockDemoApplication {    public static void main(String[] args) {        SpringApplication.run(LockDemoApplication.class, args);    }}

application.properties

server.port=10010logging.config=classpath:logback.xml

logback.xml

    logback                            %d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n                            true                                    applog/%d{yyyy-MM-dd}/%d{yyyy-MM-dd}.log                                                        %d{yyyy-MM-dd HH:mm:ss} -%msg%n                                                                            

pom.xml

    4.0.0            org.springframework.boot        spring-boot-starter-parent        3.2.1                org.wl.study    lockDemo    1.0-SNAPSHOT            17        17        UTF-8                            org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-test            test                            org.springframework.boot            spring-boot-starter-logging                                                        org.springframework.boot                spring-boot-maven-plugin                        

搭建完成之后 修改 idea 的 配置 使其支持多个实例启用

点开之后 候选上  允许多实例运行并且添加启用端口

修改端口号和服务名称 在启用一个实例 然后 通过页面访问 ,检查控制台日志 ,发现 负载均衡生效了

 

上一篇:Oceanaudio使用指南,轻松下载,畅享使用教程!

下一篇:OpenDNS,智能网络安全之选