55 lines
2.5 KiB
Markdown
55 lines
2.5 KiB
Markdown
## Java Implementation
|
|
|
|
### 运行方式
|
|
|
|
用./mvnw spring-boot:run运行
|
|
h2 database在 localhost:8080/h2-console
|
|
|
|
docker compose file for redis,rocketmq nad nacos in the root directory.
|
|
|
|
#### Redis
|
|
Redis acts as a caching service, increases application's ability to high traffic on the most popular products.
|
|
<br>
|
|
It also reduces the number of database operations which can saves costs if using a pay per use database service.
|
|
#### RocketMQ
|
|
RocketMQ is a open source messaging service that separates message producers from message consumers.
|
|
<br>
|
|
Therefore reduces coupling between different services.
|
|
<br>
|
|
It is similar to observer pattern but operates across different applications.
|
|
#### Nacos
|
|
Nacos is a registry that stores application properties and other variables that subject to change.
|
|
<br>
|
|
One of the use case is to act as some sort of environment variables manager.
|
|
<br>
|
|
It can also update those variables in real time without restarting the application.
|
|
|
|
### Test
|
|
#### Redis
|
|
I logged in to redis-cli and checked the keys and values after creating products.
|
|
<br>
|
|
Then I restart the application so that the H2 database is cleared.
|
|
However, the redis database continues to have the cached products.
|
|
After restart the H2 database shouldn't have any products. If my GET request
|
|
got a 200 response then it means the response is from the redis cache (cache hit).
|
|
#### RocketMQ
|
|
After the product is created, a listener will print to the console the name of the product.
|
|
#### Nacos
|
|
When I change the value of product.service.welcome-message in the nacos web console and refresh
|
|
the page, the welcome message is updated without restarting the application.
|
|
### Thoughts
|
|
The redis database setup is straightforward.
|
|
<br>
|
|
RocketMQ took a long time because I keep getting connection errors.
|
|
<br>
|
|
I have RocketMQ set up in docker on my linux machine with ip 10.10.2.71. My application
|
|
is on my Windows machine.
|
|
<br>
|
|
It turns out, although I set up the address of the nameserver correctly as 10.10.2.71:9876,
|
|
the applications gets the address of the broker from the nameserver. Which is
|
|
an internal ip used by docker container. There is no way for me to manually set the broker address.
|
|
So I have to move my application to the same linux machine. It works fine afterward.
|
|
<br>
|
|
I ran in to compatibility issues when I tried to set up nacos. I initially
|
|
used the version of "spring-cloud-starter-alibaba-nacos-config" in nacos documentation,
|
|
but it wasn't compatible with Spring Boot 3.5.4. I had to use the preview version of nacos |