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

Commit

Permalink
Merge pull request #10 from ramazansakin/master
Browse files Browse the repository at this point in the history
docker-compose & refactoring
piomin authored Sep 1, 2020

Verified

This commit was signed with the committer’s verified signature.
2 parents cfc4816 + 634395c commit f3de98d
Showing 38 changed files with 522 additions and 429 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.project
*.idea
*.iml
7 changes: 4 additions & 3 deletions account-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM openjdk
FROM openjdk:8-jdk-alpine
MAINTAINER Piotr Minkowski <[email protected]>
MAINTAINER Ramazan Sakin <[email protected]>
ADD target/account-service.jar account-service.jar
ENTRYPOINT ["java", "-jar", "/account-service.jar"]
EXPOSE 2222
EXPOSE 2222
ENTRYPOINT ["java", "-jar", "/account-service.jar"]
2 changes: 1 addition & 1 deletion account-service/pom.xml
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>pl.piomin.microservices.account.Application</mainClass>
<mainClass>pl.piomin.microservices.account.AccountServiceApplication</mainClass>
<addResources>true</addResources>
</configuration>
<executions>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package pl.piomin.microservices.account;

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);
}

}
package pl.piomin.microservices.account;

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

@SpringBootApplication
@EnableDiscoveryClient
public class AccountServiceApplication {

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

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

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import pl.piomin.microservices.account.exceptions.AccountNotFoundException;
import pl.piomin.microservices.account.model.Account;

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

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@Slf4j
public class Api {

private List<Account> accounts;

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();
}

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

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

private List<Account> accounts;

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(method = RequestMethod.GET, value = "/number/{number}")
public Account findByNumber(@PathVariable("number") String number) throws AccountNotFoundException {
log.info(String.format("Account.findByNumber(%s)", number));
return accounts.stream()
.filter(it -> it.getNumber().equals(number))
.findFirst()
.orElseThrow(() -> new AccountNotFoundException("number : " + number));
}

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

@RequestMapping(method = RequestMethod.GET, value = "")
public List<Account> findAll() {
log.info("Account.findAll()");
return accounts;
}

@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public Account findById(@PathVariable Integer id) throws AccountNotFoundException {
log.info("Account.findAll()");
return accounts.stream()
.filter(it -> it.getId().equals(id))
.findFirst()
.orElseThrow(() -> new AccountNotFoundException("id : " + id));
}

@RequestMapping(method = RequestMethod.POST, value = "")
public Account createNewAccount(@RequestBody Account account) {
log.info("Account.createNewAccount()");
if (account.getId() != null) {
return null;
}
int size = accounts.size();
account.setId(size + 1);
accounts.add(account);
return account;
}

@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
public boolean deleteAccount(@PathVariable Integer id) {
log.info("Account.deleteAccount()");
try {
Account byId = findById(id);
accounts.remove(byId);
} catch (AccountNotFoundException e) {
return false;
}
return true;
}

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

import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("pl.piomin.microservices.account")
public class AccountConfiguration {

@Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}

}
package pl.piomin.microservices.account.config;

import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("pl.piomin.microservices.account")
public class AccountConfiguration {

@Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}

}
Original file line number Diff line number Diff line change
@@ -4,11 +4,11 @@

@ResponseStatus(org.springframework.http.HttpStatus.NOT_FOUND)
public class AccountNotFoundException extends Exception {

private static final long serialVersionUID = 1L;

public AccountNotFoundException(String accountNumber) {
super("No such account: " + accountNumber);
}

private static final long serialVersionUID = 1L;

public AccountNotFoundException(String cause) {
super("No such account with " + cause);
}

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

public class Account {

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

public Account() {

}

public Account(Integer id, Integer customerId, String number) {
this.id = id;
this.customerId = customerId;
this.number = number;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public Integer getId() {
return id;
}

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

public Integer getCustomerId() {
return customerId;
}

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

public String getNumber() {
return number;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Account {

public void setNumber(String number) {
this.number = number;
}
private Integer id;
private Integer customerId;
private String number;

}
10 changes: 5 additions & 5 deletions account-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server:
server:
port: ${PORT:2222}

spring:
spring:
application:
name: account-service
logging:
@@ -11,14 +11,14 @@ spring:
org.springframework: WARN
org.hibernate: WARN

eureka:
eureka:
client:
serviceUrl:
defaultZone: ${DISCOVERY_URL:http://localhost:8761}/eureka/
defaultZone: http://localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2

ribbon:
eureka:
enabled: true
5 changes: 3 additions & 2 deletions customer-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM openjdk
FROM openjdk:8-jdk-alpine
MAINTAINER Piotr Minkowski <[email protected]>
MAINTAINER Ramazan Sakin <[email protected]>
ADD target/customer-service.jar customer-service.jar
EXPOSE 3333
ENTRYPOINT ["java", "-jar", "/customer-service.jar"]
EXPOSE 3333
2 changes: 1 addition & 1 deletion customer-service/pom.xml
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>pl.piomin.microservices.customer.Application</mainClass>
<mainClass>pl.piomin.microservices.customer.CustomerServiceApplication</mainClass>
<addResources>true</addResources>
</configuration>
<executions>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package pl.piomin.microservices.customer;

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 {

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

}
package pl.piomin.microservices.customer;

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 CustomerServiceApplication {

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

}
Loading

0 comments on commit f3de98d

Please sign in to comment.