Spring Cloud 程序员成长记录Spring Cloud 核心技术

spring cloud 版本升级 Daloston--->

2019-03-04  本文已影响3人  SilentBillows

加入下边依赖,帮助检测迁移过程中存在的问题
在Finchley 中,spring boot 升级到2.x ,其中配置文件中的参数需要全部小写,或者用 - 隔开.


        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-properties-migrator</artifactId>

<scope>runtime</scope>

</dependency>

1.父级包的导入变更


    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.6.RELEASE</version>

        <relativePath/>

    </parent>

      <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Finchley.SR2</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

2.eureka server

1.1 增强安全机制,


@EnableWebSecurity

class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().ignoringAntMatchers("/eureka/**");

        super.configure(http);

    }

}

1.2 依赖变更

Dalostion版本


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka-server</artifactId>

        </dependency>

Finchley版本


        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

        </dependency>

3.eureka client

依赖升级

Daloston


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

        </dependency>

FinchleySR2


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

4.zuul

1.依赖升级

Daloston


        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-zuul</artifactId>

        </dependency>

FinchleySR2


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>

        </dependency>

  1. 关于 ZuulFallbackProvider ----> FallbackProvider

finchleySR2版本中, 对zuulFallbackeProvider进行了调整,


FinchleySR2版本: 能够获得更多的熔断的错误信息和路由信息

@Override

    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {

        return new ClientHttpResponse() {

            @Override

            public HttpStatus getStatusCode() throws IOException {

                return HttpStatus.OK;

            }

            @Override

            public int getRawStatusCode() throws IOException {

                return HttpStatus.OK.value();

            }

            @Override

            public String getStatusText() throws IOException {

                return HttpStatus.OK.getReasonPhrase();

            }

            @Override

            public void close() {

            }

            @Override

            public InputStream getBody() throws IOException {

                JSONObject body = new JSONObject();

                body.put("code", ApiConstant.RESPONSE_SYSTEM_BUSY_ERROR_CODE);

                body.put("msg", ApiConstant.RESPONSE_SYSTEM_BUSY_ERROR_MSG);

                body.put("data", "");

                return new ByteArrayInputStream(body.toJSONString().getBytes("UTF-8"));

            }

            @Override

            public HttpHeaders getHeaders() {

                HttpHeaders headers = new HttpHeaders();

                //和body中的内容编码一致,否则容易乱码

                headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

                return headers;

            }

        };

    }

Daloston版本:

    @Override

    public ClientHttpResponse fallbackResponse() {

        return new ClientHttpResponse() {

            @Override

            public HttpStatus getStatusCode() throws IOException {

                return HttpStatus.OK;

            }

            @Override

            public int getRawStatusCode() throws IOException {

                return HttpStatus.OK.value();

            }

            @Override

            public String getStatusText() throws IOException {

                return HttpStatus.OK.getReasonPhrase();

            }

            @Override

            public void close() {

            }

            @Override

            public InputStream getBody() throws IOException {

                JSONObject body = new JSONObject();

                body.put("code", ApiConstant.RESPONSE_SYSTEM_BUSY_ERROR_CODE);

                body.put("msg", ApiConstant.RESPONSE_SYSTEM_BUSY_ERROR_MSG);

                body.put("data", "");

                return new ByteArrayInputStream(body.toJSONString().getBytes("UTF-8"));

            }

            @Override

            public HttpHeaders getHeaders() {

                HttpHeaders headers = new HttpHeaders();

                //和body中的内容编码一致,否则容易乱码

                headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

                return headers;

            }

        };

    }



3 zuul 关于ribbon的比较重要的配置, 主要在于文件上传


ribbon.ReadTimeout=60000

ribbon.ConnectTimeout=3000

5.Hystrix

1 依赖升级

Daloston


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-hystrix</artifactId>

        </dependency>

FinchleySR2


  <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

        </dependency>

6.Ribbon

1.依赖升级

Daloston


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-ribbon</artifactId>

        </dependency>

FinchleySR2


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>

        </dependency>

7.Feign

1.依赖

Daloston


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

FinchleySR2


<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

  1. 配置

feign.httpclient.enabled=true

feign.okhttp.enabled=true 

8.Spring boot admin

其他问题

  1. 
    

该语句的过时的替代方式

management.security.enabled=false

在spring boot 2.x 中的替代方案为

management.endpoints.web.exposure.exclude=*


2. tomcat 中类的变更 ,在需要同事兼容http和https协议,同时开发多个端口的时候需要注意

EmbeddedServletContainerFactory----> TomcatServletWebServerFactory

3. spirng boot 2.x 不在兼容支持日志  org.apache.log4j.Logger

### mybatis 的支持

无变化

### spring boot redis的支持

1. 默认的链接客户端为lettuce

2. 配置链接的变更

```java

#主要是pool配置的链接参数名称变更

spring.redis.jedis.pool.max-active=300

spring.redis.jedis.pool.max-wait=10000

spring.redis.jedis.pool.max-idle=100

spring.redis.jedis.pool.min-idle=10

3.和spring cache

9.elastic-job

支持2.x

1.包的冲突问题, 冲突主要发生在zookeeper的版本上边


<dependencies>



        <dependency>

            <groupId>com.dmap</groupId>

            <artifactId>schedule-job</artifactId>

            <version>3.0.0-SNAPSHOT</version>

        </dependency>

        <dependency>

            <groupId>org.apache.zookeeper</groupId>

            <artifactId>zookeeper</artifactId>

        </dependency>

        <dependency>

            <groupId>com.dangdang</groupId>

            <artifactId>elastic-job-lite-spring</artifactId>

            <version>2.1.5</version>

        </dependency>

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-framework</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-recipes</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.curator</groupId>

            <artifactId>curator-client</artifactId>

        </dependency>

        <dependency>

            <groupId>com.google.guava</groupId>

            <artifactId>guava</artifactId>

            <version>18.0</version>

        </dependency>

    </dependencies>

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Finchley.SR2</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>org.apache.zookeeper</groupId>

                <artifactId>zookeeper</artifactId>

                <version>3.4.6</version>

            </dependency>

            <dependency>

                <groupId>org.apache.curator</groupId>

                <artifactId>curator-recipes</artifactId>

                <version>2.10.0</version>

            </dependency>

            <dependency>

                <groupId>org.apache.curator</groupId>

                <artifactId>curator-framework</artifactId>

                <version>2.10.0</version>

            </dependency>

            <dependency>

                <groupId>org.apache.curator</groupId>

                <artifactId>curator-client</artifactId>

                <version>2.10.0</version>

            </dependency>

        </dependencies>

    </dependencyManagement>

本篇文章将持续更新,不断完善对于spring cloud 各组件升级的地方,有问题可以发送邮件lt1040773823@163.com一起探讨

上一篇下一篇

猜你喜欢

热点阅读