updated Readme

This commit is contained in:
Jincheng Lu 2025-09-17 04:11:24 +08:00
parent a5752a8b61
commit 3eefb66c58

View File

@ -1,32 +1,55 @@
## Java Implementation ## Java Implementation
#### 运行方式 ### 运行方式
用./mvnw spring-boot:run运行 用./mvnw spring-boot:run运行
h2 database在 localhost:8080/h2-console h2 database在 localhost:8080/h2-console
#### cURL测试 docker compose file for redis,rocketmq nad nacos in the root directory.
用cURL测试/api/v1/product/create #### Redis
curl -X POST -H "Content-Type: application/json" --data '{"name":"product1","price":"1.0","stock":"5"}' http://localhost:8080/api/v1/product/create Redis acts as a caching service, increases application's ability to high traffic on the most popular products.
返回 <br>
{"code":201,"name":"product1","message":"Product created"} 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.
再次用cURL测试/api/v1/product/create使用相同的name ### Test
返回 #### Redis
{"code":409,"message":"Product with the same name already exists"} I logged in to redis-cli and checked the keys and values after creating products.
<br>
用cURL测试/api/v1/product/{id} Then I restart the application so that the H2 database is cleared.
curl -X GET -H "Content-Type: application/json" http://localhost:8080/api/v1/product/product1 However, the redis database continues to have the cached products.
返回 After restart the H2 database shouldn't have any products. If my GET request
{"product":{"price":1.0,"name":"product1","stock":5},"code":200,"message":"Product retrieved"} got a 200 response then it means the response is from the redis cache (cache hit).
用cURL测试/api/v1/product/{id}使用不存在的name #### RocketMQ
返回 After the product is created, a listener will print to the console the name of the product.
{"code":404,"message":"Product not found"} #### 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.
- controller: 接受请求parameters然后调用service ### Thoughts
- service: 调用repository处理业务逻辑。在必要时throw exception The redis database setup is straightforward.
- repository: 继承JpaRepository和database交互存储数据 <br>
- product: Model class RocketMQ took a long time because I keep getting connection errors.
controller不直接调用repository,而是调用service负责业务逻辑。将来如要更改实现方式只需更改service和repository不用更改controller <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