介绍
本文介绍spring cloud 集群配置https 。
ssl证书的生成这里就不再赘述,网上有一大堆。
集群成员
eureka:注册中心
configserver:配置中心
gateway:网关
其他server
配置
eureka:
server:
port: 8761
http2:
enabled: true
ssl:
enabled: true
#证书配置
key-store-password: ****
key-store-type: PKCS12
key-store: classpath:****.pfx
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: https://${eureka.instance.hostname}:${server.port}/eureka/
instance:
hostname: localhost
# 重要配置
securePortEnabled: true
nonSecurePortEnabled: false
configserver:
eureka:
client:
service-url:
defaultZone: https://localhost或者域名:8761/eureka/
instance:
# 使用ip的方式访问configserver
prefer-ip-address: true
# 不开启安全端口
#securePortEnabled: true
configserver这里我们不开启https的形式访问,如果开启的话,服务的配置需要指定:
spring.config.uri = https://域名:8888
这样就无法使用注册中心发现服务。
如果configserver开启https形式访问,其他服务使用注册中心发现配置中心服务的话,服务启动时会报错:
No subject alternative names matching IP address xxxx found
gateway:
server:
port: 8443
http2:
enabled: true
ssl:
enabled: true
#证书配置
key-store-password: ****
key-store-type: PKCS12
key-store: classpath:****.pfx
spring:
application:
name: gateway
cloud:
config:
discovery:
enabled: true
fail-fast: true
# 使用https形式访问配置中心,若配置中心配置了https 不使用此方式无法访问
#uri:https://域名:端口
gateway:
httpclient:
ssl:
# 信任下游证书
use-insecure-trust-manager: true
eureka:
client:
service-url:
defaultZone: https://localhost或者域名:8761/eureka/
instance:
# 开启安全端口,告诉其他服务我的端口是https访问方式
securePortEnabled: true
prefer-ip-address: true
注意:如果不使用http2 可将server.http2.enabled 配置去掉
spring cloud 2021.0.0 才开始支持使用http2
http2 需在支持https的前提下使用