Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Kubernetes #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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 account-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk
FROM openjdk:alpine
MAINTAINER Piotr Minkowski <[email protected]>
ADD target/account-service.jar account-service.jar
ENTRYPOINT ["java", "-jar", "/account-service.jar"]
Expand Down
5 changes: 4 additions & 1 deletion account-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -33,7 +37,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
<configuration>
<mainClass>pl.piomin.microservices.account.Application</mainClass>
<addResources>true</addResources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
package pl.piomin.microservices.account.api;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import pl.piomin.microservices.account.data.AccountRepository;
import pl.piomin.microservices.account.model.Account;

@RestController
public class Api {

private List<Account> accounts;

@Autowired
AccountRepository repository;

protected Logger logger = Logger.getLogger(Api.class.getName());

public Api() {
accounts = new ArrayList<>();
accounts.add(new Account(1, 1, "111111"));
accounts.add(new Account(2, 2, "222222"));
accounts.add(new Account(3, 3, "333333"));
accounts.add(new Account(4, 4, "444444"));
accounts.add(new Account(5, 1, "555555"));
accounts.add(new Account(6, 2, "666666"));
accounts.add(new Account(7, 2, "777777"));
}


@RequestMapping("/accounts/{number}")
public Account findByNumber(@PathVariable("number") String number) {
logger.info(String.format("Account.findByNumber(%s)", number));
return accounts.stream().filter(it -> it.getNumber().equals(number)).findFirst().get();
return repository.findByNumber(number);
}

@RequestMapping("/accounts/customer/{customer}")
public List<Account> findByCustomer(@PathVariable("customer") Integer customerId) {
public List<Account> findByCustomer(@PathVariable("customer") String customerId) {
logger.info(String.format("Account.findByCustomer(%s)", customerId));
return accounts.stream().filter(it -> it.getCustomerId().intValue()==customerId.intValue()).collect(Collectors.toList());
return repository.findByCustomerId(customerId);
}

@RequestMapping("/accounts")
public List<Account> findAll() {
logger.info("Account.findAll()");
return accounts;
return repository.findAll();
}

@RequestMapping(method = RequestMethod.POST, value = "/accounts")
public Account add(@RequestBody Account account) {
logger.info(String.format("Account.add(%s)", account));
return repository.save(account);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.piomin.microservices.account.data;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;

import pl.piomin.microservices.account.model.Account;

public interface AccountRepository extends MongoRepository<Account, String> {

public Account findByNumber(String number);
public List<Account> findByCustomerId(String customerId);

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package pl.piomin.microservices.account.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "account")
public class Account {

private Integer id;
private Integer customerId;
@Id
private String id;
private String customerId;
private String number;

public Account() {

}

public Account(Integer id, Integer customerId, String number) {
public Account(String id, String customerId, String number) {
this.id = id;
this.customerId = customerId;
this.number = number;
}

public Integer getId() {
public String getId() {
return id;
}

public void setId(Integer id) {
public void setId(String id) {
this.id = id;
}

public Integer getCustomerId() {
public String getCustomerId() {
return customerId;
}

public void setCustomerId(Integer customerId) {
public void setCustomerId(String customerId) {
this.customerId = customerId;
}

Expand Down
6 changes: 6 additions & 0 deletions account-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ server:
spring:
application:
name: account-service
data:
mongodb:
host: localhost
port: 27017
username: micro
password: micro
logging:
pattern:
console: "%clr(%d{yyyy-MM-dd HH:mm:ss}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr([${springAppName:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]){yellow} %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
Expand Down
2 changes: 1 addition & 1 deletion customer-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk
FROM openjdk:alpine
MAINTAINER Piotr Minkowski <[email protected]>
ADD target/customer-service.jar customer-service.jar
ENTRYPOINT ["java", "-jar", "/customer-service.jar"]
Expand Down
5 changes: 4 additions & 1 deletion customer-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -37,7 +41,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
<configuration>
<mainClass>pl.piomin.microservices.customer.Application</mainClass>
<addResources>true</addResources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
package pl.piomin.microservices.customer.api;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import pl.piomin.microservices.customer.data.CustomerRepository;
import pl.piomin.microservices.customer.intercomm.AccountClient;
import pl.piomin.microservices.customer.model.Account;
import pl.piomin.microservices.customer.model.Customer;
import pl.piomin.microservices.customer.model.CustomerType;

@RestController
public class Api {

@Autowired
private AccountClient accountClient;

protected Logger logger = Logger.getLogger(Api.class.getName());

private List<Customer> customers;
@Autowired
CustomerRepository repository;

public Api() {
customers = new ArrayList<>();
customers.add(new Customer(1, "12345", "Adam Kowalski", CustomerType.INDIVIDUAL));
customers.add(new Customer(2, "12346", "Anna Malinowska", CustomerType.INDIVIDUAL));
customers.add(new Customer(3, "12347", "Paweł Michalski", CustomerType.INDIVIDUAL));
customers.add(new Customer(4, "12348", "Karolina Lewandowska", CustomerType.INDIVIDUAL));
}
protected Logger logger = Logger.getLogger(Api.class.getName());

@RequestMapping("/customers/pesel/{pesel}")
public Customer findByPesel(@PathVariable("pesel") String pesel) {
logger.info(String.format("Customer.findByPesel(%s)", pesel));
return customers.stream().filter(it -> it.getPesel().equals(pesel)).findFirst().get();
return repository.findByPesel(pesel);
}

@RequestMapping("/customers")
public List<Customer> findAll() {
logger.info("Customer.findAll()");
return customers;
return repository.findAll();
}

@RequestMapping("/customers/{id}")
public Customer findById(@PathVariable("id") Integer id) {
public Customer findById(@PathVariable("id") String id) {
logger.info(String.format("Customer.findById(%s)", id));
Customer customer = customers.stream().filter(it -> it.getId().intValue()==id.intValue()).findFirst().get();
Customer customer = repository.findById(id);
List<Account> accounts = accountClient.getAccounts(id);
customer.setAccounts(accounts);
return customer;
}

@RequestMapping(method = RequestMethod.POST, value = "/customers")
public Customer add(@RequestBody Customer customer) {
logger.info(String.format("Customer.add(%s)", customer));
return repository.save(customer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pl.piomin.microservices.customer.data;

import org.springframework.data.mongodb.repository.MongoRepository;

import pl.piomin.microservices.customer.model.Customer;

public interface CustomerRepository extends MongoRepository<Customer, String> {

public Customer findByPesel(String pesel);
public Customer findById(String id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import pl.piomin.microservices.customer.model.Account;

@FeignClient("account-service")
@FeignClient(name = "account-service", url = "http://account-service:2222")
public interface AccountClient {

@RequestMapping(method = RequestMethod.GET, value = "/accounts/customer/{customerId}")
List<Account> getAccounts(@PathVariable("customerId") Integer customerId);
@RequestMapping(method = RequestMethod.GET, value = "/accounts/customer/{customerId}")
List<Account> getAccounts(@PathVariable("customerId") String customerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

public class Account {

private Integer id;
private String id;
private String number;

public Account() {

}

public Account(Integer id, String number) {
public Account(String id, String number) {
super();
this.id = id;
this.number = number;
}

public Integer getId() {
public String getId() {
return id;
}

public void setId(Integer id) {
public void setId(String id) {
this.id = id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "customer")
public class Customer {

private Integer id;
@Id
private String id;
private String pesel;
private String name;
private CustomerType type;
Expand All @@ -14,18 +19,18 @@ public Customer() {

}

public Customer(Integer id, String pesel, String name, CustomerType type) {
public Customer(String id, String pesel, String name, CustomerType type) {
this.id = id;
this.pesel = pesel;
this.name = name;
this.type = type;
}

public Integer getId() {
public String getId() {
return id;
}

public void setId(Integer id) {
public void setId(String id) {
this.id = id;
}

Expand Down
Loading