preface
I haven’t updated it for 4 days. How can I say, I’m very busy, very busy, very busy. It’s mainly about graduation and moving from one city to another. During this period, it should be kept every 2 or 3 days as far as possible, and no more than 4 or 5 days at the latest.
With the increase of modules, there will be a common problem of complicated configuration files. You have to open many layers of directories to find the configuration files each time. The config component in springcloud is to solve this problem. The unified management of configuration files can be realized through simple configuration.
Config server
Importing config server
Create an empty config parent module, create a config server sub module below, and modifyPOM file of sub module
Note that it is the POM file of the child module. Unlike before, the POM file of the empty parent module is modified
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
configuration file
Because it is not necessary to register in Eureka for the time being, the preparation of the configuration file is relatively simple
server:
port: 8101
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/cutey_none/springcloud-study-config
username:
password:
label: master
spring.cloud.config.server.git.uri
: the address where the file is stored. The client will get the configuration file from here. It can be local or Git
If it is an open warehouse, then
username
andpassword
Do not write
Because createdspringcloud-study-config
The permissions set for the warehouse are open, so it is OK to directly use mine or create a warehouse by yourself. Just create a warehouse in the normal GitHub or gitee.
The warehouse places the configuration files of each micro service
An example is managementconfig-client
The configuration file of the microservice (which will be created later), so you need to create one in the warehouseconfig-client-dev.properties
(-dev indicates the configuration file in the development environment)
config-client-dev.properties
The contents of the document are as follows, which can be regarded asconfig-client
Some configurations of the service
Main startup class
increase@EnableConfigServer
Annotation to provide config service support
@SpringBootApplication
@EnableConfigServer
public class ConfigServer8101 {
public static void main(String[] args) {
SpringApplication.run(ConfigServer8101.class, args);
}
}
test
Springcloud config has its own HTTP service to access resources
/{application}/{profile}[/{label}]
>>/config-client/dev
/{application}-{profile}.yml
>>/config-client-dev.yml
/{label}/{application}-{profile}.yml
>>/master/config-client-dev.yml
/{application}-{profile}.properties
>>/config-client-dev.properties
/{label}/{application}-{profile}.properties
>>/master/config-client-profile.properties
Single runConfigServer8101
You can access resources from the server by using the above five methods
Config client
Importing config client
It’s been said beforeconfig-client
This is the module to be created next. The final project structure directory is as follows
Then modifyconfig-client9501
The POM file of the module. Note that the dependencies introduced by the server and the client are different
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
configuration file
The name of the configuration file is
bootstrap.yml
Because the client needs to set the URI of the server, it shouldLoad client configuration file first
server:
port: 9501
spring:
application:
name: config-client
cloud:
config:
profile: dev
label: master
uri: http://localhost:8101
spring.cloud.config.uri
: the address of the server, where to get the configuration file
Main startup class and business class
@SpringBootApplication
@RestController
public class ConfigClient9501 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient9501.class, args);
}
@Value("${name}")
String name;
@GetMapping("/hi")
public String hello() {
return "hello, " + name;
}
}
You may encounter a prompt here that the placeholder ${name} cannot be found. It must be that the steps have not been followed
test
Note that there is no name variable in the above client configuration file
The items on the server do not need to be stopped and then openedconfig-client9501
Project. The open projects are as follows
Visit belowlocalhost:9501/hi, you can see the following under normal conditions
In fact, I have taken quite a lot of spare time to write, but sometimes when I encounter some problems, I still need to understand them as much as possible and then speak them out in a simple way. I hope I can help you to read this article!!!
Creation is not easy. If it helps you, you are welcome to like, collect and share!
The following is a personal official account. If you are interested, you can pay attention to it. It may be your treasure official account. It is basically 2, 3 days and 1 more technical article!!!