Skip to content

Commit

Permalink
Update to microservercies
Browse files Browse the repository at this point in the history
  • Loading branch information
mirdesai81 committed Mar 16, 2018
1 parent 0ad6e5b commit 5ec06e7
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 30 deletions.
5 changes: 4 additions & 1 deletion gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>

<dependencies>
Expand All @@ -35,6 +34,10 @@
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.microservices.configuration.RibbonConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableEurekaClient
@RibbonClients(defaultConfiguration = RibbonConfiguration.class)
@EnableDiscoveryClient
/*@RibbonClients(value = {@RibbonClient(name="multiplication",configuration = RibbonConfiguration.class),@RibbonClient(name="gamification",configuration = RibbonConfiguration.class)})*/
@SpringBootApplication
public class GatewayApplication {

Expand Down
102 changes: 102 additions & 0 deletions gateway/src/main/java/com/microservices/GenericFallbackProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.microservices;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Created by mihir.desai on 3/15/2018.
*/
public class GenericFallbackProvider implements FallbackProvider {
private String route = "*";
private HttpStatus status = HttpStatus.SERVICE_UNAVAILABLE;
private String statusText = "Service Unavailable";
private int rawStatusCode = 503;
private HttpHeaders headers = null;
private String responseBody = "{\"message\":\"Service Unavailable. Please try after sometime\"}";

@Override
public String getRoute() {
return route;
}

public void setRoute(String route) {
this.route = route;
}

public void setStatus(HttpStatus status) {
this.status = status;
}

public void setStatusText(String statusText) {
this.statusText = statusText;
}

public void setRawStatusCode(int rawStatusCode) {
this.rawStatusCode = rawStatusCode;
}

public void setHeaders(HttpHeaders headers) {
this.headers = headers;
}

public void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}

@Override
public ClientHttpResponse fallbackResponse(String s, Throwable throwable) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
if(status == null) {
status = HttpStatus.SERVICE_UNAVAILABLE;
}
return status;
}

@Override
public int getRawStatusCode() throws IOException {
return rawStatusCode;
}

@Override
public String getStatusText() throws IOException {
if(statusText == null) {
statusText = "Service Unavailable";
}
return statusText;
}

@Override
public void close() {

}

@Override
public InputStream getBody() throws IOException {
if (responseBody == null)
responseBody ="{\"message\":\"Service Unavailable. Please try after sometime\"}";
return new ByteArrayInputStream(responseBody.getBytes());
}

@Override
public HttpHeaders getHeaders() {
if(headers == null) {
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccessControlAllowCredentials(true);
headers.setAccessControlAllowOrigin("*");
}
return headers;
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.microservices.configuration;

import com.microservices.GenericFallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.client.ClientHttpResponse;

/**
* Created by mihir.desai on 3/15/2018.
*/
@Configuration
public class HystrixFallbackProvider {
@Bean
public FallbackProvider zuulFallbackProvider() {
return new GenericFallbackProvider();
}

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

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by mihir.desai on 3/13/2018.
*/
@Slf4j
public class RibbonConfiguration {



@Bean
public IPing ribbonPing(final IClientConfig
iClientConfig) {
return new PingUrl(false, "/health");
log.debug("Ribbon PingUrl called");
return new PingUrl(false,"/application/health");
}

@Bean
public IRule ribbonRule(final IClientConfig iClientConfig) {
log.debug("Ribbon AvailabilityFilteringRule called");
return new AvailabilityFilteringRule();
}

@Bean
public ServerList<Server> ribbonServerList(final IClientConfig iClientConfig) {
ConfigurationBasedServerList configServerList = new ConfigurationBasedServerList();
configServerList.initWithNiwsConfig(iClientConfig);
return configServerList;
}

@Bean
public ServerListSubsetFilter serverListFilter() {
return new ServerListSubsetFilter();
}

}
16 changes: 10 additions & 6 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ server:
info:
component: Edge Server

endpoints:
restart:
enabled: true
shutdown:
enabled: true
management:
health:
sensitive: false
rabbit:
enabled: false

zuul:
ignoredServices: '*'
Expand Down Expand Up @@ -42,7 +39,14 @@ eureka:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
registerWithEureka: false
instance:
health-check-url: /health
status-page-url-path: /info

niws:
loadbalancer:
availabilityFilteringRule:
filterCircuitTripped: false.

logging:
level:
Expand Down
5 changes: 5 additions & 0 deletions microservices-gamification/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication
public class MicroservicesGamificationApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.microservices.gamification.client;

import com.microservices.gamification.client.dto.MultiplicationResultAttempt;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

Expand All @@ -18,16 +21,27 @@ public class MultiplicationResultAttemptClientImpl implements MultiplicationResu
private final String multiplicationHost;
private static final Logger logger = LogManager.getLogger(MultiplicationResultAttemptClientImpl.class);

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Autowired
public MultiplicationResultAttemptClientImpl(final RestTemplate restTemplate, @Value("${multiplicationHost}") final String multiplicationHost) {
this.restTemplate = restTemplate;
this.restTemplate = restTemplate();
this.multiplicationHost = multiplicationHost;
}

@HystrixCommand(fallbackMethod = "defaultResult")
@Override
public MultiplicationResultAttempt retrieveMultiplicationResultAttemptById(Long multiplicationAttemptId) {
logger.debug("Calling rest for {}",multiplicationHost);
return restTemplate.getForObject(multiplicationHost + "/results/" + multiplicationAttemptId
, MultiplicationResultAttempt.class);
}

private MultiplicationResultAttempt defaultResult(final Long multiplicationResultAttemptId) {
return new MultiplicationResultAttempt("fakeAlias",10,10,100,true);
}
}
14 changes: 7 additions & 7 deletions microservices-gamification/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ spring:
datasource:
url: jdbc:h2:file:~/gamification;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE;
rabbitmq:
host: localhost
password: guest
username: guest
host: 192.168.28.61
password: Ansa@Dev
username: ansadev
port: 5672

server:
Expand Down Expand Up @@ -44,10 +44,10 @@ multiplication:
eureka:
client:
service-url:
default-zone: http://[myIP@]:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
default-zone: ${vcap.services.discovery-service.credentials.uri:http://127.0.0.1:8761}/eureka/
instance:
lease-expiration-duration-in-seconds: 2
lease-renewal-interval-in-seconds: 1

ribbon:
eureka:
Expand Down
16 changes: 7 additions & 9 deletions microservices-multiplication/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ spring:
datasource:
url: jdbc:h2:file:~/social-multiplication;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE;
rabbitmq:
host: localhost
password: guest
username: guest
host: 192.168.28.61
password: Ansa@Dev
username: ansadev
port: 5672

server:
Expand All @@ -40,12 +40,10 @@ multiplication:
eureka:
client:
service-url:
default-zone: http://[myIP@]:8761/eureka/
default-zone: ${vcap.services.discovery-service.credentials.uri:http://127.0.0.1:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
lease-expiration-duration-in-seconds: 2
lease-renewal-interval-in-seconds: 1


ribbon:
eureka:
enabled: true

6 changes: 5 additions & 1 deletion service-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>

<dependencies>
Expand All @@ -28,6 +27,11 @@
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
4 changes: 1 addition & 3 deletions service-registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ eureka:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
fetch-registry: false
Loading

0 comments on commit 5ec06e7

Please sign in to comment.