1. 简介
分布式系统中,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件springCloud Config ,它支持从远程Git仓库中读取配置文件并存放到本地Git仓库。接下来我们来看一下服务端和客户端分别应该如何配置。
2. 配置中心服务端
2.1 pom.xml
2.2 启动类添加注解
启动类增加注解@EnableConfigServer
2.3 配置git仓库
在码云创建一个仓库,新建配置文件
文件内容写上用户微服务的application.yml文件的内容
server:
port: 7070
mybatis:
type-aliases-package: com.cloud.entity
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
application:
name: pj-provide-user
security:
user:
name: user
password: password
#logging:
# level:
# com.cloud: info
logging:
level:
root: INFO
org.springframework.web.servlet.DispatcherServlet: DEBUG
org.springframework.cloud.sleuth: DEBUG
eureka:
client:
healthcheck:
enabled: true # 健康检查
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:9090/eureka
instance:
prefer-ip-address: true # 在eureka显示ip
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
env: dev
branch: master
2.4 配置yml文件
3. 配置中心客户端(用户微服务)
3.1 添加jar包依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
3.2 修改对应服务的配置文件名称
把application.yml 改为 bootstrap.yml
3.3 配置文件
eureka:
client:
service-url:
defaultZone: http://user:password@localhost:9090/eureka
spring:
application:
name: pj-provide-user # 这个名字需要和git的配置文件的名字需要统一
#指定从哪个配置中心读取
cloud:
config:
discovery:
service-id: CONFIG-SERVER
enabled: true
profile: dev
#建议用lable去区分环境,默认是lable是master分支
label: master
4. springcloud总结
1)介绍微服务的基础知识,核心组件,CAP原理
2)SpringCloud注册中心 Eureka
3)product-service / order-service
4) 伪RPC Ribbon / feign
5) hystrix熔断
6) 服务网关介绍,zuul
7)配置中心config-server