---
title: "Spring Boot 2.0升级小记"
author: "wiselyman"
date: "2018-02-28T16:38:20Z"
tags: []
categories: ["企业架构"]
slug: "spring-boot-2-0"
draft: false
lastmod: "2018-02-28T16:38:20Z"
id: 2411813
url: "https://wiselyman.iteye.com/blog/2411813"
---

### 1. 背景

> 项目原先的`Spring Boot`版本为`1.5.4.RELEASE`，`Spring Cloud`版本为`Dalston.SR2`，升级后的`Spring Boot`版本为`2.0.0.M7`，`Spring Cloud`版本为`Finchley.M5`，因版本升级导致各组件API以及依赖包发生变化，下面是项目中用到的组件的升级方法。

### 2. Spring Security

> 项目使用`Oauth2`协议规范，认证的`Token`令牌使用`Redis`进行存储。

#### 2.1 授权服务器(authorization server)

- 客户端配置的密码由原先的明文修改为密文。

```
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("android")
                .scopes("xx")
                .secret("$2a$10$sCeoatJoccD4y1ff8AhdROLd2u6AWYBwH7YrbWXxdk6fA4VGhGITm") // 原先的代码:.secret("android")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token");
    }
}
```

#### 2.2 资源服务器(resource server)

- `Spring Boot 2.0.0.M7`的`autoconfigure`中关于`ResourceServer`的自动配置已被移除，将被移到`Spring Security`项目中，目前的替代方案为使用`https://github.com/spring-projects/spring-security-oauth2-boot.git`，添加如下依赖即可:

```xml
  <dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
  </dependency>
```

#### 2.3 重写`RedisTokenStore`

- 针对`RedisTokenStore`出现的`NoSuchMethodError RedisConnection.set([B[B)V`错误，目前解决方案是重写`RedisTokenStore`第160行`conn.set(accessKey, serializedAccessToken);`修改为`conn.stringCommands().set(accessKey, serializedAccessToken);`

### 3. ElasticSearch

> 由于项目部署在阿里云上，而阿里云推荐使用`REST API`与`ElasticSearch`进行交互，因此项目由原先的`Spring Data Elasticsearch`切换到`Spring Data Jest`作为`ElasticSearch`客户端。

- 依赖

```xml
<dependency>
    <groupId>com.github.vanroy</groupId>
    <artifactId>spring-boot-starter-data-jest</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.2</version>
</dependency>
```

- 连接配置

```yaml
spring:
  data:
    jest:
      uri: http://192.168.1.222:9200
      username: elastic
      password: changeme
```

### 4. Spring Cloud Feign

> 项目使用`Feign`作为`Rest`客户端进行远程调用，`Spring Cloud`版本升级后，`Feign`依赖变更为:

```xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```

### 5. log4j

> `Spring Boot 2.0.0.M7`已将`log4j`依赖移除，而项目中用到的`spring-cloud-stream-binder-kafka`依赖`log4j`，因此需手动添加该依赖:

```xml
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
```

* * *

源码地址：[`http://www.wisely.top/2018/02/28/springboot2-upgrade/`](http://www.wisely.top/2018/02/28/springboot2-upgrade/)