-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
...ons-edge/livelessons-edge-eureka-service/src/main/java/demo/EurekaServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
livelessons-edge/livelessons-edge-greetings-client/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+6 KB
livelessons-edge/livelessons-edge-greetings-client/src/main/java/.DS_Store
Binary file not shown.
119 changes: 119 additions & 0 deletions
119
...edge/livelessons-edge-greetings-client/src/main/java/demo/GreetingsClientApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
livelessons-edge/livelessons-edge-greetings-client/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
livelessons-edge/livelessons-edge-greetings-service/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
32 changes: 32 additions & 0 deletions
32
...ge/livelessons-edge-greetings-service/src/main/java/demo/GreetingsServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "!"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...lessons-edge/livelessons-edge-greetings-service/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.application.name = greetings-service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |