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

fix: docker image build and run with app.yml #56

Merged
merged 1 commit into from
May 26, 2020
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
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 %>