Home  >  Article  >  Java  >  How Nginx deploys SpringBoot project

How Nginx deploys SpringBoot project

WBOY
WBOYforward
2023-05-11 15:58:111672browse

1. Create a new yml file application.yml

# 端口号
server:
  port: 2001

2. Write a Controller test class

package com.example.demo1.controller;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component
@RequestMapping("/v1")
public class HelloController {
    final static Logger log = LogManager.getLogger(HelloController.class);
    @Value("${server.port}")
    private int port ;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String test() {
        return "invoke url /,port="+port;
    }

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        return "invoke url /test1,port="+port;
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2() {
        return "invoke url /test2,port="+port;
    }
}

3. Write a startup class

package com.example.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}

4. I use pom file



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.6
         
    
    com.example
    demo1
    0.0.1-SNAPSHOT
    demo1
    Demo project for Spring Boot
    
        1.8
        2.19.0
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
            
            
            
            
            

            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            
        
        
        
            org.apache.logging.log4j
            log4j-api
            ${log4j.version}
        
        
            org.apache.logging.log4j
            log4j-core
            ${log4j.version}
        
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    1.8
                    1.8
                    UTF-8
                
            
            

                org.apache.maven.plugins

                maven-assembly-plugin

                2.5.5

                
                    
                        
                            com.example.demo1.Demo1Application
                        
                    
                    
                        jar-with-dependencies
                    
                
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
            
        
    

5. First test locally and start the project. Seeing this means the startup is successful

. ____ _ _ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\ / ___)| |_)| | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/

6、 To test, enter

http://127.0.0.1:3001/v1
http://127.0.0.1:3001/v1/test1
http:/ /127.0.0.1:3001/v1/test2

If you can see the printed information of the port number in the browser, it means success

7. Maven compiles it into a jar package

8. Modify the nginx.conf file

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       89;
        server_name  nginx_server;

        location / {
            proxy_pass http://server_ip:3001/v1;
        }
        location /edu {
            proxy_pass http://server_ip:3001/v1/test1;
        }
        location /ymd {
            proxy_pass http://server_ip:3002/v1/test2;
        }
    }

}

nginx_server: The address of the server where nginx is located

server_ip: The address of the reverse proxy server

Here I am They are all 10.161.20.10

7. Test and jump to different services according to the access path

Enter in the browser:

http://10.161.20.10 :90/

invoke url /,port=3001

http://10.161.20.10:90/test1

invoke url /test1,port=3001

http://10.161.20.10:90/test2

invoke url /test2,port=3002

The above is the detailed content of How Nginx deploys SpringBoot project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete