NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Deploy Spring Boot on Hong Kong VPS: Java Microservices for Asia (2026)

· 4 min read

Spring Boot remains the dominant Java framework for enterprise microservices — powering the backends of major banks, telecoms, and SaaS platforms across Asia-Pacific. Deploying Spring Boot on a Hong Kong VPS gives Java applications direct access to mainland Chinese users via CN2 GIA routing, keeps the JVM heap on dedicated RAM without noisy-neighbour contention, and enables the full Spring ecosystem (Security, Data JPA, Cloud, Actuator) without cloud vendor lock-in.


Step 1: Install Java 21 (LTS)

<code">apt update && apt install -y wget apt-transport-https

# Install Eclipse Temurin 21 (recommended OpenJDK distribution)
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public | \
  gpg --dearmor -o /usr/share/keyrings/adoptium.gpg
echo "deb [signed-by=/usr/share/keyrings/adoptium.gpg] \
  https://packages.adoptium.net/artifactory/deb $(lsb_release -cs) main" \
  > /etc/apt/sources.list.d/adoptium.list
apt update && apt install -y temurin-21-jdk

java -version   # openjdk 21.x.x

Step 2: Spring Boot Application Configuration

<code"># src/main/resources/application.yml
spring:
  application:
    name: hk-api

  datasource:
    url: ${DATABASE_URL}
    username: ${DB_USER}
    password: ${DB_PASSWORD}
    hikari:
      maximum-pool-size: 10
      minimum-idle: 2
      connection-timeout: 30000
      idle-timeout: 600000

  jpa:
    hibernate:
      ddl-auto: validate    # Use Flyway for migrations — never auto-create in prod
    show-sql: false
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        jdbc:
          batch_size: 50

  data:
    redis:
      host: ${REDIS_HOST:localhost}
      port: ${REDIS_PORT:6379}
      password: ${REDIS_PASSWORD:}

  cache:
    type: redis

server:
  port: ${PORT:8080}
  address: 127.0.0.1
  compression:
    enabled: true
    mime-types: application/json,text/html,text/plain
    min-response-size: 1024

management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus
  endpoint:
    health:
      show-details: when-authorized

logging:
  level:
    com.yourcompany: INFO
    org.springframework: WARN
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'

Step 3: REST Controller Example

<code"># ProductController.java
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
@Slf4j
public class ProductController {

    private final ProductService productService;

    @GetMapping
    public ResponseEntity<Page> listProducts(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String category) {

        Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
        Page products = productService.findAll(category, pageable);
        return ResponseEntity.ok(products);
    }

    @GetMapping("/{id}")
    public ResponseEntity getProduct(@PathVariable UUID id) {
        return productService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ProductDto createProduct(@Valid @RequestBody CreateProductRequest request) {
        return productService.create(request);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    public Map<String, Object> handleValidation(MethodArgumentNotValidException ex) {
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getFieldErrors()
                .forEach(e -> errors.put(e.getField(), e.getDefaultMessage()));
        return Map.of("error", "Validation failed", "details", errors);
    }
}

Step 4: Build Fat JAR and Deploy

<code"># Build (Maven)
./mvnw clean package -DskipTests -Pproduction

# Or Gradle
./gradlew bootJar

# The fat JAR contains everything:
ls -lh target/*.jar
# hk-api-1.0.0.jar  ~50–80 MB

# Transfer to VPS
scp target/hk-api-1.0.0.jar root@VPS_IP:/opt/hk-api/app.jar

# On VPS: create application user
useradd -m -s /bin/bash spring-app
mkdir -p /opt/hk-api/logs
chown -R spring-app:spring-app /opt/hk-api

# Environment file
cat > /opt/hk-api/.env << 'EOF'
DATABASE_URL=jdbc:postgresql://localhost:5432/myapp_db
DB_USER=myapp_user
DB_PASSWORD=StrongDBPass!
REDIS_HOST=localhost
REDIS_PASSWORD=RedisPass!
SPRING_PROFILES_ACTIVE=production
JAVA_OPTS=-Xms256m -Xmx512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200
EOF

Step 5: Systemd Service

<code">cat > /etc/systemd/system/spring-app.service << 'EOF'
[Unit]
Description=Spring Boot Application
After=network.target postgresql.service

[Service]
Type=simple
User=spring-app
WorkingDirectory=/opt/hk-api
EnvironmentFile=/opt/hk-api/.env
ExecStart=/usr/bin/java \
    $JAVA_OPTS \
    -Dspring.profiles.active=production \
    -Dlogging.file.name=/opt/hk-api/logs/app.log \
    -jar /opt/hk-api/app.jar
SuccessExitStatus=143
Restart=always
RestartSec=10

# Security hardening
NoNewPrivileges=yes
PrivateTmp=yes

[Install]
WantedBy=multi-user.target
EOF

systemctl enable --now spring-app
journalctl -u spring-app -f

Step 6: Nginx Reverse Proxy

<code">cat > /etc/nginx/sites-available/spring-app << 'EOF'
upstream spring { server 127.0.0.1:8080; keepalive 32; }

server {
    listen 443 ssl http2;
    server_name api.yourdomain.com;
    ssl_certificate /etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/privkey.pem;

    location /actuator/ {
        allow 127.0.0.1;
        deny all;
        proxy_pass http://spring;
    }

    location / {
        proxy_pass http://spring;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 30s;
    }
}
EOF
ln -s /etc/nginx/sites-available/spring-app /etc/nginx/sites-enabled/
certbot --nginx -d api.yourdomain.com
nginx -t && systemctl reload nginx

Step 7: JVM Tuning for VPS

<code"># JVM flags for different VPS RAM sizes:

# 1 GB VPS:
JAVA_OPTS="-Xms128m -Xmx384m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"

# 2 GB VPS (Spring Boot + DB on same server):
JAVA_OPTS="-Xms256m -Xmx768m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"

# 4 GB VPS (dedicated API server):
JAVA_OPTS="-Xms512m -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=100 \
           -XX:G1HeapRegionSize=16m"

# Virtual threads (Java 21 — much better concurrency for IO-bound apps):
JAVA_OPTS="$JAVA_OPTS -Dspring.threads.virtual.enabled=true"

# GraalVM native image (experimental — instant startup, low memory):
# ./mvnw native:compile -Pnative
# Produces: target/hk-api (native binary, starts in <100ms, uses ~50 MB RAM)

Zero-Downtime Deployment

<code"># deploy.sh — blue/green on a single VPS
#!/bin/bash
NEW_JAR=$1

# Copy new jar
cp $NEW_JAR /opt/hk-api/app-new.jar

# Start new process on alternate port
java $JAVA_OPTS -Dserver.port=8081 -jar /opt/hk-api/app-new.jar &
NEW_PID=$!

# Wait for health check to pass
for i in {1..30}; do
  if curl -sf http://127.0.0.1:8081/actuator/health | grep -q '"UP"'; then
    break
  fi
  sleep 2
done

# Switch Nginx to new port
sed -i 's/:8080/:8081/' /etc/nginx/sites-available/spring-app
nginx -s reload

# Stop old process
systemctl stop spring-app

# Move new jar into place, restart service normally
mv /opt/hk-api/app-new.jar /opt/hk-api/app.jar
kill $NEW_PID
sed -i 's/:8081/:8080/' /etc/nginx/sites-available/spring-app
systemctl start spring-app
nginx -s reload

echo "Deployment complete"

Conclusion

Spring Boot on a Hong Kong VPS delivers enterprise Java applications to Asia-Pacific users at CN2 GIA latency — 30–60ms TTFB from mainland China. The self-contained fat JAR deploys without an application server, systemd manages the JVM process lifecycle, and Java 21 virtual threads provide Go-like concurrency for I/O-bound microservices without thread-per-request overhead. For Java teams serving Greater China and Southeast Asia, HK VPS infrastructure removes the latency penalty of US-based cloud Java hosting.

Run Java in Asia: Browse Server.HK Hong Kong VPS plans — a 2–4 GB plan handles Spring Boot with PostgreSQL and Redis for most production microservices workloads.

Related articles

More in VPS & ServersServer setup guides