1.简介
- 优先级:远程配置 > 命令行参数配置 > application > bootstrap
- 应用启动初期仅读取bootstrap,此阶段获得的应用本地配置路径(application)读取本地配置,最后获取远程配置
- {application}映射到客户端的“spring.application.name”;{profile}映射到客户端上的“spring.profiles.active”(逗号分隔列表); 和{label}这是分支名称
2.开启配置中心服务
pom
<!--开启配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--//注册为服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--开启rrbq消息队列-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
|
application
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
|
properties
详见附表一
3.配置配置客户端
boostrap.properties(不能放在applaction里)
#从配置中心获取配置
spring.application.name=配置中心配置文件名称
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.failFast=true
#注册为服务
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
feign.hystrix.enabled=true//开启降级服务
|
application
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
|
pom
<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>
<!--开启rrbq消息队列-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--//注册为服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
|
4.架构设计
附表一
application
# 注册为微服务
spring.application.name=config-server
server.port=3001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
# 设置webhook密码
encrypt.key=123456
# git管理配置
spring.cloud.config.server.git.uri=https://github.com/silcat/config-server
spring.cloud.config.server.git.username=silcat
spring.cloud.config.server.git.password=a909983218a
# 开启本地配置
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:
/test
# 开启rabmq消息队列,配合webhook开启自动配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=spring
spring.rabbitmq.password=123456
management.security.enabled=false
|
内部application
#定义全局变量
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=spring
spring.rabbitmq.password=123456
management.security.enabled=false
|
zuul
server.port=2001
zuul.ignoredServices=classpath:/static/**
zuul.routes.factory.path=/factory/**
zuul.routes.shop.path=/shop/**
feign.hystrix.enable=true
zuul.host.socket-timeout-millis=60000
zuul.host.connect-timeout-millis=10000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
##ribbon设置
#当ribbon.eureka.enabled=false实现负载均衡,shop与shop-1轮寻,集群使用
ribbon.eureka.enabled=false
shop.ribbon.listOfServers=111.11.11.11:4001,111.11.11.11:4002
factory.ribbon.listOfServers=
172.16.20.202:5001
#当ribbon.eureka.enabled=true 不能实现,单实例使用
#ribbon.eureka.enabled=true
#zuul.routes.shop.serviceId=shop
#zuul.routes.shop-1.path=/shop/**
#zuul.routes.shop-1.serviceId=shop-1
#zuul.routes.factory.serviceId=factory
|
turbine集群
#服务端
server.port=9090
spring.application.name=turbine
turbine.appConfig=shop,shop-1//服务中心注册需要集群的服务
turbine.aggregator.clusterConfig= shop
turbine.clusterNameExpression= metadata['
cluster']//访问http://../turbine.stream?
cluster=MAIN
|
#需要node客户端添加:
eureka.instance.metadata-map.cluster=
MAIN
|
其他环境配置
#端口设置
server.port=2001
#feign与hystrix设置
feign.hystrix.enabled=true//开启降级服务
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000//调整全局超时错误时间,默认1000
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds //指定CommandKey方法名的超时时间是该值
#线程池
hystrix.threadpool.default.coreSize=10//线程池核心数
#断路器
- hystrix.command.default.circuitBreaker.requestVolumeThreshold=20 //当在配置时间窗口内达到此数量的失败后,进行短路。默认20个)
- hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5 //短路多久以后开始尝试是否恢复,默认5s)
- hystrix.command.default.circuitBreaker.errorThresholdPercentage=50% //出错百分比阈值,当达到此阈值后,开始短路。默认50%)
#数据库设置
.....
|
参考:http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/