First, Introduction 1, why need to configure the center?
When the service is deployed more and more, the scale becomes bigger, the corresponding machine quantity is also more and more large, it becomes difficult and error-prone to manage and maintain the service configuration information by manpower.
Therefore, a place to dynamically register and obtain service information is required to unify the management Service name and its corresponding server list information, called the Service configuration Center. When the service provider starts, it registers the service name and server address it provides to the Service configuration center. Service consumers through the Service configuration center to obtain the services to be called, through the corresponding load balancing algorithm, select one of the servers to start the call.
2, what is Spring Cloud Config
Spring Cloud Config is a component in spring cloud, a distributed configuration center component that enables configuration services to be placed in the memory of the configuration service (that is, local) and also in a remote Git repository. In Spring Cloud Config component, there are two roles, one is config server and the other is config client.
Two, Spring Cloud Config Combat 2.1, build config Server2.1.1, create a spring-boot project named Config-server, Pom.xml relies on the following: Note the component version I use
Spring Boot 2.0.x
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starte R-parent</artifactid> <version>2.0.4.RELEASE</version> <relativePath/> <!--Looku P Parent from repository--</parent> <properties> <project.build.sourceencoding>utf-8& Lt;/project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> <SPRING-CLOUD.VERSION&G T finchley.release</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <ARTIFACTID>SPRING-CLOUD-CONFIG-SERVER&L t;/artifactid> </dependency> <dependency> <groupid>org.springframework.boot& Lt;/groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </depen Dencies>
2.2, add annotations to the entry class Configserverapplication
@EnableConfigServer, open Config Configuration Center Server 2.3, new configuration file in root directory
config-dev.ymland submit to the Git repository, the instance is the MySQL data originally configured information, submitted by GitHub, the configuration file is as follows:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.10.100:3306/spring?useSSL=false username: test password: 123456
2.4, configure the APPLICATION.YML, configured as follows:
spring: application: name: config-server cloud: config: server: git: uri: github.com/jarvisqi/spring-cloud-microservice search-paths: config-server username: password: default-label: masterserver: port: 9400
- Spring.cloud.config.server.git.url: Specify Git's warehouse address and change it to your own
- Spring.cloud.config.server.git.search-paths: Specify the directory where your configuration files are located
- Username,password if it's GitHub public, you don't have to add your own private repository.
2.4, start Config-server, and visit:
http://localhost:9400/config/devAddress, see the following information, indicating that the server is operating properly:
{ "name": "config", "profiles": [ "dev" ], "label": null, "version": "e64289cb775e2ac7db7494c07d7e8c4933163daf", "state": null, "propertySources": [ { "name": "github.com/jarvisqi/spring-cloud-microservice/config-server/config-dev.yml", "source": { "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver", "spring.datasource.url": "jdbc:mysql://192.168.10.100:3306/spring?useSSL=false", "spring.datasource.username": "test", "spring.datasource.password": 123456 } } ]}
The key below source is the configured information, and the HTTP request address and resource file mappings are as follows:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
2.2, build config Client2.2.1, create a spring-boot project named Config-client, Pom.xml relies on the following: Note the component version I'm using
Spring Boot 2.0.x
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start Er-parent</artifactid> <version>2.0.4.RELEASE</version> <relativePath/> <!--look Up parent from repository-</parent> <properties> <project.build.sourceencoding>utf-8 </project.build.sourceEncoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> <SPRING-CLOUD.VERSION&G T finchley.release</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</a rtifactid> </dependency> <dependency> <groupId>org.springframework.cloud< /groupid> <artifactid>spring-cloud-starter-config</artifactid> </dependency> <dependency> <groupid>org .springframework.boot</groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </DEP Endency> </dependencies>
2.2.2, configure the APPLICATION.YML, configured as follows:
spring: application: name: config-client cloud: config: uri: http://localhost:9400 name: config profile: dev label: masterserver: port: 9410
- Spring.cloud.config.uri: Specify Config-server server address, note that it is not a git warehouse address
- Spring.cloud.config.name:
注意 If name the value is not written, will default to spring.application.name the value, the resource file name becomes config-client-dev , according to the HTTP request address and resource file mapping, must not be found, will error, unable to start
- Spring.cloud.config.profile: It's usually dev, test, PRD, you can customize it, because the file I'm submitting is dev.
- Spring.cloud.config.label:git's Branch
2.2.3, configure the BOOSTRAP.YML, configured as follows:
spring: cloud: config: uri: http://localhost:9400
注意, there is a pit, if you do not add the boostrap.yml file and specify spring.cloud.config.uri the specified, start the client will error, will default to specify the port: 8888, not a custom port, can not be started error, of course, you can directly use boostrap.yml , I see a lot of people here are experiencing problems, The problem is that the reduction version to 1.5.X is correct, this can also calculate the solution?
2.2.4, add the Configcontroller interface and get the configuration information as follows:
@RestControllerpublic class ConfigController { @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @RequestMapping("/dataconfig") public DataConfig getDataConfig() { DataConfig config = new DataConfig(driverClassName, url, username, password); return config; }}
2.2.5, first start the config-server, ensure normal operation, and then start config-client, and request the address
http://localhost:9410/dataconfig, the following results are obtained, indicating that the configuration center is complete:
{ "driverClassName": "com.mysql.jdbc.Driver", "url": "jdbc:mysql://192.168.10.100:3306/spring?useSSL=false", "username": "test", "password": "123456"}
Above, the configuration center can be deployed separately, and the configuration information values need to be submitted to the specified directory by git, which can be obtained by each of the MicroServices ' independent service applications.
Attached Source: github.com/jarvisqi/spring-cloud-microservice.git
Reference:
- Http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html