美文网首页
springboot2.2.0升级过程

springboot2.2.0升级过程

作者: 谁在烽烟彼岸 | 来源:发表于2019-10-24 18:03 被阅读0次

    项目升级,springboot由1.5升级到2.2

    一、SpringBootRedis

    在springboot2.0之后, springbootredis默认使用Lettuce, springbootredis1.0使用jedis
    详情可见SpringBoot2.0Redis配置

    netty

    在升级之后redis和elastic都使用了netty,区别于之前的1.5,
    1.注意版本的统一,如有问题,可添加该属性设置

    System.setProperty("es.set.netty.runtime.available.processors", "false");
    

    2.在关闭项目的时候,netty关闭将由单独的线程来完成,将晚于主线程关闭

    二、SpringBootElasticsearch

    在设置时要采用netty4的方式,且要注意Elasticseearch服务器是否包含netty4的插件

       public TransportClient client() {
            TransportClient client = null;
            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .put("transport.type", "netty4")
                    .put("http.type", "netty4")
                    .put("http.enabled", "true")
                    .build();
            try {
                   client = new PreBuiltTransportClient(settings)
                            .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
                
                logger.info("The es client create successful" + "host = " + host + ",port = " + port);
            } catch (UnknownHostException e) {
                logger.error("The es client create failure");
                e.printStackTrace();
            }
    
            return client;
        }
    

    三、SpringBootMongodb

    官方文档

    api的变化

    1.save(List list) => saveAll(List list)
    2.delete(String id) => deleteById(String id)
    3.findOne(String id) => findById(String id) 返回结果由<T>转为Optional<T>

    提示自动创建index将不再被推荐使用,请大家多注意

        Automatic index creation will be turned OFF by default with the release of 3.x. We recommend index creation to happen either out of band or as part of the application startup using IndexOperations.
    
    [WARN ] o.s.d.m.c.index.MongoPersistentEntityIndexCreator - Automatic index creation will be disabled by default as of Spring Data MongoDB 3.x.
        Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
        However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.
    
        > -----------------------------------------------------------------------------------------
        > @EventListener(ApplicationReadyEvent.class)
        > public void initIndicesAfterStartup() {
        >
        >     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
        >
        >     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
        >     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
        > }
        > -----------------------------------------------------------------------------------------
    

    链接

    SimpleMongoDbFactory类已经失效,被SimpleMongoClientDbFactory。

     @Bean
        public MongoDbFactory secondaryFactory() throws Exception {
            ConnectionString connectionString = new ConnectionString("mongodb://name:password@uri/database2?authSource=admin&authMechanism=SCRAM-SHA-1");
            return new SimpleMongoClientDbFactory(connectionString);
        }
    

    对于事务的支持

    @Service
    public class StateService {
    
        @Transactional
        Mono<UpdateResult> someBusinessFunction(Step step) {                                  
    
            return template.insert(step)
                .then(process(step))
                .then(template.update(Step.class).apply(Update.set("state", …));
        };
    })
    

    相关文章

      网友评论

          本文标题:springboot2.2.0升级过程

          本文链接:https://www.haomeiwen.com/subject/tcydvctx.html