Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: shared session for multi apollo portal #3786

Merged
merged 7 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Apollo 1.9.0
* [feat(apollo-client): add method interestedChangedKeys to ConfigChangeEvent](https://github.com/ctripcorp/apollo/pull/3666)
* [add More... link for known users](https://github.com/ctripcorp/apollo/pull/3757)
* [update OIDC documentation](https://github.com/ctripcorp/apollo/pull/3766)
* [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)

14 changes: 14 additions & 0 deletions apollo-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion apollo-portal/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions apollo-portal/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/zh/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

- 使用文档
Expand Down
4 changes: 4 additions & 0 deletions docs/zh/development/apollo-development-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
78 changes: 78 additions & 0 deletions docs/zh/development/portal-how-to-enable-session-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
从 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 的表
vdiskg marked this conversation as resolved.
Show resolved Hide resolved
spring session 提供了自动建表功能, 请确保使用的数据库帐号具有 DDL 权限, 然后配置 `spring.session.jdbc.initialize-schema=always`(System Property, 环境变量, 外部配置文件均可) 即可。
一共会自动创建两张表 `spring_session` 和 `spring_session_attributes` 创建完成后配置 `spring.session.jdbc.initialize-schema=never` 否则每次启动都会尝试去建表,虽然没有其它影响但是会刷一大堆错误日志。