A. What is Distributed Configuration Center?
Is to provide centralized services for the micro-architecture of the micro-service external configuration support, configuration center provides external configuration of the center of each micro-environment for all service applications (may be more difficult to understand, wanted to know what it means will know why so configuration: in order to solve such a configuration is application.properties configuration management issues in a number of micro-services provider, as well as redundant configuration issues, to focus on these configurations to be stored together, and the repeated configuration extracted resolve redundancy)
1 store our configuration file on git Hub
2 config-server to remotely connect to git Hub.
3 config-client connection to the config-server.
Run: When we started config-client service time, client will pass config-server connection git above to get the remote configuration file, and then loaded into the subject through Spring.
A github account can have many warehouses -> a warehouse corresponds to only one item -> so the project name the name of the warehouse is to be submitted
if it is a new account, you must first have a namespace (also his creation, you can arbitrarily named)
Select File-> clone repository-> Select loaded into local projects
4. Create three files (divided dev: development environment, test: test environment, pro: on-line environment)
开发环境:
application-dev.properties
spring.profiles=dev
server.port=3081
spring.application.name=application-dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dev?useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
测试环境:
application-test.properties
spring.profiles=test
server.port=3081
spring.application.name=application-test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
当使用github desktop提交代码到github上的时候,只能一个一个的提交,不能一起提交
选择commit to master(记住这个master)
选择repository选择–>push
无论是yml还是properties都可以使用该规则进行访问:
/{application}/{profile}[/{label}]
properties文件:
/{application}-{profile}.properties
/{label(分支)}/{application}-{profile}.properties
yml文件:
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
#首先还是常规的:
server.port=4081
server.servlet.context-path=/
#配置application.name (可配置,可不配置) ,在此配置是为了提醒eureka中的这个配置(因为eureka中服务的发现就是找的这个名字),不要忘记
spring.application.name=springcloud-config-server-4081 #开始配置GitHub
#先配置GitHub的仓库的地址(在浏览器的地址栏上,直接复制就好) spring.cloud.config.server.git.uri=https://github.com/命名空间/仓库名 #配置GitHub的账号和密码 spring.cloud.config.server.git.username=邮箱/账号 spring.cloud.config.server.git.password=密码 #配置GitHub的仓库的搜索路径(固定的不要补全!!!) spring.cloud.config.server.git.search-paths=config-repo #跳过SSL的认证 spring.cloud.config.server.git.skip-ssl-validation=true
5.1.3 ApplicationRun启动类注解
除了常规的@@SpringBootApplication外,还有一个@EnableConfigServer,标明是server层的配置中心
<dependencies>
<dependency>
<groupId>com.wlx.springcloud</groupId> <artifactId>20191108-management-model</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
此处的配置文件和之前的有区别,有两个配置文件 bootstrap.properties 和 application.properties 文件,配置两个文件的作用就是:查漏补缺。
把相同的配置放到了GitHub上,有差异的配置放在了application文件中,加载的时候会把这两个文件进行合并
Bootstrap.properties文件:
#从github上读取所要配置的文件的名称
#从GitHub上的repository读取文件名
#根据读取的规则:不要加后缀名 .properties或.yml
spring.cloud.config.name=application-dev
#配置prpfile的名称 #必须要和GitHub上文件配置中心中的spring.profile的值一致,否则匹配不到 spring.cloud.config.profile=dev #配置label(master) 如果使用默认的就不需要配置 spring.cloud.config.label=master #配置config的 服务器端 的地址及端口 spring.cloud.config.uri=http://localhost:端口号
Application.properties file:
#一定要和bootstrap.properties中的spring.cloud.config.name的值一致,否则映射不到`
spring.application.name=application-dev
5.2.3 test whether a successful link server layer server, load the cloud configuration file
Create a new directory controller -> Create a controller class test TestController, use @Value notes to get the value in the configuration file
@RestController
public class TestController { @Value("${spring.datasource.driver-class-name}") private String driverClassName; @RequestMapping("/test") public String test(){ return driverClassName; } }
First blog, do not like do not spray! ! !