Hello, everyone. Recently, the company's new project is developed with spincloud family bucket. Originally, spring cloud was only at the primary level of understanding. This time, with the opportunity of the new project, we can deeply practice spring cloud, even Happy. When I graduated from University, I had the idea of writing technical articles when I entered the job. I wasted so many years. All the problems I encountered during the period were easily saved in the narrow space of Youdao cloud notes. I have to see all the notes. Later, I have the opportunity to move out, learn from each other, improve my skills and cultivate my internal skills. Let's start the spring cloud journey now!
Project version
spring-boot-version: 2.2.5.RELEASE
spring-cloud.version: Hoxton.SR3
ConfigServer server
Build the spring cloud config server first
Open https://start.spring.io/
Check Config Server, download and import IDE
Some pom information
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
application.properties
# Application service name spring.application.name=config-server # Application service port number server.port=9001 # Git remote library retrieve relative address spring.cloud.config.server.git.search-paths=/springCloudAProject/** # Git remote library http access project address spring.cloud.config.server.git.uri=https://github.com/niuniu9631/SpringCloudConfig # Git remote library http access authentication user name spring.cloud.config.server.git.username=Own Git Account number # Git remote library http access authentication password spring.cloud.config.server.git.password=Own Git Account password
ConfigServerApplication.java
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
So far, the relevant code configuration of the server has been OK. Let's Run it together.
Access the service through the browser, http://localhost:9001/
Because no access interface is open to the public, 404 is right. As long as the project is not started incorrectly, it means that there is no problem in configuration. You can build a client to access and pull down data. Here I provide a tip I found here. After the startup is correct, you can go to the temporary file of the system to check whether there is a new directory starting with config repo XXXX. Some words indicate that there is no problem in Git configuration of the server.
Windows default temporary directory: C:\Users\wyb\AppData\Local\Temp
Linux default temporary directory: / tmp
The configuration item can be adjusted according to the configuration item
spring.cloud.config.server.git.basedir = specify directory
I have not configured the specified directory. The temporary directory file is as follows
ConfigClient client
As usual, open https://start.spring.io/,
Check Config Client, Spring Web, download and import IDE.
Some pom information
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
application.properties
# Client service application name spring.application.name=config-client # Client port number server.port=9002
bootstrap.properties
# Application service name spring.cloud.config.name=config-client # Corresponding branch name on git spring.cloud.config.label=master # Corresponding to the profiles parameter on the configuration file spring.cloud.config.profile=dev # Configure server request address spring.cloud.config.uri=http://localhost:9001/
ConfigClientApplication.java
@SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
ConfigClientWebController.java
Used to access configuration items for authentication
@RestController public class ConfigClientWebController { @Value("${app.env.name}") private String appEnvName; @RequestMapping("/getEnv") public String getEnv() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+",envName:"+appEnvName; } }
So far, the configuration of the client code is OK. Let's Run it together.
Access the service through the browser, http://localhost:9002/getEnv
Successfully obtained configuration item information in Git remote warehouse. The remote warehouse directory is as follows
Matters needing attention
So far, I have basically completed the construction and configuration of the server and client of the configuration center. Next, I have encountered problems in the construction process.
Question 1: configuration of spring.cloud.config.server.git.search-paths
At the beginning, the configuration parameters are as follows
spring.cloud.config.server.git.search-paths=/springCloudAProject/**
The Config Server starts normally, and the Config Client starts with the following error
java.lang.IllegalArgumentException: Could not resolve placeholder
Later, I found out the problem by myself/**
There is a certain correspondence, which can't be misused. After adjusting the directory structure, you can access it normally
Question 2
The Git of the company's project is managed by Alibaba cloud Code. After the server is configured, it always prompts that there is no permission to access it
After checking the permissions of the following items, they are also the master. The account and password are all right. How can they not have permission to access them. After struggling for a long time, I didn't make progress. Later, I checked the Spring Cloud Config configuration item and found that SSH access can be configured. Let's try. Adjust the server configuration as follows
spring.cloud.config.server.git.uri = git@code.aliyun.com:XXX/XProjectConfig.git spring.cloud.config.server.git.ignore-local-ssh-settings=true spring.cloud.config.server.git.private-key=XXX
There is another problem here. The configuration on the Internet is based on the yml configuration. I think it should be compatible. Copy the private key, and it seems that the configuration can't be recognized normally if it's not correct. Then I learned from Baidu that
The line before the beginning and end of the private key needs to be \ n, and the middle line needs to be \ connected; to ensure that the content of the private key is the green valid value text
As follows
spring.cloud.config.server.git.private-key=-----BEGIN RSA PRIVATE KEY-----\n\ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\ XXXXXXXXXXXXXXXXXXXXXXXXX\ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\ XXXXXXXXXXXXXXXXXXXXXXX\n\ -----END RSA PRIVATE KEY-----
This paper is a platform of operation tools such as blog group sending one article and multiple sending OpenWrite Release