未验证 提交 bf80850a 编写于 作者: V vdisk-group 提交者: GitHub

feature: shared session for multi apollo portal (#3786)

* add spring session support

* fix spring session support unit test

* move default store-type to application.yml

* add doc

* add CHANGES

* update doc
上级 f1eca02f
......@@ -48,6 +48,7 @@ Apollo 1.9.0
* [update OIDC documentation](https://github.com/ctripcorp/apollo/pull/3766)
* [feature: add Spring Boot 2.4 config data loader support](https://github.com/ctripcorp/apollo/pull/3754)
* [feat(open-api): get authorized apps](https://github.com/ctripcorp/apollo/pull/3647)
* [feature: shared session for multi apollo portal](https://github.com/ctripcorp/apollo/pull/3786)
------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/6?closed=1)
......@@ -51,6 +51,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- yml processing -->
<dependency>
<groupId>org.yaml</groupId>
......
......@@ -23,7 +23,8 @@ spring:
hibernate:
query:
plan_cache_max_size: 192 # limit query plan cache max size
session:
store-type: none
server:
port: 8070
compression:
......
......@@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring.session.store-type=none
spring.datasource.url = jdbc:h2:mem:~/apolloportaldb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
......
......@@ -18,6 +18,7 @@
- [Intellij Code Style](https://github.com/ctripcorp/apollo/blob/master/apollo-buildtools/style/intellij-java-google-style.xml)
- [Portal实现用户登录功能](zh/development/portal-how-to-implement-user-login-function.md)
- [Portal接入邮件服务](zh/development/portal-how-to-enable-email-service.md)
- [Portal 共享 session](zh/development/portal-how-to-enable-session-store.md)
- [Portal启用webhook通知](zh/development/portal-how-to-enable-webhook-notification.md)
- 使用文档
......
......@@ -239,3 +239,7 @@ Apollo客户端针对不同的环境会从不同的服务器获取配置,所
## 3.2 Portal 接入邮件服务
请参考[Portal 接入邮件服务](zh/development/portal-how-to-enable-email-service)
## 3.3 Portal 集群部署时共享 session
请参考[Portal 共享 session](zh/development/portal-how-to-enable-session-store)
从 1.9.0 版本开始,apollo-portal 增加了 session 共享支持,从而可以在集群部署 apollo-portal 时实现 session 共享。
## 使用方式
### 1. 不启用 session 共享(默认)
默认配置即为不启用
所以清除 session 共享相关的配置即可,需要清理的配置如下
外置配置文件(properties/yml)里面的 `spring.session.store-type` 配置项
环境变量里面的 `SPRING_SESSION_STORE_TYPE`
System Property 里面的 `spring.session.store-type`
### 2. 基于 Redis 的 session 共享
有以下几种方式设置,按照优先级从高到低分别为:
注:redis 也支持集群、哨兵模式,配置方式为标准的 `Spring Data Redis` 模式(以 `spring.redis` 开头的配置项),具体方式请自行研究 `Spring Data Redis` 相关文档或咨询 `Spring Data` Group
#### 2.1 System Property
```bash
-Dspring.session.store-type=redis
-Dspring.redis.host=xxx
-Dspring.redis.port=xxx
-Dspring.redis.username=xxx
-Dspring.redis.password=xxx
```
#### 2.2 环境变量
```bash
export SPRING_SESSION_STORE_TYPE="redis"
export SPRING_REDIS_HOST="xxx"
export SPRING_REDIS_PORT="xxx"
export SPRING_REDIS_USERNAME="xxx"
export SPRING_REDIS_PASSWORD="xxx"
```
#### 2.3 外部配置文件
例如 `config/application-github.properties`
```properties
spring.session.store-type=redis
spring.redis.host=xxx
spring.redis.port=xxx
spring.redis.username=xxx
spring.redis.password=xxx
```
### 3. 基于 JDBC 的 session 共享
有以下几种方式设置,按照优先级从高到低分别为:
#### 3.1 System Property
```bash
-Dspring.session.store-type=jdbc
-Dspring.datasource.url=xxx
-Dspring.datasource.username=xxx
-Dspring.datasource.password=xxx
```
#### 3.2 环境变量
```bash
export SPRING_SESSION_STORE_TYPE="jdbc"
export SPRING_DATASOURCE_URL="xxx"
export SPRING_DATASOURCE_USERNAME="xxx"
export SPRING_DATASOURCE_PASSWORD="xxx"
```
#### 3.3 外部配置文件
例如 `config/application-github.properties`
```properties
spring.session.store-type=jdbc
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
```
#### 关于初始化 session 的表
##### 1. apollo-portal 应用自动建表
给 apollo-portal 准备好具有 DDL 权限的数据库帐号。
然后首次启动时配置 `spring.session.jdbc.initialize-schema=always`(System Property,环境变量,外部配置文件均可) 即可,
一共会自动创建两张表 `spring_session``spring_session_attributes`
创建完成后配置 `spring.session.jdbc.initialize-schema=never`,否则每次启动都会尝试去建表,会刷一大堆错误日志(无实际影响)。
##### 2. 临时部署建表应用进行自动建表
准备好一个给 apollo-portal 使用的普通权限的数据库帐号,以及一个给建表临时应用使用的具有 DDL 权限的数据库帐号。
部署一个 apollo-portal 作为临时建表应用,配置如下
```properties
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
spring.datasource.url=xxx
spring.datasource.username={{DDL权限帐号}}
spring.datasource.password={{DDL权限帐号的密码}}
```
应用启动完成后检查数据库里 `spring_session``spring_session_attributes` 这两张表是否创建完成,创建完成即可停止并删除该临时应用,使用普通数据库帐号配置部署 apollo-portal 集群即可。
##### 3. 手动建表
可以选择手动在数据库执行建表语句,sql 脚本由 [spring-session](https://github.com/spring-projects/spring-session) 提供
具体的建表 sql 如下,请根据所使用的数据库选择对应的 sql 脚本
[db2.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-db2.sql)
[derby.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-derby.sql)
[h2.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-h2.sql)
[hsqldb.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-hsqldb.sql)
[mysql.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-mysql.sql)
[oracle.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-oracle.sql)
[postgresql.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-postgresql.sql)
[sqlite.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-sqlite.sql)
[sqlserver.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-sqlserver.sql)
[sybase.sql](https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-sybase.sql)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册