本文介绍下Spring Cloud配置中心,以及具体实现(GIT作为配置文件管理)
POM:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies>
bootstrap.yml:
spring: application: name: ConfigServer
application.yml:
--- server: port: 8888 management: context-path: /admin spring: profiles: Single cloud: config: server: git: uri: https://github.com/zxmthinker/config.git username: password: rabbitmq: host: 192.168.199.197 port: 5672 username: admin password: admin eureka: environment: dev #test,prod instance: statusPageUrlPath: ${management.contextPath}/info healthCheckUrlPath: ${management.contextPath}/health hostname: 10.25.193.111 instanceId: ${spring.application.name}:${server.port}:${random.value} leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30 client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://${eureka.instance.hostname}:8761/eureka/ region: default preferSameZoneEureka: true useDnsForFetchingServiceUrls: false healthCheck: true logging: config: classpath:logback.xml
GIT:
以上图为例
Gateway.properties:Gateway公共配置文件
Gateway.yml:Gateway公共配置文件
Gateway-G8771:profile为G8771的配置文件
如何访问
http://localhost:8888/Eureka/Single/dev
Eureka:微服务名称
Single:profile名称
dev:GIT上的分支
客户端如何配置
bootstarp.yml
spring: application: name: Eureka cloud: config: uri: http://10.25.193.111:8888application.ymlspring: profiles: G8771 cloud: config: profile: G8771 label: dev