Skip to content

Commit

Permalink
new in v2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlong committed Sep 30, 2016
1 parent 187b16a commit 3aa37f1
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 0 deletions.
9 changes: 9 additions & 0 deletions livelessons-edge/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:compat-mode:
= Lesson 9: Developing Web Applications

_Basics of developing web applications to consume your Microservices._

- link:livelessons-web-resources[Resources]
- link:livelessons-web-resources-angular[Resources (Angular)]
- link:livelessons-web-templates[Templating]
- link:livelessons-web-transforms[Transforms]
27 changes: 27 additions & 0 deletions livelessons-edge/livelessons-edge-eureka-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>livelessons</groupId>
<artifactId>livelessons-edge</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>livelessons-edge-eureka-service</artifactId>
<properties>
<main.basedir>../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

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

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
48 changes: 48 additions & 0 deletions livelessons-edge/livelessons-edge-greetings-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>livelessons</groupId>
<artifactId>livelessons-edge</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>livelessons-edge-eureka-client</artifactId>
<properties>
<main.basedir>../..</main.basedir>
</properties>
<dependencies>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

</dependencies>
</project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package demo ;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@EnableCircuitBreaker
@EnableFeignClients
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class GreetingsClientApplication {

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

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

@FeignClient("greetings-service")
interface GreetingsClient {

@RequestMapping(method = RequestMethod.GET, value = "/greetings/{name}")
Greeting greet(@PathVariable("name") String name);
}

@RestController
class GreetingsApiGatewayRestController {

private final GreetingsClient greetingsClient;

@Autowired
public GreetingsApiGatewayRestController(GreetingsClient client) {
this.greetingsClient = client;
}

public String fallback(String name) {
return "ohai!";
}

@HystrixCommand(fallbackMethod = "fallback")
@RequestMapping(method = RequestMethod.GET, value = "/hi/{name}")
String greet(@PathVariable String name) {
return this.greetingsClient.greet(name).getGreeting();
}
}

class Greeting {
private String greeting;

public String getGreeting() {
return greeting;
}
}

//@Component
class RateLimitingZuulFilter extends ZuulFilter {

private final RateLimiter rateLimiter = RateLimiter.create(1.0 / 30.0);

@Override
public String filterType() {
return "pre";
}

@Override
public int filterOrder() {
return Ordered.HIGHEST_PRECEDENCE + 100;
}

@Override
public boolean shouldFilter() {
return true;
}

@Override
public Object run() {
try {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletResponse response = currentContext.getResponse();

if (!this.rateLimiter.tryAcquire()) {
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
response.getWriter().append(HttpStatus.TOO_MANY_REQUESTS.getReasonPhrase());
currentContext.setSendZuulResponse(false);
}
} catch (IOException e) {
ReflectionUtils.rethrowRuntimeException(e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.application.name = greetings-client
server.port=9999
41 changes: 41 additions & 0 deletions livelessons-edge/livelessons-edge-greetings-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>livelessons</groupId>
<artifactId>livelessons-edge</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>livelessons-edge-greetings-service</artifactId>
<properties>
<main.basedir>../..</main.basedir>
</properties>
<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

@EnableDiscoveryClient
@SpringBootApplication
public class GreetingsServiceApplication {

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

@RestController
class GreetingsRestController {

@RequestMapping(method = RequestMethod.GET, value = "/greetings/{name}")
Map<String, String> greeting(@PathVariable String name,
@RequestHeader("x-forwarded-host") Optional<String> host,
@RequestHeader("x-forwarded-port") Optional<Integer> port) {
host.ifPresent(h -> System.out.println("host = " + h));
port.ifPresent(p -> System.out.println("port = " + p));
return Collections.singletonMap("greeting", "Hello, " + name + "!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name = greetings-service
35 changes: 35 additions & 0 deletions livelessons-edge/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>livelessons</groupId>
<artifactId>livelessons-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../livelessons-parent</relativePath>
</parent>
<artifactId>livelessons-edge</artifactId>
<packaging>pom</packaging>
<properties>
<main.basedir>..</main.basedir>
</properties>
<modules>
<module>livelessons-edge-eureka-service</module>
<module>livelessons-edge-greetings-client</module>
<module>livelessons-edge-greetings-service</module>

</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>

0 comments on commit 3aa37f1

Please sign in to comment.