springboot2.2.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将不再被使用,请大家多注意

[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);
    }

对于事务的支持

@Transactional(readOnly = true)建议ReactiveMongoTransactionManager还启动将添加ClientSession到传出请求的事务。

@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", …));
    };
})

推荐阅读更多精彩内容