中心认证
- 中心认证和之前eureka的认证方式一致,通过 security 安全认证
1.server端添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.配置文件增加security配置
security:
basic:
enabled: true
user:
name: user
password: password123
3.client端增加配置
spring:
cloud:
config:
uri: http://localhost:8888 # curl style
username: user
password: password123
# 或者
spring:
cloud:
config:
uri: http://user:password123@localhost:8888 # curl style
服务端注册到Eureka实现高可用
- 搭建多个Conf的服务端注册到Eureka实现高可用
Server端
1.服务端添加依赖
<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>
2.入口程序添加注解@EnableEurekaClient,@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class MicroserviceConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConfigServerApplication.class, args);
}
}
3.配置文件,配置conf,eureka信息
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/xxx/xxxx
search-paths:
- bar # bar路径
username:
password:
application:
name: microservice-config-server
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:1234@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
Client端
1.添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.入口程序添加注解@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3.配置文件
- 在conf 前章中说道要将conf的配置放置到bootstrap.yml中,程序会先加载bootstrap.yml去获取配置信息。可是更改为cloud.config.discovery的方式去发现服务端时,根据实测,所有的配置信息都要放置在application.yml中才有效,conf的配置放置到bootstrap.yml中则会报错。
server:
port: 8081
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:1234@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
spring:
application:
name: bar
cloud:
config:
discovery:
enabled: true
service-id: microservice-config-server
# uri: http://localhost:8888
profile: dev
label: master # 当configserver的后端存储是Git时,默认就是master
结合Spring Cloud Bus组件的自动刷新
- Spring Cloud Bus使用轻量级的消息中间件将分布式系统中的节点连接起来。这可以在集群中广播状态改变(比如配置更改)或其他管理指令。总线之于Spring Boot应用程序即是一个可扩展的分布式Actuator,也是各个应用之间通信的通道。
架构图:
1.轻量级的消息中间件采用RabbitMQ,具体安装参考其官网
2.在conf服务端即客户端都添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
3.在Config Server和Config Client的bootstrap.yml中添加对RabbitMQ的配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: 1234@pass
4.访问服务端/bus/refresh链接(http://localhost:8888/bus/refresh),服务端则会让RabbitMQ将更新的消息发送给各个客户端,即更新配置信息。