Spring Cloud Config与Eureka配合使用
1.将Config Server和Config Client都注册到Eureka Server上。
spring:
application:
#d对应config server所获取的配置文件的{application}
name: foo
cloud:
config:
uri: http://localhost:8080/
#profile对应config server所获取的配置文件中的{profile}
profile: dev
#指定Git仓库的分支,对应config server所获取的配置文件的{label}
label: master
discovery:
#表示使用服务发现组件中的Config Server,而不是自己指定的Config Server的uri,默认为false
enabled: true
#指定Config Server在服务发现中的serviceId,默认是configserver
service-id: eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
简要说明一下bootstrap.yml中的配置属性:
spring.cloud.config.discovery.enabled = true:表示开启通过服务发现组件访问Config Server的功能;
spring.cloud.config.discovery.service-id:指定Config Server在服务发现组件中serviceId;
SpringCould Config的用户认证
1.复制项目microservice-config-server,将ArtifactId修改为microservice-config-server-authenticating。
2.为项目添加以下依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.在application.yml添加以下内容
spring:
security:
basic:
enabled: true #开启基于HTTP basic的认证
user:
name: user #配置登录账号
password: password123 #配置登录密码
这样酒味Config Server添加了基于HTTP basic的认证。如果不设置这段内容,账号就默认是user,密码是一个随机值,该值会在启动时打印出来。
Config Client连接需用户认证的Config Server
方式一:使用curl风格的Url。实例:
spring:
cloud:
config:
uri:http://user:password123@localhost:8080/
方式二:指定Config Server的账号与密码。实例:
spring:
cloud:
config:
uri: http://localhost:8080/
username: user
password: password123
本文大部分内容转载自周立的《Spring Cloud与Docker微服务架构实战》