Nginx部署SpringBoot項目的實現(xiàn)
筆記記錄一下用Nginx部署SpringBoot項目
1、新建一個yml文件 application.yml
# 端口號 server: port: 2001
2、編寫一個Controler測試類
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、編寫一個啟動類
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、我用到的pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> ? ? <modelVersion>4.0.0</modelVersion> ? ? <parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <version>2.7.6</version> ? ? ? ? <relativePath/> <!-- lookup parent from repository --> ? ? </parent> ? ? <groupId>com.example</groupId> ? ? <artifactId>demo1</artifactId> ? ? <version>0.0.1-SNAPSHOT</version> ? ? <name>demo1</name> ? ? <description>Demo project for Spring Boot</description> ? ? <properties> ? ? ? ? <java.version>1.8</java.version> ? ? ? ? <log4j.version>2.19.0</log4j.version> ? ? </properties> ? ? <dependencies> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? ? ? <!--?? ??? ??? ?<exclusions>--> ? ? ? ? ? ? <!--?? ??? ??? ??? ?<exclusion>--> ? ? ? ? ? ? <!--?? ??? ??? ??? ??? ?<groupId>ch.qos.logback</groupId>--> ? ? ? ? ? ? <!--?? ??? ??? ??? ??? ?<artifactId>logback-classic</artifactId>--> ? ? ? ? ? ? <!--?? ??? ??? ??? ?</exclusion>--> ? ? ? ? ? ? <!--?? ??? ??? ?</exclusions>--> ? ? ? ? ? ? <exclusions> ? ? ? ? ? ? ? ? <exclusion> ? ? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-logging</artifactId> ? ? ? ? ? ? ? ? </exclusion> ? ? ? ? ? ? </exclusions> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId> ? ? ? ? ? ? <!-- <scope>test</scope> --> ? ? ? ? </dependency> ? ? ? ? <!--日志框架--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.logging.log4j</groupId> ? ? ? ? ? ? <artifactId>log4j-api</artifactId> ? ? ? ? ? ? <version>${log4j.version}</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.logging.log4j</groupId> ? ? ? ? ? ? <artifactId>log4j-core</artifactId> ? ? ? ? ? ? <version>${log4j.version}</version> ? ? ? ? </dependency> ? ? ? ? <!--日志框架--> ? ? </dependencies> ? ? <build> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId> ? ? ? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId> ? ? ? ? ? ? ? ? <version>3.7.0</version> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <source>1.8</source> ? ? ? ? ? ? ? ? ? ? <target>1.8</target> ? ? ? ? ? ? ? ? ? ? <encoding>UTF-8</encoding> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? </plugin> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId> ? ? ? ? ? ? ? ? <artifactId>maven-assembly-plugin</artifactId> ? ? ? ? ? ? ? ? <version>2.5.5</version> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <archive> ? ? ? ? ? ? ? ? ? ? ? ? <manifest> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <mainClass>com.example.demo1.Demo1Application</mainClass> ? ? ? ? ? ? ? ? ? ? ? ? </manifest> ? ? ? ? ? ? ? ? ? ? </archive> ? ? ? ? ? ? ? ? ? ? <descriptorRefs> ? ? ? ? ? ? ? ? ? ? ? ? <descriptorRef>jar-with-dependencies</descriptorRef> ? ? ? ? ? ? ? ? ? ? </descriptorRefs> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? ? ? <executions> ? ? ? ? ? ? ? ? ? ? <execution> ? ? ? ? ? ? ? ? ? ? ? ? <id>make-assembly</id> ? ? ? ? ? ? ? ? ? ? ? ? <phase>package</phase> ? ? ? ? ? ? ? ? ? ? ? ? <goals> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>single</goal> ? ? ? ? ? ? ? ? ? ? ? ? </goals> ? ? ? ? ? ? ? ? ? ? </execution> ? ? ? ? ? ? ? ? </executions> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build> </project>
5、先在本地測試,啟動項目,看到這個就說明啟動成功了
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
6、測試,在瀏覽器中依次輸入
http://127.0.0.1:3001/v1
http://127.0.0.1:3001/v1/test1
http://127.0.0.1:3001/v1/test2
在瀏覽器中能看到端口號的打印信息就說明成功了
7、maven編譯打成jar包
8、修改nginx.conf文件
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:nginx所在的服務(wù)器的地址
server_ip:反向代理的服務(wù)器的地址
這里我都是10.161.20.10
7、測試,根據(jù)訪問的路徑跳轉(zhuǎn)到不同的服務(wù)中
瀏覽器中輸入:
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
到此這篇關(guān)于Nginx部署SpringBoot項目的實現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx部署SpringBoot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大阿家以后多多支持腳本之家!
相關(guān)文章
修改Nginx源碼實現(xiàn)worker進(jìn)程隔離實現(xiàn)詳解
這篇文章主要為大家介紹了修改Nginx源碼實現(xiàn)worker進(jìn)程隔離實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Nginx配置編寫時支持邏輯運算與大小寫字母轉(zhuǎn)換的方法
這篇文章主要介紹了Nginx配置編寫時支持邏輯運算與大小寫字母轉(zhuǎn)換的方法,其中大小寫字母轉(zhuǎn)換是以lower upper case模塊來實現(xiàn),需要的朋友可以參考下2016-01-01nginx基于域名,端口,不同IP的虛擬主機(jī)設(shè)置的實現(xiàn)
這篇文章主要介紹了nginx基于域名,端口,不同IP的虛擬主機(jī)設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11網(wǎng)站如何通過nginx設(shè)置黑/白名單IP限制及國家城市IP訪問限制
如果你的服務(wù)器被攻擊很厲害,而且服務(wù)器是自己練手的,不需要其他用戶訪問的,那么就可以配置一下nginx的白名單,下面這篇文章主要給大家介紹了關(guān)于網(wǎng)站如何通過nginx設(shè)置黑/白名單IP限制及國家城市IP訪問限制的相關(guān)資料,需要的朋友可以參考下2022-07-07