Skip to content

Commit

Permalink
fix: docker image build and run with app.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelbrayelle committed May 26, 2020
1 parent 6ca77a2 commit 437469b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Generated consumers should not be explicitly used in other classes as each of th

🚀 [AKHQ (previously known as KafkaHQ)](https://github.com/tchiotludo/akhq) can be used following those steps in the root directory:

1. Run `docker-compose -f src/main/docker/kafka.yml up -d` to launch the ZooKeeper and Kafka services along with the AKHQ one
1. Run `docker-compose -f src/main/docker/kafka.yml up -d` to launch the ZooKeeper and Kafka services with AKHQ
1. Go to [http://localhost:8081](http://localhost:8081)
1. Start your application with `./mvnw` to manage your topics and more!

Expand Down
58 changes: 43 additions & 15 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,10 @@ module.exports = class extends BaseGenerator {
}

writing() {
// function to use directly template
this.template = function(source, destination) {
this.fs.copyTpl(this.templatePath(source), this.destinationPath(destination), this);
};

// function generate kafka application properties
this.generateKafkaProperties = function() {
this.generateKafkaProperties = function(enabled) {
this.enabled = enabled;

let generatedKafkaProperties = '';
jhipsterUtils.renderContent(this.templatePath('src/main/resources/application-kafka.yml.ejs'), this, this, {}, res => {
generatedKafkaProperties = res;
Expand Down Expand Up @@ -296,11 +293,16 @@ module.exports = class extends BaseGenerator {
this.removeFile(`${testDir}web/rest/${this.upperFirstCamelCase(this.baseName)}KafkaResourceIT.java`);

if (this.components.includes('consumer') && this.entities.length > 0) {
this.template('src/main/java/package/service/kafka/GenericConsumer.java.ejs', `${javaDir}service/kafka/GenericConsumer.java`);
this.template(
'src/main/java/package/service/kafka/GenericConsumer.java.ejs',
`${javaDir}service/kafka/GenericConsumer.java`,
null,
null
);
}

if (this.components.includes('consumer') || this.components.includes('producer')) {
this.template('src/main/java/package/config/KafkaProperties.java.ejs', `${javaDir}config/KafkaProperties.java`);
this.template('src/main/java/package/config/KafkaProperties.java.ejs', `${javaDir}config/KafkaProperties.java`, null, null);
}

this.entities.forEach(entity => {
Expand All @@ -310,11 +312,15 @@ module.exports = class extends BaseGenerator {
if (this.components.includes('consumer')) {
this.template(
'src/main/java/package/service/kafka/consumer/EntityConsumer.java.ejs',
`${javaDir}service/kafka/consumer/${entity}Consumer.java`
`${javaDir}service/kafka/consumer/${entity}Consumer.java`,
null,
null
);
this.template(
'src/main/java/package/service/kafka/deserializer/EntityDeserializer.java.ejs',
`${javaDir}service/kafka/deserializer/${entity}Deserializer.java`
`${javaDir}service/kafka/deserializer/${entity}Deserializer.java`,
null,
null
);
}
});
Expand All @@ -326,23 +332,45 @@ module.exports = class extends BaseGenerator {
if (this.components.includes('producer')) {
this.template(
'src/main/java/package/service/kafka/producer/EntityProducer.java.ejs',
`${javaDir}service/kafka/producer/${entity}Producer.java`
`${javaDir}service/kafka/producer/${entity}Producer.java`,
null,
null
);
this.template(
'src/main/java/package/service/kafka/serializer/EntitySerializer.java.ejs',
`${javaDir}service/kafka/serializer/${entity}Serializer.java`
`${javaDir}service/kafka/serializer/${entity}Serializer.java`,
null,
null
);
}
});

const kafkaProperties = this.generateKafkaProperties();
const kafkaProperties = this.generateKafkaProperties(true);
this.log(`kafkaProperties=\n\n${kafkaProperties}\n\n`);

const kafkaTestProperties = this.generateKafkaProperties(false);
this.log(`kafkaTestProperties=\n\n${kafkaTestProperties}\n\n`);

const kafkaBlockPattern = /\n+kafka:\n(\s.+\n+)+/g;
this.replaceContent(`${resourceDir}config/application.yml`, kafkaBlockPattern, kafkaProperties);
this.replaceContent(`${testResourceDir}config/application.yml`, kafkaBlockPattern, kafkaProperties);
this.replaceContent(`${testResourceDir}config/application.yml`, kafkaBlockPattern, kafkaTestProperties);

this.template('src/main/docker/kafka.yml.ejs', `${jhipsterConstants.MAIN_DIR}docker/kafka.yml`, this, null, null);

// Related to: https://github.com/jhipster/generator-jhipster/issues/11846
this.overrideMainGeneratorAppYml();
}

overrideMainGeneratorAppYml() {
const appYmlPath = `${jhipsterConstants.MAIN_DIR}docker/app.yml`;

const kafkaBootstrapServersPattern = /^\s.*KAFKA_BOOTSTRAPSERVERS.*$/gm;
const kafkaBootstrapServers = ' - KAFKA_BOOTSTRAP_SERVERS=kafka:29092';
this.replaceContent(appYmlPath, kafkaBootstrapServersPattern, kafkaBootstrapServers);

this.template('src/main/docker/kafka.yml.ejs', `${jhipsterConstants.MAIN_DIR}docker/kafka.yml`);
const kafkaAdvertisedListenersPattern = /^\s.*KAFKA_ADVERTISED_LISTENERS.*$/gm;
const kafkaAdvertisedListeners = ' - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092';
this.replaceContent(appYmlPath, kafkaAdvertisedListenersPattern, kafkaAdvertisedListeners);
}

install() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<%# Important : leave the next 2 lines so that this.replaceContent will work %>


kafka:
bootstrap.servers: localhost:9092
bootstrap.servers: ${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}
<%_ if (pollingTimeout) { _%>
polling.timeout: <%= pollingTimeout %>
<%_ } _%>
Expand All @@ -18,25 +17,23 @@ kafka:
<%_ for (entity of entities) { _%>
<%= _.camelCase(entity) _%>:
name: queuing.<%= snakeCaseBaseName %>.<%= _.snakeCase(entity) %>
enabled: true
enabled: <%= enabled %>
'[key.deserializer]': org.apache.kafka.common.serialization.StringDeserializer
'[value.deserializer]': <%= packageName %>.service.kafka.deserializer.<%= entity %>Deserializer
'[group.id]': <%= dasherizedBaseName %>
<%_ if (autoOffsetResetPolicy) { _%>
<%_ if (autoOffsetResetPolicy) { _%>
'[auto.offset.reset]': <%= autoOffsetResetPolicy %>
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ } _%>
<%_ if (components.includes('producer')) { _%>
producer:
<%_ for (entity of entities) { _%>
<%= _.camelCase(entity) _%>:
name: queuing.<%= snakeCaseBaseName %>.<%= _.snakeCase(entity) %>
enabled: true
enabled: <%= enabled %>
'[key.serializer]': org.apache.kafka.common.serialization.StringSerializer
'[value.serializer]': <%= packageName %>.service.kafka.serializer.<%= entity %>Serializer
<%_ } _%>
<%_ } _%>


<%# Important : leave the previous 2 lines so that this.replaceContent will work %>

0 comments on commit 437469b

Please sign in to comment.