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

[SpringBoot 2.3] Upgrade to Spring Data Elasticsearch 4.0 and Elasticsearch 7.6 #11683

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions generators/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ function cleanupOldServerFiles(generator, javaDir, testDir, mainResourceDir, tes
generator.removeFile(`${javaDir}config/cassandra/CassandraConfiguration.java`);
generator.removeFile(`${testDir}config/CassandraConfigurationIT.java`);
}
if (generator.searchEngine === 'elasticsearch') {
generator.removeFile(`${testDir}config/ElasticsearchTestConfiguration.java`);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion generators/generator-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const DOCKER_HAZELCAST_MANAGEMENT_CENTER = 'hazelcast/management-center:3.12.9';
const DOCKER_MEMCACHED = 'memcached:1.6.6-alpine';
const DOCKER_REDIS = 'redis:5.0.9'; // waiting for https://github.com/jhipster/generator-jhipster/issues/11678
const DOCKER_KEYCLOAK = 'jboss/keycloak:10.0.0'; // The version should match the attribute 'keycloakVersion' from /docker-compose/templates/realm-config/jhipster-realm.json.ejs and /server/templates/src/main/docker/config/realm-config/jhipster-realm.json.ejs
const DOCKER_ELASTICSEARCH = 'docker.elastic.co/elasticsearch/elasticsearch:6.8.8'; // The version should be coherent with the one from spring-data-elasticsearch project
const DOCKER_ELASTICSEARCH = 'docker.elastic.co/elasticsearch/elasticsearch:7.6.2'; // The version should be coherent with the one from spring-data-elasticsearch project
const DOCKER_KAFKA = `confluentinc/cp-kafka:${KAFKA_VERSION}`;
const DOCKER_ZOOKEEPER = `confluentinc/cp-zookeeper:${KAFKA_VERSION}`;
const DOCKER_SONAR = 'sonarqube:8.3.1-community';
Expand Down
13 changes: 6 additions & 7 deletions generators/heroku/templates/application-heroku.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ spring:
hikari:
maximumPoolSize: 8
<%_ } _%>
<%_ if (prodDatabaseType === 'mongodb' || searchEngine === 'elasticsearch') { _%>
<%_ if (prodDatabaseType === 'mongodb') { _%>
data:
<%_ if (prodDatabaseType === 'mongodb') { _%>
mongodb:
uri: ${MONGODB_URI}?authMode=scram-sha1
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
jest:
uri: ${BONSAI_URL}
<%_ } _%>
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
elasticsearch:
rest:
uris: ${BONSAI_URL}
<%_ } _%>
2 changes: 0 additions & 2 deletions generators/kubernetes-knative/templates/service.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ spec:
value: <%= app.baseName.toLowerCase() %>
<%_ } _%>
<%_ if (app.searchEngine === 'elasticsearch') { _%>
- name: SPRING_DATA_JEST_URI
value: http://<%= app.baseName.toLowerCase() %>-elasticsearch.<%= kubernetesNamespace %>.svc.cluster.local:9200
- name: SPRING_ELASTICSEARCH_REST_URIS
value: http://<%= app.baseName.toLowerCase() %>-elasticsearch.<%= kubernetesNamespace %>.svc.cluster.local:9200
<%_ } _%>
Expand Down
2 changes: 0 additions & 2 deletions generators/kubernetes/templates/deployment.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ spec:
value: <%= app.baseName.toLowerCase() %>
<%_ } _%>
<%_ if (app.searchEngine === 'elasticsearch') { _%>
- name: SPRING_DATA_JEST_URI
value: http://<%= app.baseName.toLowerCase() %>-elasticsearch.<%= kubernetesNamespace %>.svc.cluster.local:9200
- name: SPRING_ELASTICSEARCH_REST_URIS
value: http://<%= app.baseName.toLowerCase() %>-elasticsearch.<%= kubernetesNamespace %>.svc.cluster.local:9200
<%_ } _%>
Expand Down
2 changes: 0 additions & 2 deletions generators/openshift/templates/deployment.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ objects:
value: ${APPLICATION_NAME}
<%_ } _%>
<%_ if (app.searchEngine === 'elasticsearch') { _%>
- name: SPRING_DATA_JEST_URI
value: http://${APPLICATION_NAME}-elasticsearch:9200
- name: SPRING_ELASTICSEARCH_REST_URIS
value: http://${APPLICATION_NAME}-elasticsearch:9200
<%_ } _%>
Expand Down
10 changes: 0 additions & 10 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,16 +1107,6 @@ const serverFiles = {
},
],
},
{
condition: generator => generator.searchEngine === 'elasticsearch',
path: SERVER_TEST_SRC_DIR,
templates: [
{
file: 'package/config/ElasticsearchTestConfiguration.java',
renameTo: generator => `${generator.testDir}config/ElasticsearchTestConfiguration.java`,
},
],
},
],
serverJavaDomain: [
{
Expand Down
9 changes: 0 additions & 9 deletions generators/server/templates/build.gradle.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,6 @@ dependencies {
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
implementation "org.springframework.boot:spring-boot-starter-data-elasticsearch"
<%_ if (!reactive) { _%>
// Spring Data Jest dependencies for Elasticsearch
implementation ("com.github.vanroy:spring-boot-starter-data-jest") {
exclude module: "commons-logging"
}
<%_ } _%>
// log4j2-mock needed to create embedded elasticsearch instance with SLF4J
runtimeOnly "de.dentrassi.elasticsearch:log4j2-mock:${log4j2_mock_version}"
// end of Spring Data Jest dependencies
<%_ } _%>
<%_ if (['mongodb', 'cassandra', 'couchbase'].includes(databaseType)) { _%>
implementation "org.springframework.boot:spring-boot-starter-data-<%= databaseType %><% if (reactive) { %>-reactive<% } %>"
Expand Down
21 changes: 0 additions & 21 deletions generators/server/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -674,27 +674,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<%_ if (!reactive) { _%>
<!-- Spring Data Jest dependencies for Elasticsearch -->
<dependency>
<groupId>com.github.vanroy</groupId>
<artifactId>spring-boot-starter-data-jest</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<%_ } _%>
<!-- log4j2-mock needed to create embedded elasticsearch instance with SLF4J -->
<dependency>
<groupId>de.dentrassi.elasticsearch</groupId>
<artifactId>log4j2-mock</artifactId>
<version>${log4j2-mock.version}</version>
<scope>runtime</scope>
</dependency>
<!-- end of Spring Data Jest dependencies -->
<%_ } _%>
<%_ if (['mongodb', 'cassandra', 'couchbase'].includes(databaseType)) { _%>
<dependency>
Expand Down
1 change: 0 additions & 1 deletion generators/server/templates/src/main/docker/app.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ services:
- JHIPSTER_SLEEP=30 # gives time for other services to boot before the application
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
- SPRING_DATA_JEST_URI=http://<%= baseName.toLowerCase() %>-elasticsearch:9200
- SPRING_ELASTICSEARCH_REST_URIS=http://<%= baseName.toLowerCase() %>-elasticsearch:9200
<%_ } _%>
<%_ if (messageBroker === 'kafka') { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,121 +18,119 @@
-%>
package <%= packageName %>.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
<%_ if (!reactive) { _%>
import com.github.vanroy.springdata.jest.JestElasticsearchTemplate;
import com.github.vanroy.springdata.jest.mapper.DefaultJestResultsMapper;
import io.searchbox.client.JestClient;
<%_ } _%>
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
<%_ if (!reactive) { _%>
import org.springframework.context.annotation.Primary;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
<%_ } _%>
import org.springframework.data.elasticsearch.core.EntityMapper;
<%_ if (!reactive) { _%>
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
<%_ } _%>
<%_ if (databaseType === 'mongodb' || !reactive) { _%>
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
<%_ } _%>
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
<%_ if (databaseType === 'mongodb') { _%>
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentProperty;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity;
<%_ } _%>
import org.springframework.data.mapping.MappingException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;

@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfiguration {

private ObjectMapper mapper;

public ElasticsearchConfiguration(ObjectMapper mapper) {
this.mapper = mapper;
}
public class ElasticsearchConfiguration extends ElasticsearchConfigurationSupport {

@Bean
public EntityMapper getEntityMapper() {
return new CustomEntityMapper(mapper);
}
<%_ if (databaseType === 'mongodb') { _%>

@Bean
SimpleElasticsearchMappingContext mappingContext() {
return new CustomElasticsearchMappingContext();
@Override
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(
new ZonedDateTimeWritingConverter(),
new ZonedDateTimeReadingConverter(),
new InstantWritingConverter(),
new InstantReadingConverter(),
new LocalDateWritingConverter(),
new LocalDateReadingConverter()
)
);
}
<%_ } _%>
<%_ if (!reactive) { _%>

@Bean
@Primary
public ElasticsearchOperations elasticsearchTemplate(JestClient jestClient,
ElasticsearchConverter elasticsearchConverter,
SimpleElasticsearchMappingContext mappingContext,
EntityMapper entityMapper) {
return new JestElasticsearchTemplate(
jestClient,
elasticsearchConverter,
new DefaultJestResultsMapper(mappingContext, entityMapper));
@WritingConverter
static class ZonedDateTimeWritingConverter implements Converter<ZonedDateTime, String> {
@Override
public String convert(ZonedDateTime source) {
if (source == null) {
return null;
}
return source.toInstant().toString();
}
}
<%_ } _%>

public class CustomEntityMapper implements EntityMapper {

private ObjectMapper objectMapper;

public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
@ReadingConverter
static class ZonedDateTimeReadingConverter implements Converter<String, ZonedDateTime> {
@Override
public ZonedDateTime convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source).atZone(ZoneId.systemDefault());
}
}

@WritingConverter
static class InstantWritingConverter implements Converter<Instant, String> {
@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
public String convert(Instant source) {
if (source == null) {
return null;
}
return source.toString();
}
}

@ReadingConverter
static class InstantReadingConverter implements Converter<String, Instant> {
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
public Instant convert(String source) {
if (source == null) {
return null;
}
return Instant.parse(source);
}
}

@WritingConverter
static class LocalDateWritingConverter implements Converter<LocalDate, String> {
@Override
public Map<String, Object> mapObject(Object source) {
try {
return objectMapper.readValue(mapToString(source), HashMap.class);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
public String convert(LocalDate source) {
if (source == null) {
return null;
}
return source.toString();
}
}

@ReadingConverter
static class LocalDateReadingConverter implements Converter<String, LocalDate> {
@Override
public <T> T readObject(Map<String, Object> source, Class<T> targetType) {
try {
return mapToObject(mapToString(source), targetType);
} catch (IOException e) {
throw new MappingException(e.getMessage(), e);
public LocalDate convert(String source) {
if (source == null) {
return null;
}
return LocalDate.parse(source);
}
}
<%_ if (databaseType === 'mongodb') { _%>

@Bean
SimpleElasticsearchMappingContext mappingContext() {
return new CustomElasticsearchMappingContext();
}
<%_ } _%>
}
<%_ if (databaseType === 'mongodb') { _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ spring:
local-datacenter: datacenter1
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
elasticsearch:
properties:
path:
home: <%= BUILD_DIR %>elasticsearch
elasticsearch:
rest:
uris: http://localhost:9200
<%_ } _%>
<%_ if (databaseType === 'couchbase') { _%>
couchbase:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ spring:
<%_ } _%>
show-sql: false
<%_ } _%>
<%_ if (databaseType === 'mongodb' || databaseType === 'cassandra' || searchEngine === 'elasticsearch') { _%>
<%_ if (databaseType === 'mongodb' || databaseType === 'cassandra' || (searchEngine === 'elasticsearch' && reactive)) { _%>
data:
<%_ } _%>
<%_ if (databaseType === 'mongodb') { _%>
Expand All @@ -139,18 +139,15 @@ spring:
local-datacenter: datacenter1
<%_ } _%>
<%_ if (searchEngine === 'elasticsearch') { _%>
<%_ if (!reactive) { _%>
jest:
uri: http://localhost:9200
# see https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html#boot-features-connecting-to-elasticsearch-jest
elasticsearch:
rest:
uris: http://localhost:9200
<%_ } else { _%>
<%_ if (reactive) { _%>
elasticsearch:
client:
reactive:
endpoints: localhost:9200
<%_ } else { _%>
elasticsearch:
rest:
uris: http://localhost:9200
<%_ } _%>
<%_ } _%>
<%_ if (databaseType === 'couchbase') { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ management:
enabled: true

spring:
<%_ if (searchEngine === 'elasticsearch' && !reactive) { _%>
autoconfigure:
exclude: <% if (databaseType === 'sql' && messageBroker === 'kafka') { %>org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration,<% } %>org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration
<%_ } else if (databaseType === 'sql' && messageBroker === 'kafka') { _%>
<%_ if (databaseType === 'sql' && messageBroker === 'kafka') { _%>
autoconfigure:
exclude: org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration
<%_ } _%>
Expand Down
Loading