Skip to content

warehouse-dev-007-cluster-niginx - Cluster deployment with Nginx #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ brew services start redis

brew services stop redis

# reload nginx config
nginx -s reload

#---------------------------
# Intellij
#---------------------------
Expand Down
51 changes: 46 additions & 5 deletions springWarehouse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ARCHITECTURE :
- V1
<p align="center"><img src ="./doc/pic/SpringWareHouse.svg"></p>

- V2
- Cluster mode with Nginx

[Demo](http://43.206.107.101:7777/)

Expand Down Expand Up @@ -70,7 +72,41 @@ ARCHITECTURE :
mvn package

# run
java -jar target/springWarehouse-0.0.1-SNAPSHOT.jar
# https://blog.csdn.net/G971005287W/article/details/114879972
java -jar target/springWarehouse-0.0.1-SNAPSHOT.jar --server.port=7777
java -jar target/springWarehouse-0.0.1-SNAPSHOT.jar --server.port=7778

#---------------------------
# Run nginx
#---------------------------

# https://github.com/yennanliu/utility_shell/blob/master/nginx/install_nginx.sh
# http://localhost:8080/

# start
brew services start nginx

# stop
brew services stop nginx


#---------------------------
# Other cmd
#---------------------------

# reload nginx config
nginx -s reload

# kill progress uses port
lsof -i :<port>
kill <pid>

# pressure testing - jmeter
cd apache-jmeter-5.6.2
bash bin/jmeter

# curl
curl http://localhost:7777/productType/list?pageNoStr=1
```

</details>
Expand Down Expand Up @@ -148,10 +184,15 @@ nohup java -jar target/springWarehouse-0.0.1-SNAPSHOT.jar &

## API

| API | Type | Purpose | Example cmd | Comment|
| ----- | -------- | ---- | ----- | ---- |
| http://localhost:7777/ | Get | Home page || |
| http://localhost:7777/swagger-ui/index.html | Get | API doc page || |
| API | Type | Purpose | Example cmd | Comment|
|-----------------------------------------------------|---------------| ---- | ----- | ---- |
| http://localhost:7777/ | Get | Home page || |
| http://localhost:7777/swagger-ui/index.html | Get | API doc page || |
| http://localhost:7777/test/type | test endpoint | | |
| http://localhost:7777/test/prod | test endpoint | | |
| http://localhost:7777/test/prod/deduct/{product_id} | test endpoint | | |
| http://localhost:8080/ | Nginx endpoint | | |


## Important Concepts

Expand Down
93 changes: 93 additions & 0 deletions springWarehouse/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

# https://youtu.be/CDaWk2RIBL4?si=dfe4B7Ct4fsPSOGW&t=315
# https://github.com/yennanliu/utility_shell/blob/master/nginx/nginx_backup.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


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

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

# load balance, reverse proxy
# https://youtu.be/CDaWk2RIBL4?si=k5piBHOSF-7vNTh1&t=392
upstream warehouseService {
server localhost:7777;
server localhost:7778;
}

server {
listen 8080;
server_name localhost;

location / {
# root html;
# index index.html index.htm;
proxy_pass http://warehouseService;
}

}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
19 changes: 12 additions & 7 deletions springWarehouse/sql/ddl/product.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

-- create database warehouse_system;

-- DROP TABLE product;

CREATE TABLE IF NOT EXISTS product(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(100) not null,
type_id int not null,
type_id int DEFAULT 1,
price int not null,
merchant_id varchar(100),
product_status varchar(10),
amount int DEFAULT 0,
constraint FK_PRODUCT_TYPE FOREIGN KEY (type_id) references product_type(type_id)
amount int DEFAULT 0
-- constraint FK_PRODUCT_TYPE FOREIGN KEY (type_id) references product_type(type_id)
);

-- DML
INSERT INTO product(name, type_id, price, merchant_id, amount, product_status)
-- truncate product;

INSERT INTO product(name, price, merchant_id, amount, product_status)
VALUES
("i-phone", 2, 300, 1, 3, "on_board"),
("cookie2", 4, 10, 2, 2, "new"),
("soap", 1, 50, 1, 1, "off_board");
("i-phone", 300, 1, 5000, "on_board"),
("cookie2", 10, 2, 5000, "new"),
("soap", 50, 3, 5000, "off_board"),
("soap", 70, 4, 5000, "off_board");
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public String update(Order order) {
*/
@GetMapping(value="/download")
public void download(){

List<Order> orders = orderService.getAllOrders();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.yen.springWarehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yen.springWarehouse.bean.Product;
import com.yen.springWarehouse.bean.ProductType;
import com.yen.springWarehouse.service.ProductService;
import com.yen.springWarehouse.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/test")
@RestController
public class TestController {

@Autowired
ProductTypeService productTypeService;

@Autowired
ProductService productService;


/** productType */
@GetMapping("/type")
List<ProductType> getAllProductType(){

return productTypeService.list(new QueryWrapper<>());
}


/** product */
@GetMapping("/prod")
List<Product> getAllProduct(){

return productService.list(new QueryWrapper<>());
}

@GetMapping("/prod/deduct/{productId}")
String deductProduct(@PathVariable("productId") Integer productId){

productService.deduct(productId);
return "OK";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.yen.springWarehouse.bean.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

Expand All @@ -16,4 +17,6 @@ public interface ProductMapper extends BaseMapper<Product> {
// TODO : check @Param("ew") ??
List<Product> getProductList(Page<Product> productPage, @Param("ew") Wrapper<Product> wrapper);

@Update("update product set amount = amount - #{amount} where id = #{productId} and amount >= #{amount}")
int updateProductCount(@Param("productId") Integer productId, @Param("amount") Integer amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public interface ProductService extends IService<Product> {

List<ProductDto> getProductDtoListFromProductList(List<Product> productList);

void deduct(Integer id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yen.springWarehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
Expand Down Expand Up @@ -30,6 +31,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
@Autowired
ProductTypeMapper productTypeMapper;

@Autowired
ProductMapper productMapper;

@Autowired
MerchantService merchantService;

Expand Down Expand Up @@ -94,4 +98,25 @@ public List<ProductDto> getProductDtoListFromProductList(List<Product> productLi
return productDtoList;
}

@Override
public void deduct(Integer id) {

/** V1 */
System.out.println(">>> id = " + id);
Product product = productMapper.selectById(id);
System.out.println(">>> product = " + product);
int amount = product.getAmount();

// product.setAmount(amount-1);
// UpdateWrapper<Product> wrapper = new UpdateWrapper<>(product);
// // TODO : check why productMapper CAN'T update with UpdateWrapper
// //productMapper.update(product, wrapper); //baseMapper.update(product, wrapper);
// productMapper.updateById(product);

/** V2 : use Mysql lock deal with cluster deployment */

productMapper.updateProductCount(product.getId(), 1);
System.out.println(">>> product = " + product);
}

}
10 changes: 5 additions & 5 deletions springWarehouse/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ debug=true
server.tomcat.uri-encoding=UTF-8
spring-http-encoding.charset=UTF-8
# hot deployment
spring.devtools.restart.enabled=true
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
#spring.devtools.restart.enabled=true
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.cache=false
#spring.thymeleaf.suffix=.html

# mysql
spring.datasource.url=jdbc:mysql://localhost:3306/warehouse_system?createDatabaseIfNotExist=true
Expand Down