Skip to content

Commit 3586dbc

Browse files
committed
Working example
0 parents  commit 3586dbc

Some content is hidden

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

51 files changed

+1584
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.project
2+
/.settings/

department-service/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
/.classpath
3+
/.project
4+
/.settings/

department-service/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM openjdk:8-jre-alpine
2+
ENV APP_FILE department-service-1.0-SNAPSHOT.jar
3+
ENV APP_HOME /usr/apps
4+
EXPOSE 8080
5+
COPY target/$APP_FILE $APP_HOME/
6+
WORKDIR $APP_HOME
7+
ENTRYPOINT ["sh", "-c"]
8+
CMD ["exec java -jar $APP_FILE"]

department-service/pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.0.0.RELEASE</version>
9+
</parent>
10+
<artifactId>department-service</artifactId>
11+
<groupId>pl.piomin.services</groupId>
12+
<version>1.0-SNAPSHOT</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>Finchley.RELEASE</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</artifactId>
34+
<version>0.3.0.BUILD-SNAPSHOT</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
43+
<version>0.3.0.BUILD-SNAPSHOT</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-starter-openfeign</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-openfeign-core</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-actuator</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.springfox</groupId>
67+
<artifactId>springfox-swagger2</artifactId>
68+
<version>2.9.2</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework.cloud</groupId>
72+
<artifactId>spring-cloud-starter-sleuth</artifactId>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package pl.piomin.services.department;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.cloud.openfeign.EnableFeignClients;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
9+
10+
import springfox.documentation.builders.ApiInfoBuilder;
11+
import springfox.documentation.builders.PathSelectors;
12+
import springfox.documentation.builders.RequestHandlerSelectors;
13+
import springfox.documentation.spi.DocumentationType;
14+
import springfox.documentation.spring.web.plugins.Docket;
15+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
16+
17+
@SpringBootApplication
18+
@EnableDiscoveryClient
19+
@EnableFeignClients
20+
@EnableMongoRepositories
21+
@EnableSwagger2
22+
//@RibbonClient(name = "employee")
23+
public class DepartmentApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(DepartmentApplication.class, args);
27+
}
28+
29+
@Bean
30+
public Docket swaggerPersonApi10() {
31+
return new Docket(DocumentationType.SWAGGER_2)
32+
.select()
33+
.apis(RequestHandlerSelectors.basePackage("pl.piomin.services.department.controller"))
34+
.paths(PathSelectors.any())
35+
.build()
36+
.apiInfo(new ApiInfoBuilder().version("1.0").title("Department API").description("Documentation Department API v1.0").build());
37+
}
38+
39+
// @Bean
40+
// @LoadBalanced
41+
// RestTemplate restTemplate() {
42+
// return new RestTemplate();
43+
// }
44+
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pl.piomin.services.department.client;
2+
3+
import java.util.List;
4+
5+
import org.springframework.cloud.openfeign.FeignClient;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
9+
import pl.piomin.services.department.model.Employee;
10+
11+
@FeignClient(name = "employee")
12+
public interface EmployeeClient {
13+
14+
@GetMapping("/department/{departmentId}")
15+
List<Employee> findByDepartment(@PathVariable("departmentId") String departmentId);
16+
17+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package pl.piomin.services.department.controller;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.cloud.client.discovery.DiscoveryClient;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.client.RestTemplate;
16+
17+
import pl.piomin.services.department.client.EmployeeClient;
18+
import pl.piomin.services.department.model.Department;
19+
import pl.piomin.services.department.model.Employee;
20+
import pl.piomin.services.department.repository.DepartmentRepository;
21+
22+
@RestController
23+
public class DepartmentController {
24+
25+
private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);
26+
27+
@Autowired
28+
DepartmentRepository repository;
29+
@Autowired
30+
EmployeeClient employeeClient;
31+
32+
@GetMapping("/feign")
33+
public List<Employee> listRest() {
34+
return employeeClient.findByDepartment("1");
35+
}
36+
37+
@PostMapping("/")
38+
public Department add(@RequestBody Department department) {
39+
LOGGER.info("Department add: {}", department);
40+
return repository.save(department);
41+
}
42+
43+
@GetMapping("/{id}")
44+
public Department findById(@PathVariable("id") String id) {
45+
LOGGER.info("Department find: id={}", id);
46+
return repository.findById(id).get();
47+
}
48+
49+
@GetMapping("/")
50+
public Iterable<Department> findAll() {
51+
LOGGER.info("Department find");
52+
return repository.findAll();
53+
}
54+
55+
@GetMapping("/organization/{organizationId}")
56+
public List<Department> findByOrganization(@PathVariable("organizationId") Long organizationId) {
57+
LOGGER.info("Department find: organizationId={}", organizationId);
58+
return repository.findByOrganizationId(organizationId);
59+
}
60+
61+
@GetMapping("/organization/{organizationId}/with-employees")
62+
public List<Department> findByOrganizationWithEmployees(@PathVariable("organizationId") Long organizationId) {
63+
LOGGER.info("Department find: organizationId={}", organizationId);
64+
List<Department> departments = repository.findByOrganizationId(organizationId);
65+
departments.forEach(d -> d.setEmployees(employeeClient.findByDepartment(d.getId())));
66+
return departments;
67+
}
68+
69+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package pl.piomin.services.department.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.annotation.Transient;
8+
import org.springframework.data.mongodb.core.mapping.Document;
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package pl.piomin.services.department.model;
2+
3+
public class Employee {
4+
5+
private Long id;
6+
private String name;
7+
private int age;
8+
private String position;
9+
10+
public Employee() {
11+
12+
}
13+
14+
public Employee(String name, int age, String position) {
15+
this.name = name;
16+
this.age = age;
17+
this.position = position;
18+
}
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public int getAge() {
37+
return age;
38+
}
39+
40+
public void setAge(int age) {
41+
this.age = age;
42+
}
43+
44+
public String getPosition() {
45+
return position;
46+
}
47+
48+
public void setPosition(String position) {
49+
this.position = position;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Employee [id=" + id + ", name=" + name + ", position=" + position + "]";
55+
}
56+
57+
}

0 commit comments

Comments
 (0)