Skip to content

Commit de2e980

Browse files
initial commit
0 parents  commit de2e980

File tree

89 files changed

+3629
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3629
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.DS_Store
2+
build/
3+
target/
4+
bin/
5+
dependency-reduced-pom.xml
6+
.classpath
7+
.project
8+
.settings/
9+
*.iml
10+
*.ipr
11+
*.iws
12+
*.idea
13+
*.log
14+
Thumbs.db

LICENSE

+674
Large diffs are not rendered by default.

department-service/Dockerfile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
ARG MVN_VERSION=3.6.3
2+
ARG JDK_VERSION=11
3+
4+
FROM maven:${MVN_VERSION}-jdk-${JDK_VERSION}-slim as build
5+
6+
WORKDIR /build
7+
COPY pom.xml .
8+
# creates a layer with all of the Manven dependencies, first time it takes a while consequent call are very fast
9+
RUN mvn dependency:go-offline
10+
11+
COPY ./pom.xml /tmp/
12+
COPY ./src /tmp/src/
13+
WORKDIR /tmp/
14+
RUN mvn clean package
15+
16+
WORKDIR /application
17+
COPY target/*.jar application.jar
18+
RUN java -Djarmode=layertools -jar application.jar extract
19+
20+
FROM gcr.io/distroless/java:${JDK_VERSION}
21+
22+
USER nonroot:nonroot
23+
WORKDIR application
24+
25+
COPY --from=build --chown=nonroot:nonroot /application/dependencies/ ./
26+
COPY --from=build --chown=nonroot:nonroot /application/snapshot-dependencies/ ./
27+
COPY --from=build --chown=nonroot:nonroot /application/resources/ ./
28+
COPY --from=build --chown=nonroot:nonroot /application/application/ ./
29+
30+
EXPOSE 8080
31+
32+
ENV _JAVA_OPTIONS "-XX:MinRAMPercentage=60.0 -XX:MaxRAMPercentage=90.0 \
33+
-Djava.security.egd=file:/dev/./urandom \
34+
-Djava.awt.headless=true -Dfile.encoding=UTF-8 \
35+
-Dspring.output.ansi.enabled=ALWAYS \
36+
-Dspring.profiles.active=default"
37+
38+
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

department-service/pom.xml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.3.0.M2</version>
9+
</parent>
10+
<artifactId>department-service</artifactId>
11+
<groupId>vmware.services</groupId>
12+
<version>1.1</version>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
</properties>
17+
18+
<dependencyManagement>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.cloud</groupId>
22+
<artifactId>spring-cloud-dependencies</artifactId>
23+
<version>Hoxton.SR2</version>
24+
<type>pom</type>
25+
<scope>import</scope>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.cloud</groupId>
41+
<artifactId>spring-cloud-starter-openfeign</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-actuator</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.springfox</groupId>
57+
<artifactId>springfox-swagger2</artifactId>
58+
<version>2.9.2</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-starter-sleuth</artifactId>
63+
</dependency>
64+
</dependencies>
65+
66+
<repositories>
67+
<repository>
68+
<id>spring-milestones</id>
69+
<name>Spring Milestones</name>
70+
<url>https://repo.spring.io/milestone</url>
71+
</repository>
72+
</repositories>
73+
<pluginRepositories>
74+
<pluginRepository>
75+
<id>spring-milestones</id>
76+
<name>Spring Milestones</name>
77+
<url>https://repo.spring.io/milestone</url>
78+
</pluginRepository>
79+
</pluginRepositories>
80+
81+
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-maven-plugin</artifactId>
87+
<configuration>
88+
<layout>LAYERED_JAR</layout>
89+
</configuration>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package vmware.services.department;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
8+
import org.springframework.cloud.netflix.ribbon.RibbonClients;
9+
import org.springframework.cloud.openfeign.EnableFeignClients;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
12+
import springfox.documentation.builders.ApiInfoBuilder;
13+
import springfox.documentation.builders.PathSelectors;
14+
import springfox.documentation.builders.RequestHandlerSelectors;
15+
import springfox.documentation.spi.DocumentationType;
16+
import springfox.documentation.spring.web.plugins.Docket;
17+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
18+
import vmware.services.department.config.RibbonConfiguration;
19+
20+
@SpringBootApplication
21+
@EnableDiscoveryClient
22+
@EnableFeignClients
23+
@EnableMongoRepositories
24+
@EnableSwagger2
25+
@AutoConfigureAfter(RibbonAutoConfiguration.class)
26+
@RibbonClients(defaultConfiguration = RibbonConfiguration.class)
27+
public class DepartmentApplication {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(DepartmentApplication.class, args);
31+
}
32+
33+
@Bean
34+
public Docket swaggerApi() {
35+
return new Docket(DocumentationType.SWAGGER_2)
36+
.select()
37+
.apis(RequestHandlerSelectors.basePackage("vmware.services.department.controller"))
38+
.paths(PathSelectors.any())
39+
.build()
40+
.apiInfo(new ApiInfoBuilder().version("1.0").title("Department API").description("Documentation Department API v1.0").build());
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package vmware.services.department.client;
2+
3+
import org.springframework.cloud.openfeign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import vmware.services.department.model.Employee;
7+
8+
import java.util.List;
9+
10+
@FeignClient(name = "employee")
11+
public interface EmployeeClient {
12+
13+
@GetMapping("/department/{departmentId}")
14+
List<Employee> findByDepartment(@PathVariable("departmentId") String departmentId);
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package vmware.services.department.config;
2+
3+
import com.netflix.client.config.IClientConfig;
4+
import com.netflix.loadbalancer.Server;
5+
import com.netflix.loadbalancer.ServerList;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.cloud.client.discovery.DiscoveryClient;
9+
import org.springframework.cloud.netflix.ribbon.StaticServerList;
10+
import org.springframework.context.annotation.Bean;
11+
12+
public class RibbonConfiguration {
13+
14+
@Autowired
15+
private DiscoveryClient discoveryClient;
16+
17+
private String serviceId = "client";
18+
protected static final String VALUE_NOT_SET = "__not__set__";
19+
protected static final String DEFAULT_NAMESPACE = "ribbon";
20+
21+
public RibbonConfiguration() {
22+
}
23+
24+
public RibbonConfiguration(String serviceId) {
25+
this.serviceId = serviceId;
26+
}
27+
28+
@Bean
29+
@ConditionalOnMissingBean
30+
public ServerList<?> ribbonServerList(IClientConfig config) {
31+
32+
Server[] servers = discoveryClient.getInstances(config.getClientName()).stream()
33+
.map(i -> new Server(i.getHost(), i.getPort()))
34+
.toArray(Server[]::new);
35+
36+
return new StaticServerList(servers);
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package vmware.services.department.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import vmware.services.department.client.EmployeeClient;
8+
import vmware.services.department.model.Department;
9+
import vmware.services.department.model.Employee;
10+
import vmware.services.department.repository.DepartmentRepository;
11+
12+
import java.util.List;
13+
14+
@RestController
15+
public class DepartmentController {
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);
18+
19+
@Autowired
20+
DepartmentRepository repository;
21+
@Autowired
22+
EmployeeClient employeeClient;
23+
24+
@GetMapping("/feign")
25+
public List<Employee> listRest() {
26+
return employeeClient.findByDepartment("1");
27+
}
28+
29+
@PostMapping("/")
30+
public Department add(@RequestBody Department department) {
31+
LOGGER.info("Department add: {}", department);
32+
return repository.save(department);
33+
}
34+
35+
@GetMapping("/{id}")
36+
public Department findById(@PathVariable("id") String id) {
37+
LOGGER.info("Department find: id={}", id);
38+
return repository.findById(id).get();
39+
}
40+
41+
@GetMapping("/")
42+
public Iterable<Department> findAll() {
43+
LOGGER.info("Department find");
44+
return repository.findAll();
45+
}
46+
47+
@GetMapping("/organization/{organizationId}")
48+
public List<Department> findByOrganization(@PathVariable("organizationId") Long organizationId) {
49+
LOGGER.info("Department find: organizationId={}", organizationId);
50+
return repository.findByOrganizationId(organizationId);
51+
}
52+
53+
@GetMapping("/organization/{organizationId}/with-employees")
54+
public List<Department> findByOrganizationWithEmployees(@PathVariable("organizationId") Long organizationId) {
55+
LOGGER.info("Department find: organizationId={}", organizationId);
56+
List<Department> departments = repository.findByOrganizationId(organizationId);
57+
departments.forEach(d -> d.setEmployees(employeeClient.findByDepartment(d.getId())));
58+
return departments;
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package vmware.services.department.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.annotation.Transient;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@Document(collection = "department")
11+
public class Department {
12+
13+
@Id
14+
private String id;
15+
private Long organizationId;
16+
private String name;
17+
@Transient
18+
private List<Employee> employees = new ArrayList<>();
19+
20+
public Department() {
21+
22+
}
23+
24+
public Department(Long organizationId, String name) {
25+
super();
26+
this.organizationId = organizationId;
27+
this.name = name;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public Long getOrganizationId() {
39+
return organizationId;
40+
}
41+
42+
public void setOrganizationId(Long organizationId) {
43+
this.organizationId = organizationId;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public List<Employee> getEmployees() {
55+
return employees;
56+
}
57+
58+
public void setEmployees(List<Employee> employees) {
59+
this.employees = employees;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Department [id=" + id + ", organizationId=" + organizationId + ", name=" + name + "]";
65+
}
66+
67+
}

0 commit comments

Comments
 (0)