Skip to content

Commit b54ba21

Browse files
committed
Simplified environment variables, Upgraded Gradle
1 parent 12d2145 commit b54ba21

File tree

15 files changed

+178
-34
lines changed

15 files changed

+178
-34
lines changed

README.adoc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,23 @@ To run the application you must set the `DOCKER_HOST_IP` environment variable to
142142
* Docker toolbox/Virtual machine - IP address of the virtual machine
143143
* Docker for Windows/Mac/Linux - IP address of your laptop/desktop
144144

145-
Please do NOT set it to the (unresolvable) hostname of your machine, `localhost` or `127.0.0.1`.
145+
The value of `DOCKER_HOST_IP` must be meaningful to both Java services/tests running on your desktop/laptop and to Docker containers.
146+
Please do NOT set it to the unresolvable hostname of your machine, `localhost` or `127.0.0.1` since the Docker containers will probably not work correctly.
146147

147-
You must also set the AWS environment variables.
148+
=== Verifying that DOCKER_HOST_IP is set correctly
149+
150+
You can verify that `DOCKER_HOST_IP` is set correctly by running this command:
151+
152+
----
153+
docker run -p 8889:8888 -e DOCKER_DIAGNOSTICS_PORT=8889 -e DOCKER_HOST_IP \
154+
--rm eventuateio/eventuateio-docker-networking-diagnostics:0.2.0.RELEASE
155+
----
156+
157+
==== Setting the environment variable in your IDE
158+
159+
If you want to run Java services/tests within your IDE on your desktop/laptop AND the Docker containers are not accessible via `localhost` THEN you will need to set `DOCKER_HOST_IP` within your IDE.
160+
How to do this depends on your operating system and IDE.
161+
For example, I find it convenient to launch my IDE from the command line and after setting this environment variable.
148162

149163

150164
=== Running the application

buildSrc/src/main/groovy/WaitForMySql.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.sql.Connection;
55
import java.sql.DriverManager;
66
import java.sql.SQLException;
7+
import java.util.Optional;
8+
import java.util.function.Supplier;
79

810
public class WaitForMySql extends DefaultTask {
911

@@ -16,11 +18,18 @@ public void waitForMySql() {
1618
waitForConnection();
1719
}
1820

21+
private String getenv(String name, String defaultValue) {
22+
return Optional.ofNullable(System.getenv(name)).orElse(defaultValue);
23+
}
24+
private String getenv(String name, Supplier<String> defaultValue) {
25+
return Optional.ofNullable(System.getenv(name)).orElseGet(defaultValue);
26+
}
27+
1928
private void loadDriver() {
2029
try {
2130
System.out.println("Trying to initialize driver");
2231

23-
String datasourceDriverClassName = System.getenv("SPRING_DATASOURCE_DRIVER_CLASS_NAME");
32+
String datasourceDriverClassName = getenv("SPRING_DATASOURCE_DRIVER_CLASS_NAME", "com.mysql.jdbc.Driver");
2433
Class.forName(datasourceDriverClassName);
2534

2635
System.out.println("Initialization succeed");
@@ -37,9 +46,9 @@ private void waitForConnection() {
3746
try {
3847
System.out.println("Trying to connect...");
3948

40-
String datasourceUrl = System.getenv("SPRING_DATASOURCE_URL");
41-
String datasourceUsername = System.getenv("SPRING_DATASOURCE_USERNAME");
42-
String datasourcePassword = System.getenv("SPRING_DATASOURCE_PASSWORD");
49+
String datasourceUrl = getenv("SPRING_DATASOURCE_URL", () -> String.format("jdbc:mysql://%s/eventuate", getenv("DOCKER_HOST_IP", "localhost")));
50+
String datasourceUsername = getenv("SPRING_DATASOURCE_USERNAME", "mysqluser");
51+
String datasourcePassword = getenv("SPRING_DATASOURCE_PASSWORD", "mysqlpw");
4352

4453
connection = DriverManager.getConnection(datasourceUrl, datasourceUsername, datasourcePassword);
4554

docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
depends_on:
1414
- zookeeper
1515
environment:
16-
- ADVERTISED_HOST_NAME=${DOCKER_HOST_IP}
16+
- ADVERTISED_HOST_NAME=${DOCKER_HOST_IP?DOCKER_HOST_IP must be set}
1717
- KAFKA_HEAP_OPTS=-Xmx192m -Xms192m
1818
- ZOOKEEPER_SERVERS=zookeeper:2181
1919
mysql:
@@ -163,10 +163,10 @@ services:
163163
SPRING_DATASOURCE_DRIVER_CLASS_NAME: com.mysql.jdbc.Driver
164164
EVENTUATELOCAL_KAFKA_BOOTSTRAP_SERVERS: kafka:9092
165165
EVENTUATELOCAL_ZOOKEEPER_CONNECTION_STRING: zookeeper:2181
166-
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
167-
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
166+
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID:-id_key}
167+
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY:-access_key}
168168
AWS_DYNAMODB_ENDPOINT_URL: http://dynamodblocal:8000
169-
AWS_REGION: $AWS_REGION
169+
AWS_REGION: ${AWS_REGION:-us-west-2}
170170
JAVA_OPTS: -Xmx192m
171171
ftgo-api-gateway:
172172
build: ./ftgo-api-gateway
@@ -211,6 +211,6 @@ services:
211211
restart: on-failure
212212
environment:
213213
AWS_DYNAMODB_ENDPOINT_URL: http://dynamodblocal:8000
214-
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
215-
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
216-
AWS_REGION: $AWS_REGION
214+
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID:-id_key}
215+
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY:-access_key}
216+
AWS_REGION: ${AWS_REGION:-us-west-2}

ftgo-common/src/main/resources/application.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,18 @@ logging.level.org.hibernate.SQL=DEBUG
44
logging.level.io.eventuate=DEBUG
55
logging.level.net.chrisrichardson.ftgo=DEBUG
66
logging.level.io.eventuate.tram=TRACE
7+
spring.datasource.url=jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate
8+
spring.datasource.username=mysqluser
9+
spring.datasource.password=mysqlpw
10+
spring.datasource.driver.class.name=com.mysql.jdbc.Driver
11+
spring.data.mongodb.uri=mongodb://${DOCKER_HOST_IP:localhost}/bankingexampledb
12+
13+
eventuatelocal.kafka.bootstrap.servers=${DOCKER_HOST_IP:localhost}:9092
14+
eventuatelocal.cdc.db.user.name=root
15+
eventuatelocal.cdc.db.password=rootpassword
16+
eventuatelocal.zookeeper.connection.string=${DOCKER_HOST_IP:localhost}:2181
17+
18+
aws.access.key_id=id_key
19+
aws.secret.access.key=access_key
20+
aws.dynamodb.endpoint.url=http://${DOCKER_HOST_IP:localhost}:8000
21+
aws.region=us-west-2

ftgo-kitchen-service/src/test/resources/application.properties

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,20 @@ logging.level.org.springframework.cloud.contract=DEBUG
33
logging.level.io.eventuate=DEBUG
44
spring.jpa.generate-ddl=true
55
stubrunner.stream.enabled=false
6-
stubrunner.integration.enabled=false
6+
stubrunner.integration.enabled=false
7+
8+
spring.datasource.url=jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate
9+
spring.datasource.username=mysqluser
10+
spring.datasource.password=mysqlpw
11+
spring.datasource.driver.class.name=com.mysql.jdbc.Driver
12+
spring.data.mongodb.uri=mongodb://${DOCKER_HOST_IP:localhost}/bankingexampledb
13+
14+
eventuatelocal.kafka.bootstrap.servers=${DOCKER_HOST_IP:localhost}:9092
15+
eventuatelocal.cdc.db.user.name=root
16+
eventuatelocal.cdc.db.password=rootpassword
17+
eventuatelocal.zookeeper.connection.string=${DOCKER_HOST_IP:localhost}:2181
18+
19+
aws.access.key_id=id_key
20+
aws.secret.access.key=access_key
21+
aws.dynamodb.endpoint.url=http://${DOCKER_HOST_IP:localhost}:8000
22+
aws.region=us-west-2

ftgo-order-history-service/src/main/java/net/chrisrichardson/ftgo/cqrs/orderhistory/dynamodb/OrderHistoryDynamoDBConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.chrisrichardson.ftgo.cqrs.orderhistory.dynamodb;
22

3+
import com.amazonaws.auth.AWSCredentialsProvider;
4+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
5+
import com.amazonaws.auth.BasicAWSCredentials;
36
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
47
import com.amazonaws.client.builder.AwsClientBuilder;
58
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
@@ -21,6 +24,12 @@ public class OrderHistoryDynamoDBConfiguration {
2124
@Value("${aws.region}")
2225
private String awsRegion;
2326

27+
@Value("${aws.access.key_id:null}")
28+
private String accessKey;
29+
30+
@Value("${aws.secret.access.key:null}")
31+
private String secretKey;
32+
2433
@Bean
2534
public AmazonDynamoDB amazonDynamoDB() {
2635

@@ -29,12 +38,13 @@ public AmazonDynamoDB amazonDynamoDB() {
2938
.standard()
3039
.withEndpointConfiguration(
3140
new AwsClientBuilder.EndpointConfiguration(awsDynamodbEndpointUrl, awsRegion))
41+
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
3242
.build();
3343
} else {
3444
return AmazonDynamoDBClientBuilder
3545
.standard()
3646
.withRegion(awsRegion)
37-
.withCredentials(new EnvironmentVariableCredentialsProvider())
47+
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
3848
.build();
3949
}
4050
}

ftgo-order-history-service/src/test/resources/application.properties

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,20 @@ logging.level.org.springframework.cloud.contract=DEBUG
33
logging.level.io.eventuate=DEBUG
44
spring.jpa.generate-ddl=true
55
stubrunner.stream.enabled=false
6-
stubrunner.integration.enabled=false
6+
stubrunner.integration.enabled=false
7+
8+
spring.datasource.url=jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate
9+
spring.datasource.username=mysqluser
10+
spring.datasource.password=mysqlpw
11+
spring.datasource.driver.class.name=com.mysql.jdbc.Driver
12+
spring.data.mongodb.uri=mongodb://${DOCKER_HOST_IP:localhost}/bankingexampledb
13+
14+
eventuatelocal.kafka.bootstrap.servers=${DOCKER_HOST_IP:localhost}:9092
15+
eventuatelocal.cdc.db.user.name=root
16+
eventuatelocal.cdc.db.password=rootpassword
17+
eventuatelocal.zookeeper.connection.string=${DOCKER_HOST_IP:localhost}:2181
18+
19+
aws.access.key_id=id_key
20+
aws.secret.access.key=access_key
21+
aws.dynamodb.endpoint.url=http://${DOCKER_HOST_IP:localhost}:8000
22+
aws.region=us-west-2

ftgo-order-service/src/component-test/java/net/chrisrichardson/ftgo/orderservice/cucumber/OrderServiceComponentTestStepDefinitions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3535
import org.springframework.boot.autoconfigure.domain.EntityScan;
36+
import org.springframework.boot.test.context.SpringBootTest;
3637
import org.springframework.context.annotation.Bean;
3738
import org.springframework.context.annotation.Configuration;
3839
import org.springframework.context.annotation.Import;
@@ -51,9 +52,9 @@
5152
import static org.junit.Assert.fail;
5253

5354

54-
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
55-
@ContextConfiguration(classes = OrderServiceComponentTestStepDefinitions.TestConfiguration.class)
56-
public class OrderServiceComponentTestStepDefinitions /* extends OrderServiceComponentTestSpringContextConfiguration */ {
55+
@SpringBootTest(classes = OrderServiceComponentTestStepDefinitions.TestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
56+
@ContextConfiguration
57+
public class OrderServiceComponentTestStepDefinitions {
5758

5859

5960

ftgo-order-service/src/component-test/resources/application.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ spring.jpa.generate-ddl=true
1010

1111
stubrunner.stream.enabled=false
1212
stubrunner.integration.enabled=false
13+
spring.datasource.url=jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate
14+
spring.datasource.username=mysqluser
15+
spring.datasource.password=mysqlpw
16+
spring.datasource.driver.class.name=com.mysql.jdbc.Driver
17+
spring.data.mongodb.uri=mongodb://${DOCKER_HOST_IP:localhost}/bankingexampledb
18+
19+
eventuatelocal.kafka.bootstrap.servers=${DOCKER_HOST_IP:localhost}:9092
20+
eventuatelocal.cdc.db.user.name=root
21+
eventuatelocal.cdc.db.password=rootpassword
22+
eventuatelocal.zookeeper.connection.string=${DOCKER_HOST_IP:localhost}:2181
23+
24+
aws.access.key_id=id_key
25+
aws.secret.access.key=access_key
26+
aws.dynamodb.endpoint.url=http://${DOCKER_HOST_IP:localhost}:8000
27+
aws.region=us-west-2

ftgo-order-service/src/integration-test/resources/application.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ spring.jpa.generate-ddl=true
1010

1111
stubrunner.stream.enabled=false
1212
stubrunner.integration.enabled=false
13+
spring.datasource.url=jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate
14+
spring.datasource.username=mysqluser
15+
spring.datasource.password=mysqlpw
16+
spring.datasource.driver.class.name=com.mysql.jdbc.Driver
17+
spring.data.mongodb.uri=mongodb://${DOCKER_HOST_IP:localhost}/bankingexampledb
18+
19+
eventuatelocal.kafka.bootstrap.servers=${DOCKER_HOST_IP:localhost}:9092
20+
eventuatelocal.cdc.db.user.name=root
21+
eventuatelocal.cdc.db.password=rootpassword
22+
eventuatelocal.zookeeper.connection.string=${DOCKER_HOST_IP:localhost}:2181
23+
24+
aws.access.key_id=id_key
25+
aws.secret.access.key=access_key
26+
aws.dynamodb.endpoint.url=http://${DOCKER_HOST_IP:localhost}:8000
27+
aws.region=us-west-2

0 commit comments

Comments
 (0)