简述---统一配置中心也是一个微服务
下面开始建立,选择Eureka 客户端,和Config的Server端
开启注解并注册服务端
package com.sola;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
server.port=30530
spring.application.name=Config-Server
eureka.client.service-url.defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
下面开始运行,报错,大概说要一个git repository
什么鬼设定,那么我们去码云上建立个项目。。。然后把roadwork的appliction.properties上传一份(并不是鬼设定,这样去直接修改git里面的配置文件就可以了,用本地没有这样方便)
然后再去项目配置文件配置路径,没有账号密码也无法访问所以还要配置上git账号密码。。。
server.port=30530
spring.application.name=Config-Server
eureka.client.service-url.defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
spring.cloud.config.server.git.username=账号
spring.cloud.config.server.git.password=密码
spring.cloud.config.server.git.uri=https://gitee.com/choufengtongxue/Config-repository
然后运行。。。。然后特么的还真运行成功了
然后去访问地址 格式貌似是XXX-XXX.xxx 还真访问成功了。。。
不过数据位置和源文件不太一样
就算是源文件是properties文件 后缀打yml也能转成yml格式,还可以点json,也会转成json格式
命名规则 label不写的话就是master分支
之前有说过他是去git去找配置文件并且往自己本地存放一份git配置,下面是配置存放路径
server.port=30530
spring.application.name=Config-Server
eureka.client.service-url.defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
spring.cloud.config.server.git.uri=https://gitee.com/choufengtongxue/Config-repository
spring.cloud.config.server.git.basedir=D:\SpringCloudWorkSpace\Config_Server\localgit
---------------------------------------------------------------------------------------------------------------------
还好有本书,书上写的是本地的配置方法,不用git
开启服务支持和之前一样主要在于配置
server.port=30530
spring.application.name=Config-Server
eureka.client.service-url.defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
//这里设置的就是本地,没有设置默认就用的是git
spring.profiles.active=native
//这个是设置配置文件的路径
spring.cloud.config.server.native.search-locations=classpath:/config
文件路径也可以这么写
spring.cloud.config.server.native.search-locations=D:/nativeconfig
目前测试直接改文件内容比如 postman+123 去按路径访问配置也会同步更新
之前都是Config统一配置中心服务端,下面介绍客户端的配置
在客户端例如PostMan里的POM文件添加 config client的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
这里启动类不需要加入启动注解
然后去配置postman的application
spring.application.name=postman
//开启固定格式
spring.cloud.config.discovery.enabled=true
//配置统一配置管理的id
spring.cloud.config.discovery.service-id=CONFIG-SERVER
//横岗后面的名字
spring.cloud.config.profile=best
统一配置中的properties文件如下
server.port=30525
applications
spring.datasource.url=jdbc:oracle:thin:@192.168.66.226:1521:znjt
spring.datasource.username=znjtpostman
spring.datasource.password=znjtpostman
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#mybatis
#mybatis.mapper-locations=classpath:/mappers/postman/*Mapper.xml
mybatis.mapper-locations=classpath*:com/sola/mappers/**/*.xml
#mybatis.type-aliases-package=briliance.znjt.business.template.entity
spring.application.name=EurekaClient-PostMan
eureka.client.service-url.defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
然后运行会报错,报错原因文件读取先后的问题,假如读取postmanapplication的指向配置,与系统同时去读postmanapplication里的数据源信息 那么 数据源先去读,因为还没加载指向的配置 所以他就会报错找不到数据源。
所以要去指定先去读取指向的配置中心的配置
把Postman的Application的配置文件改名成bootstrap(bootstrap.yml或boostrap.properties会比application先一步读取)
spring:
application:
name: postman
cloud:
config:
discovery:
enabled: true
service-id: CONFIG-SERVER
profile: best
eureka:
client:
service-url:
defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
这里Service-id是注册中心的id,这里也可以改写成
spring.cloud.config.uri= http://localhost:30530/ 写死,不过这样不具备灵活性
有个不知道是不是的坑
eureka:
client:
service-url:
defaultZone: http://localhost:30520/eureka/,http://localhost:31520/eureka/
要写到bootstrap里,不能写在postman里
个人理解可能是因为没有注册到注册中心找不到id为Config-Server的地址
----------------------------------------------------------------------------------------------------------
注册中心的高可用
和Eureka Server一样,configServer也是一个微服务,那么操作方法一样,多启动几个号就好了= =
----------------------------------------------------------------------------------------------------------
配置中心还有一个小坑,那就是假如配置中心有两个配置文件
postman.yml
postman-best.yml
两个文件内容一致。
假如要用到best,然后想把里面的一部分删除,比如数据库用户名密码删除,然后删掉再去读取。
会发现还是会有账号和密码。
他的机制是这样postman-best.yml和postman.yml。
如果去读取postman-best.yml,他也会读取postman.yml的内容做一个整合再返回来。
是这样的一个机制,所以要做以上操作,也要把postman的内容删掉。
这样的机制有一个好处,那就是可以把通用的配置提取出来到postman。
之后的派生配置就不用写了。
----------------------------------------------------------------------------------------------------------
然而讲了这么多还是没有达到直接修改配置不用重启项目的效果,请学习下章节。。。。