Sunday, June 22, 2025
HomeJavaSticky periods with Apache APISIX

Sticky periods with Apache APISIX


Beside the counter, I wish to show two further items of knowledge: the hostname and the logged-in person.

For the hostname, I add the next technique to the controller:

@ModelAttribute("hostname")
personal String hostname() throws UnknownHostException {
    return InetAddress.getLocalHost().getHostName();
}

Displaying the logged-in person requires a further dependency:

pom.xml

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>

On the web page, it’s simple:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">  (1)
<physique>
<td sec:authentication="principal.label">Me</td>                    (2)

1 Add the sec namespace. It’s not obligatory however could assist the IDE that will help you
2 Require the underlying UserDetail implementation to have a getLabel() technique

Final however not least, we have to configure Apache APISIX with sticky periods, as we noticed final week:

apisix.yml

routes:
  - uri: /*
    upstream:
      nodes:
        "webapp1:8080": 1
        "webapp2:8080": 1
      kind: chash
      hash_on: cookie
      key: cookie_JSESSIONID
#END

Right here’s the design carried out on Docker Compose:

docker-compose.yml

providers:
  apisix:
    picture: apache/apisix:3.3.0-debian
    volumes:
      - ./apisix/config.yml:/usr/native/apisix/conf/config.yaml:ro
      - ./apisix/apisix.yml:/usr/native/apisix/conf/apisix.yaml:ro    (1)
    ports:
      - "9080:9080"                                                  (2)
    depends_on:
      - webapp1
      - webapp2
  webapp1:
    construct: ./webapp
    hostname: webapp1                                                (3)
  webapp2:
    construct: ./webapp
    hostname: webapp2                                                (3)

1 Use the earlier configuration file
2 Solely expose the API Gateway to the skin world
3 Set the hostname to show it on the web page

We will log in utilizing one of many two hard-coded accounts. I’m utilizing john, with password john and label John Doe. Discover that Apache APISIX units me on a node and retains utilizing the identical if I refresh.

Screenshot from our app

We will attempt to log in with the opposite account (jane/jane) from a non-public window and verify the counter begins from 0.

Now comes the enjoyable half. Let’s cease the node which ought to be internet hosting the session knowledge, right here webapp2 and refresh the web page:

docker compose cease webapp2

We will see thrilling issues within the logs. Apache APISIX can now not discover the webapp2, so it forwards the request to the opposite upstream that it is aware of, webapp1.

  1. The request continues to be authenticated; it goes by Spring Safety
  2. The framework will get the principal out of the request
  3. It queries Spring Session
  4. And will get the proper counter worth that Hazelcast replicated from the opposite node

The one side-effect is an elevated latency due to Apache APISIX timeout. It’s 5 seconds by default, however you may configure it to a decrease worth if wanted.

After we begin webapp2 once more, the whole lot works as anticipated once more.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments