Search code examples
spring-boothaproxy

Ha Proxy with 3 springboot applications


I have 3 spring boot applications, each running on a different port. Can someone guide me how to set up Ha Proxy to demonstarte load balancing between the 3 applications (can make multiple instances). Is there any feature in spring boot which integrates Ha Proxy? What are the thing that I have to change in the config file of Ha Proxy?


Solution

  • Actually, there are several ways one can achieve this. But, I don't think there is anything in spring boot to integrate with HAProxy, because they two are different processes and they two work independently and nothing linked to each other as you might know what spring boot does. And HAProxy is a load balancer, and also a proxy server for TCP and HTTP process that are distributed across multiple servers.

    That explains the first part of your question.

    Now actually, how can you achieve this is entirely based on how you want to set this up.

    1. Run individual applications as service like you did, and route traffic to each of them based on url.

    2. Another deploying the individual applications on a single tomcat and taking the help of context path in your application properties you can route traffic from outside world to tomcat while tomcat takes care of everything.

    And there might be other ways to do this, someone can add in the future to this answer. But either way you need to use a proxy server to do this, it could be either HAProxy, Nginx, or anything that fits the purpose.

    So, taking your approach let's assume you are running your applications on port 8081, 8082, 8083. Your HAProxy setting should look something like this.

    frontend www_http
        mode            http
        bind            *:80
        bind            *:443 ssl crt /etc/ssl/certs/mycompany.pem
    
        # passing on that browser is using https
        reqadd X-Forwarded-Proto:\ https
        # for Clickjacking
        rspadd X-Frame-Options:\ SAMEORIGIN
    
        # prevent browser from using non-secure
        rspadd Strict-Transport-Security:\ max-age=15768000
        redirect        scheme https code 301 if !{ ssl_fc }
    
        stats enable
        stats refresh 30s
        stats show-node
        stats realm Haproxy\ Statistics
        stats uri /haproxy?stats
    
        acl app1 hdr(host) -i app1.mycompany.com
        acl app2 hdr(host) -i app2.mycompany.com
        acl app3 hdr(host) -i app3.mycompany.com
    
        # Just incase if you are using path instead of subdomain. But it's commented.
        # acl app1 url_beg /app1
        # acl app2 url_beg /app2
        # acl app3 url_beg /app3
    
        use_backend app_1_backend if app1
        use_backend app_2_backend if app2
        use_backend app_3_backend if app3
    
    # backend for app 1
    backend app_1_backend
        timeout client  300000
        timeout server  300000
        redirect scheme https if !{ ssl_fc }
        server app-1 127.0.0.1:8081 check
        http-response set-header X-TS-Server-ID %s
    
    # backend for app 2
    backend app_2_backend
        timeout client  300000
        timeout server  300000
        redirect scheme https if !{ ssl_fc }
        server app-2 127.0.0.1:8082 check
        http-response set-header X-TS-Server-ID %s
    
    # backend for app 3
    backend app_3_backend
        timeout client  300000
        timeout server  300000
        redirect scheme https if !{ ssl_fc }
        server app-3 127.0.0.1:8083 check
        http-response set-header X-TS-Server-ID %s
    

    This is some basic setup, but you can add your options and change everything as you like.

    Hope this helps.