diff --git a/Readme.md b/Readme.md index 0c9b849..8e66278 100644 --- a/Readme.md +++ b/Readme.md @@ -1,32 +1,55 @@ ## Java Implementation -#### 运行方式 +### 运行方式 用./mvnw spring-boot:run运行 h2 database在 localhost:8080/h2-console -#### cURL测试 +docker compose file for redis,rocketmq nad nacos in the root directory. -用cURL测试/api/v1/product/create -curl -X POST -H "Content-Type: application/json" --data '{"name":"product1","price":"1.0","stock":"5"}' http://localhost:8080/api/v1/product/create -返回 -{"code":201,"name":"product1","message":"Product created"} +#### Redis +Redis acts as a caching service, increases application's ability to high traffic on the most popular products. +
+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. +
+Therefore reduces coupling between different services. +
+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. +
+One of the use case is to act as some sort of environment variables manager. +
+It can also update those variables in real time without restarting the application. -再次用cURL测试/api/v1/product/create使用相同的name -返回 -{"code":409,"message":"Product with the same name already exists"} - -用cURL测试/api/v1/product/{id} -curl -X GET -H "Content-Type: application/json" http://localhost:8080/api/v1/product/product1 -返回 -{"product":{"price":1.0,"name":"product1","stock":5},"code":200,"message":"Product retrieved"} -用cURL测试/api/v1/product/{id}使用不存在的name -返回 -{"code":404,"message":"Product not found"} - -#### 代码结构 -- controller: 接受请求parameters然后调用service -- service: 调用repository,处理业务逻辑。在必要时throw exception -- repository: 继承JpaRepository,和database交互存储数据 -- product: Model class -controller不直接调用repository,而是调用service负责业务逻辑。将来如要更改实现方式,只需更改service和repository,不用更改controller +### Test +#### Redis +I logged in to redis-cli and checked the keys and values after creating products. +
+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. +
+RocketMQ took a long time because I keep getting connection errors. +
+I have RocketMQ set up in docker on my linux machine with ip 10.10.2.71. My application +is on my Windows machine. +
+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. +
+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 \ No newline at end of file