spring cloud 配置部署
(一)consul注册中心配置部署
consul注册中心配置部署可以说是我最顺利进行配置部署的方式,但是介于consul每次启动需要重新部署配置到key/value数据库,所以本人并不是很喜欢(如果consul布置在远程服务端并且长时间开启则可以忽略)
1.开启consul服务器,如果只是想要简单使用则在consul.exe所在文件夹执行下方命令即可
consul agent -dev
如果是专业部署到远程服务端,我的思路是在远程开启服务端server,在本电脑开启client模式并加入到远程服务器所在的节点中。
服务端
注:
(1)如果所在局域网中有多台主机,则-bind设置成你所在主机ip即可
(2)-client必须设置为0.0.0.0,即允许局域网下所有主机进行客户端连接
consul agent -bootstrap-expect 2 -server -data-dir /data -node 节点名 -bind 0.0.0.0
-config-dir /config -enabled-script-checks=true -datacenter=数据中心名 -client=0.0.0.0
客户端
consul agent -ui ... -client=0.0.0.0 -join 服务端ip
这样就可以在本机浏览到服务端的注册情况,而不需要去服务端访问consul
2.spring cloud 配置
consul作为配置中心的好处是可以不用使用spring cloud的配置中心,即无需建立config-server就可以读取远程配置
首先需要在pom.xml中需要以下依赖:
<!-- 服务注册、发现 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--consul配置中心组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
3.bootstrap.yml的主要配置如下:
spring:
profiles:
active: development
application:
name: exam-service
cloud:
consul:
host: ${CONSUL_HOST:localhost}
port: ${CONSUL_PORT:8500}
discovery:
enabled: true
health-check-interval: 30s
config:
format: yaml
enabled: true
prefix: config
profile-separator: ":"
data-key: configuration
我使用的是yaml文件作为配置文件
大致解释下各个参数
spring.profiles.active: 启用的配置文件版本后缀
spring.application.name: 服务名称
cloud.consul.discovery.enabled: 开启自动发现服务功能,即可以发现其他注册的服务
cloud.consul.discovery.health-check-interval: 进行健康检查的时间,可以根据需要设置长度
cloud.consul.config.format: consul服务配置文件格式
cloud.consul.config.enabled: 开启consul配置中心服务
cloud.consul.config.prefix: consul配置中心前缀,可以理解为文件所在路径
cloud.consul.config.profile-separator: consul分隔符,可以理解为路径和文件名之间的分割标志
cloud.consul.config.data-key: 配置文件的key,由于使用的是key/value数据保存,一个key对应一个value,即配置文件内容
4.之后需要在consul服务端放置我们需要的配置文件,具体步骤如下:
(1)通过localhost:8500访问consul
(2)到key/value一栏,点击create
(3)我的路径配置大致为
注:不要忘记右下角设置文件类型
(4)保存,配置完成
运行,如果在日志栏没有出现datasource等配置丢失错误,说明加载成功,完成配置过程。
(二)使用git仓库以及spring-cloud-config作为配置中心
这种方法虽然前期工作可能要比consul困难一些,但是由于使用git仓库作为配置存放地,所以只要联网,就可以读取到配置文件信息,没有局域网的限制,替换配置文件使用git,方便团队协作。
1.首先需要在git上建立一个仓库,公有私有都可以,将所需服务配置文件放入其中
2.建立config-service子项目,在spring-boot主程序上加@EnableConfigServer注解
3. pom.xml需要依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
4. application.yml中配置如下
spring:
cloud:
config:
server:
git:
uri: git地址
basedir: target/config
search-paths: /**
default-label: master
label: master
git下的各个属性:
uri: git地址
basedir:配置中心读取git配置文件暂存位置
search-paths:git仓库配置所在位置,/**是搜索所有包含目录,如果有二级目录在这里进行设置
default-label:默认分支
5. bootstrap.yml文件下的配置
server:
port: 8088
spring:
application:
name: config-service
profiles:
# 指定为native,开启本地存储配置的方式,spring cloud微服务配置中心默认有3种方式:远程git仓库、远程SVN、本地方式(本地目录)
active: ${SPRING_PROFILES_ACTIVE:git}
security:
user:
name: ${CONFIG_SERVER_USERNAME:cust}
password: ${CONFIG_SERVER_PASSWORD:custhitachi}
6.配置config-client,即需要加载配置的子项目
pom.xml需要引入以下依赖:
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
bootstrap.yml配置如下(注意一定是bootstrap.yml,不是application.yml,不然读取配置会加载失败)
spring:
application: exam-service
cloud:
config:
enabled: true
fail-fast: true
retry:
max-attempts: 5
discovery:
# 默认false,设为true表示使用注册中心中的配置服务(服务发现)而不自己指定配置服务的地址(即uri)
enabled: true
service-id: config-service
username: ${CONFIG_SERVER_USERNAME:cust}
password: ${CONFIG_SERVER_PASSWORD:custhitachi}
profile: development
label: master
name: ${spring.application.name}
解释下上面配置:
spring.cloud.config.enabled:使用config作为配置中心
spring.cloud.config.fail-fast:使用快速失败机制
spring.cloud.config.retry.max-attempts:最大重试次数
spring.cloud.config.discovery.enabled:使用自动发现服务机制,开启后可以不设置uri
spring.cloud.config.discovery.service-id:要发现服务的唯一标识
spring.cloud.config.username/password:设置配置服务的用户名/密码
spring.cloud.config.profile:配置文件后缀,对应{application}-{profile}.yml中的profile
spring.cloud.config.label:所取得的分支
spring.cloud.config.name:配置文件名,对应{application}-{profile}.yml中的application
注意以上配置属性缺一不可,否则可能会加载失败
(我当时没有加name属性,以为spring.application.name可以自动识别,结果各种加载失败)