When using springcloud, springcloud configserver is generally used to obtain the configuration, and the configuration is generally saved in git. The form of the file is convenient for modification and reference. In fact, the configuration can also be saved in the database.
But recently, I found a problem when using H3C private cloud . That is, the cloud comes with a microservice engine that can automatically create and deploy Eureka, ConfigServer, and GateWay. However, when configuring ConfigServer, it is found that only the configuration is saved to the database, that is, the database address needs to be filled in. This is not the same as the previous development method.
So I tried to fill in a database address, click Create, and found that a database h3c_pring_cloud was automatically created
There are several tables in it:
spring_cloud_config
spring_cloud_gateway
spring_cloud_gateway_jwt
spring_cloud_route_subsidiary
spring_cloud_route_to_subsidiary
It can be seen that spring_cloud_config is the table to save the configuration. The table structure is as follows,
It is obvious that the useful fields are config_key/config_value/application.
So try to insert a piece of data, only insert name, application, config_key, config_value others remain empty.
name is set to test-app
application is set to test-app-dev
config_key and config_value are set casually
Access the configuration center directly using a browser.
curl http://localhost:8080/test-app-dev/default/
The reason why the address is followed by default is because if you don’t follow it, you will not be able to access it, that is, return an error page, so why set it to default, because if spring.profiles.active is not specified in springboot, the default is default
The results are as follows.
The desired data was not returned. So I wonder if it is a problem with the profile field, so I set the profule field to default. When setting the profile to default here, you need to pay attention
Executed again, still did not find any data.
This is strange, the data can be found directly using sql, but I don't know what SQL statement is used by the ConfigServer automatically created by the cloud, because the ConfigServer is automatically created, so its source code cannot be viewed. When I was at a loss, I suddenly wanted to let the cloud create a database again, so I deleted the database directly, and then I was thinking, what will happen if I visit it again at this time? So, I use the browser again to access ConfigServer. The following results were obtained.
From the returned result, I accidentally got the sql statement of the created ConfigServer, and found that the env_id was a fixed value. After searching, I found that the value was generated when the microservice engine was created. Then, I set the env_id field in the data to this value. Re-use the browser to initiate the request and return the result.
As a result, there is still no data, which is depressing. After looking at the remaining null fields, there is only one label, which is suspicious, but what value should be set?
Why do you struggle with this problem? It is very convenient to change the label by accessing on the browser, but it is mainly used in the spring project, usually the master. Therefore, set the label to master.
visit now
curl http://localhost:8080/test-app-dev/default/
got the data.
After a period of exploration, I probably know how its ConfigServer was written, so. Paste the following code.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080
context-path: /
spring:
profiles:
active: jdbc
application:
name: CONFIG_SERVER
datasource:
url: jdbc:mysql://10.116.67.115:3306/h3c_pring_cloud
password: root
username: root
driver-class-name: com.mysql.jdbc.Driver
cloud:
config:
server:
jdbc:
sql: SELECT config_key ,config_value from spring_cloud_config where env_id = 'asdfrsgt34fgfd' and APPLICATION=? and PROFILE=? and LABEL=?
enabled: true
Application.java
@EnableConfigServer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
database script
DROP TABLE IF EXISTS `spring_cloud_config`;
CREATE TABLE `spring_cloud_config` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`env_id` varchar(100) DEFAULT NULL,
`application` varchar(100) DEFAULT NULL,
`profile` varchar(100) DEFAULT NULL,
`label` varchar(100) DEFAULT NULL,
`config_key` varchar(100) DEFAULT NULL,
`config_value` varchar(100) DEFAULT NULL,
`config_type` varchar(100) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`states` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `spring_cloud_config` VALUES (1342953439026266112,'test-app','asdfrsgt34fgfd','test-app-dev','default','master','spring.datasource.url','jbdc:mysql://127.0.0.1:3306/test',NULL,NULL,NULL,NULL);
Then create the database. The configuration can be obtained through browser access, but when I delete the database, I access it again.
An error page appeared.
Then I saw that the console also reported an error.
But why did not appear the page of ConfigServer created by the cloud, because the configserver log cannot be seen in the microservice engine of H3C. I wonder if the ConfigServer created by the cloud uses the global exception handling of spring, so try to add A global exception handler.
GlobalExceptionHandler.java
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(Exception.class)
public BaseResult globalException(HttpServletResponse response, Exception ex) {
BaseResult baseResult = new BaseResult();
baseResult.setCode(0);
baseResult.setTotal(0);
baseResult.setMsg(ex.getMessage());
return baseResult;
}
}
BaseResult.java
public class BaseResult {
private int code;
private String msg;
private int total;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
Try again and find that the return result is consistent with the return result of the configserver created by the cloud
Finally, you can look at the mapping rules of configserver
At the same time, after this experiment, I found that there are still many inconveniences in using mysql to save configuration files