Skip to content

Commit 683b8a1

Browse files
committed
Salary Microservices added. with docker build and config server
1 parent 10dd0ac commit 683b8a1

File tree

14 files changed

+1058
-11
lines changed

14 files changed

+1058
-11
lines changed

person-service/src/main/java/com/cevher/ms/person/config/DepartmentDiscoveryClient.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@
2626
@Slf4j
2727
public class DepartmentDiscoveryClient {
2828

29-
private final DiscoveryClient discoveryClient;
29+
// private final DiscoveryClient discoveryClient;
30+
private final RestTemplate restTemplate;
3031

3132
public DepartmentVM getDepartment(String departmentId) {
32-
RestTemplate restTemplate = new RestTemplate();
33-
List<ServiceInstance> instances = discoveryClient.getInstances("department-service");
34-
35-
if (instances.size() == 0) return null;
36-
String serviceUri = String.format("%s/v1/departments/%s", instances.get(0).getUri().toString(), departmentId);
37-
log.info("!!!! SERVICE URI: " + serviceUri);
38-
39-
val restExchange = restTemplate.exchange(
33+
// RestTemplate restTemplate = new RestTemplate();
34+
// List<ServiceInstance> instances = discoveryClient.getInstances("department-service");
35+
//
36+
// if (instances.size() == 0) return null;
37+
// String serviceUri = String.format("%s/v1/departments/%s", instances.get(0).getUri().toString(), departmentId);
38+
// log.info("!!!! SERVICE URI: " + serviceUri);
39+
40+
//for Load Balancing
41+
val serviceUri = "http://department-service/v1/departments/{department-id}";
42+
return restTemplate.exchange(
4043
serviceUri,
4144
HttpMethod.GET,
4245
null,
4346
DepartmentVM.class,
44-
departmentId);
47+
departmentId)
48+
.getBody();
4549

46-
return restExchange.getBody();
4750
}
4851
}

salary-service/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.net.*;
18+
import java.io.*;
19+
import java.nio.channels.*;
20+
import java.util.Properties;
21+
22+
public class MavenWrapperDownloader {
23+
24+
private static final String WRAPPER_VERSION = "0.5.6";
25+
/**
26+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
27+
*/
28+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
30+
31+
/**
32+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
33+
* use instead of the default one.
34+
*/
35+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
36+
".mvn/wrapper/maven-wrapper.properties";
37+
38+
/**
39+
* Path where the maven-wrapper.jar will be saved to.
40+
*/
41+
private static final String MAVEN_WRAPPER_JAR_PATH =
42+
".mvn/wrapper/maven-wrapper.jar";
43+
44+
/**
45+
* Name of the property which should be used to override the default download url for the wrapper.
46+
*/
47+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
48+
49+
public static void main(String args[]) {
50+
System.out.println("- Downloader started");
51+
File baseDirectory = new File(args[0]);
52+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
53+
54+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
55+
// wrapperUrl parameter.
56+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57+
String url = DEFAULT_DOWNLOAD_URL;
58+
if (mavenWrapperPropertyFile.exists()) {
59+
FileInputStream mavenWrapperPropertyFileInputStream = null;
60+
try {
61+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62+
Properties mavenWrapperProperties = new Properties();
63+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65+
} catch (IOException e) {
66+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67+
} finally {
68+
try {
69+
if (mavenWrapperPropertyFileInputStream != null) {
70+
mavenWrapperPropertyFileInputStream.close();
71+
}
72+
} catch (IOException e) {
73+
// Ignore ...
74+
}
75+
}
76+
}
77+
System.out.println("- Downloading from: " + url);
78+
79+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80+
if (!outputFile.getParentFile().exists()) {
81+
if (!outputFile.getParentFile().mkdirs()) {
82+
System.out.println(
83+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84+
}
85+
}
86+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87+
try {
88+
downloadFileFromURL(url, outputFile);
89+
System.out.println("Done");
90+
System.exit(0);
91+
} catch (Throwable e) {
92+
System.out.println("- Error downloading");
93+
e.printStackTrace();
94+
System.exit(1);
95+
}
96+
}
97+
98+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100+
String username = System.getenv("MVNW_USERNAME");
101+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102+
Authenticator.setDefault(new Authenticator() {
103+
@Override
104+
protected PasswordAuthentication getPasswordAuthentication() {
105+
return new PasswordAuthentication(username, password);
106+
}
107+
});
108+
}
109+
URL website = new URL(urlString);
110+
ReadableByteChannel rbc;
111+
rbc = Channels.newChannel(website.openStream());
112+
FileOutputStream fos = new FileOutputStream(destination);
113+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114+
fos.close();
115+
rbc.close();
116+
}
117+
118+
}
49.5 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

salary-service/README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Java Microservices - Salary Service
2+
3+
## Dependencies
4+
* Linux, VirtualBox or Docker Desktop
5+
* Java 11
6+
* Maven
7+
* Git Config Server Client
8+
* Spring Boot : 2.5.2
9+
* Netflix Eureka Client
10+
* Google Container Tools
11+
* H2 Database
12+
* Lombok
13+
* Zipkin
14+
* Apache Kafka
15+
* Spring Actuator
16+
* Spring Dev Tools
17+
18+
19+
20+
## Configuration
21+
src/main/docker/app.yml
22+
```yaml
23+
version: '3.8'
24+
services:
25+
salary-service:
26+
image: cevheri/salary-service
27+
environment:
28+
- _JAVA_OPTIONS=-Xmx512m -Xms256m
29+
- APP_SLEEP=10 # for zipkin
30+
ports:
31+
- 9004:9004
32+
zipkin:
33+
image: openzipkin/zipkin
34+
ports:
35+
- 9411:9411
36+
```
37+
38+
39+
We will use GitHub public repository for our configuration:
40+
https://github.com/cevheri/microservices-config-server
41+
42+
---
43+
## Development
44+
```shell
45+
$ ./mvnw clean package
46+
$ java -jar target/*.jar
47+
```
48+
Visit : http://localhost:8761/eureka/apps/salary-service
49+
50+
---
51+
## Production With Docker
52+
We will create Docker Image using Google Container Tools and run this Docker Image with Docker Compose.
53+
54+
```xml
55+
<pluginManagement>
56+
<plugins>
57+
<plugin>
58+
<groupId>com.google.cloud.tools</groupId>
59+
<artifactId>jib-maven-plugin</artifactId>
60+
<version>${jib-maven-plugin.version}</version>
61+
<configuration>
62+
<from>
63+
<image>adoptopenjdk:11-jre-hotspot</image>
64+
</from>
65+
<to>
66+
<image>${docker.hub.repo.name}/${project.artifactId}:${project.version}</image>
67+
</to>
68+
<container>
69+
<entrypoint>
70+
<shell>bash</shell>
71+
<option>-c</option>
72+
<arg>/entrypoint.sh</arg>
73+
</entrypoint>
74+
<ports>
75+
<port>9004</port>
76+
</ports>
77+
<environment>
78+
<SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
79+
<APP_SLEEP>0</APP_SLEEP>
80+
</environment>
81+
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
82+
<user>1000</user>
83+
</container>
84+
<extraDirectories>
85+
<paths>src/main/docker/jib</paths>
86+
<permissions>
87+
<permission>
88+
<file>/entrypoint.sh</file>
89+
<mode>755</mode>
90+
</permission>
91+
</permissions>
92+
</extraDirectories>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</pluginManagement>
97+
```
98+
99+
### Build docker image:
100+
```shell
101+
$ ./mvnw -Pprod clean verify jib:dockerBuild
102+
103+
...
104+
[INFO] Executing tasks:
105+
[INFO] [==============================] 100.0% complete
106+
[INFO]
107+
[INFO] ------------------------------------------------------------------------
108+
[INFO] BUILD SUCCESS
109+
[INFO] ------------------------------------------------------------------------
110+
[INFO] Total time: 17.728 s
111+
[INFO] Finished at: 21:45:11+03:00
112+
[INFO] ------------------------------------------------------------------------
113+
```
114+
115+
---
116+
117+
### Run:
118+
```shell
119+
$ docker-compose -f src/main/docker/app.yml up -d
120+
121+
Creating salary-service ... done
122+
```
123+
---
124+
Visit : http://localhost:8761/eureka/apps/salary-service
125+
```xml
126+
<application>
127+
<name>SALARY-SERVICE</name>
128+
<instance>
129+
<instanceId>192.168.1.57:salary-service:9004</instanceId>
130+
<hostName>service-registry</hostName>
131+
<app>SALARY-SERVICE</app>
132+
<ipAddr>192.168.1.57</ipAddr>
133+
<status>UP</status>
134+
...
135+
...
136+
</instance>
137+
</application>
138+
```
139+
140+
---
141+
### View docker images:
142+
```shell
143+
$ docker images
144+
145+
REPOSITORY TAG IMAGE ID CREATED SIZE
146+
cevheri/salary-service latest 57a100df8bcd 26 minutes ago 287MB
147+
148+
```
149+
150+
### View docker containers:
151+
````shell
152+
$ docker ps
153+
154+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
155+
2ca74578a26c cevheri/salary-service "bash -c /entrypoint…" 8 seconds ago Up 6 seconds 0.0.0.0:9004->9004/tcp, :::9004->9004/tcp salary-service
156+
157+
````
158+
159+
### Stop Docker Compose:
160+
```shell
161+
$ docker-compose -f src/main/docker/app.yml down
162+
163+
```
164+
165+
166+

0 commit comments

Comments
 (0)