1. Introduction
There are many libraries for NoSQL
Here we look at two common NoSQL types: Redis and MongoDB
The basic knowledge of Docker, Redis and MongoDB is required. If not, self-study of Redis and MongoDB is recommended
2. Integrate Redis
Start the redis
If you use Docker for the first time, the password is 123 and you can specify it yourself
If you had a Redis container before
Idea Creates a Springboot project
Application. Properties Adds three base configurations
Double-click Shift to open the search panel search and enter the RedisAutoConfiguration class
You can see that two template classes are provided: RedisTemplate and StringRedisTemplate
The RedisTemplate is used to process key-value data (which will also become a string if stored in Redis).
StringRedisTemplate is used to handle data where key-value is a string
Case 1:
Prepare the User class, noting that the Serializable interface is automatically serialized
public class User implements Serializable {
private String username;
private String address;
/ / omit the get, set, toString
}
Copy the code
Test runs separately in test cases to see the results
@SpringBootTest
class Redis01ApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
void contextLoads(a) {
User user = new User();
user.setUsername("southyin");
user.setAddress("www.southyin.top");
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("u", user);
User u = (User) valueOperations.get("u");
System.out.println("u = " + u);
}
@Test
void contextLoads1(a){
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set("southyin"."www.southyin.top");
String southyin = valueOperations.get("southyin");
System.out.println(southyin);
}
@Test
void contextLoads2(a) throws JsonProcessingException {
User user = new User();
user.setUsername("southyin");
user.setAddress("www.southyin.top");
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
ObjectMapper om = new ObjectMapper();
valueOperations.set("southyin",om.writeValueAsString(user));
String southyin = valueOperations.get("southyin"); User user1 = om.readValue(southyin, User.class); System.out.println(user1); }}Copy the code
Note:
The redisTemplate is serialized regardless of whether the key-value is an object or a string
StringRedisTemplate does not serialize key-value
3. The session to be Shared
Session sharing is often necessary in clustered and distributed scenarios.
Imagine a client sending a request to Nginx, which Nginx forwards to Tomcat1. Tomcat1 stores the write data in the session, and the client sends another request. Nginx uses load balancing to forward requests to Tomcat2, which does not have the previously saved data
The simple solution to this problem is to store the data that needs to be shared among various services in a “public place”. The mainstream solution is Redis
Case study:
Create a project
The configuration file
spring.redis.port=6379
spring.redis.password=123
spring.redis.host=192.168.189.101
Copy the code
HelloController
@RestController
public class HelloController {
@Value("${server.port}")
Integer port;
@GetMapping("/set")
public String set(HttpSession session) {
session.setAttribute("southyin"."www.southyin.top");
return String.valueOf(port);
}
@GetMapping("/get")
public String get(HttpSession session) {
String southyin = (String)session.getAttribute("southyin");
returnsouthyin + port; }}Copy the code
Project package
After you upload Linux, open two projects
Java -jar springboot-session-0.0.1 -snapshot.jar --server.port=8080 >log1.log 2> &1&java-jar Springboot-session-0.0.1 - snapshot.jar --server.port=8081 >log2.log 2> &1&Copy the code
Browser access test
The test found that we did not call set on port 8081 to store data, but could directly get the data of set on port 8080, indicating that the session had been shared
4.Nginx implements load balancing
The above example is not perfect, in practice we can not be specified to access 8080 or 8081, so use Nginx to implement
Start the nginx
Go to the container and jump to the directory
Copy the contents of the file (CTRL + Insert),exit to exit the edit
Create the vim default.conf file, enter the editing mode, and paste the content of the file
Save, exit and paste into the container
docker cp ./default.conf mynginx:/etc/nginx/conf.d
Copy the code
Then go to the container and jump to the directory
Check the file
After copying the file contents, create the file
Paste into the container and restart
Start the browser test to see the load balancing effect
5. Interface power feature
Interface idempotence means that the results of one or multiple requests for the same operation are consistent without side effects caused by multiple clicks. For an example of the simplest, and that is to pay, after users buy goods payment, your payment is successful, but to return the result of network anomaly, right now the money has been buckled, users click on the button again, this time will be the second deductions, successful, user queries the balance back to find more buckles money, running water record into two, this is no guarantee the idempotence of the interface
6. Integrating mongo
The installation
Prepare mongo. Yml
Version: '3.1' services: mongo: image: mongo ports: -27017:27017 restart: always environment: MONGO_INITDB_ROOT_USERNAME: madmin MONGO_INITDB_ROOT_PASSWORD: m123 mongo-express: links: - mongo image: mongo-express restart: always ports: - 8081:8081 environment: ME_CONFIG_OPTIONS_EDITORTHEME: 3024-night ME_CONFIG_BASICAUTH_USERNAME: mongoexpress ME_CONFIG_BASICAUTH_PASSWORD: mongoexpress ME_CONFIG_MONGODB_ADMINUSERNAME: madmin ME_CONFIG_MONGODB_ADMINPASSWORD: m123Copy the code
Start both containers with Docker-compose (if there is no Docker-compose, install the following code in sequence)
cd /usr/localhttps://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-Linux-x86_64 / bin wget rename docker-compose-Linux-x86_64 docker-compose docker-compose-Linux-x86_64 chmod +x /usr/local/bin/docker-compose
docker-compose version
Copy the code
Then perform
docker-compose -f mongo.yml up -d
Copy the code
Browser access IP :8081 login, login name and password are mongoExpress above, the default at the beginning of the following three libraries
integration
Create a project
Configuration file parameters
spring.data.mongodb.port=27017
spring.data.mongodb.host=192.168.189.101
spring.data.mongodb.username=madmin
spring.data.mongodb.password=m123
# select test library, if not, create test
spring.data.mongodb.database=test
The user password information is authenticated based on the admin library
spring.data.mongodb.authentication-database=admin
Copy the code
MongoTemplate is mainly used for test case testing
@SpringBootTest
class SpringbootMongodbApplicationTests {
@Autowired
MongoTemplate mongoTemplate;
@Test
void contextLoads(a) {
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId((long) i);
user.setUsername("southyin:" + i);
user.setAddress("www.southyin.top:"+ i); mongoTemplate.insert(user); }}@Test
void contextLoads1(a) {
List<User> users = mongoTemplate.findAll(User.class);
for(User user : users) { System.out.println(user); }}@Test
void contextLoads2(a) {
User user = new User();
user.setId(1L); mongoTemplate.remove(user); }}Copy the code
After test run, access IP :8081, namely MongoExpress view