Skip to content

Commit 3aa37f1

Browse files
committed
new in v2
1 parent 187b16a commit 3aa37f1

File tree

11 files changed

+328
-0
lines changed

11 files changed

+328
-0
lines changed

livelessons-edge/README.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:compat-mode:
2+
= Lesson 9: Developing Web Applications
3+
4+
_Basics of developing web applications to consume your Microservices._
5+
6+
- link:livelessons-web-resources[Resources]
7+
- link:livelessons-web-resources-angular[Resources (Angular)]
8+
- link:livelessons-web-templates[Templating]
9+
- link:livelessons-web-transforms[Transforms]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>livelessons</groupId>
7+
<artifactId>livelessons-edge</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>livelessons-edge-eureka-service</artifactId>
11+
<properties>
12+
<main.basedir>../..</main.basedir>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.cloud</groupId>
17+
<artifactId>spring-cloud-starter-eureka-server</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-test</artifactId>
23+
<scope>test</scope>
24+
</dependency>
25+
26+
</dependencies>
27+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
@EnableEurekaServer
8+
@SpringBootApplication
9+
public class EurekaServiceApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(EurekaServiceApplication.class, args);
13+
}
14+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>livelessons</groupId>
7+
<artifactId>livelessons-edge</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>livelessons-edge-eureka-client</artifactId>
11+
<properties>
12+
<main.basedir>../..</main.basedir>
13+
</properties>
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>com.google.guava</groupId>
18+
<artifactId>guava</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.cloud</groupId>
22+
<artifactId>spring-cloud-starter-zuul</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.cloud</groupId>
26+
<artifactId>spring-cloud-starter-eureka</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.cloud</groupId>
30+
<artifactId>spring-cloud-starter-feign</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-starter-hystrix</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
</dependencies>
48+
</project>
Binary file not shown.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package demo ;
2+
3+
import com.google.common.util.concurrent.RateLimiter;
4+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
5+
import com.netflix.zuul.ZuulFilter;
6+
import com.netflix.zuul.context.RequestContext;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
11+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
12+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
13+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
14+
import org.springframework.cloud.netflix.feign.FeignClient;
15+
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
16+
import org.springframework.context.annotation.Bean;
17+
import org.springframework.core.Ordered;
18+
import org.springframework.http.HttpStatus;
19+
import org.springframework.util.ReflectionUtils;
20+
import org.springframework.web.bind.annotation.PathVariable;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestMethod;
23+
import org.springframework.web.bind.annotation.RestController;
24+
import org.springframework.web.client.RestTemplate;
25+
26+
import javax.servlet.http.HttpServletResponse;
27+
import java.io.IOException;
28+
29+
@EnableCircuitBreaker
30+
@EnableFeignClients
31+
@EnableZuulProxy
32+
@EnableDiscoveryClient
33+
@SpringBootApplication
34+
public class GreetingsClientApplication {
35+
36+
@Bean
37+
@LoadBalanced
38+
RestTemplate restTemplate() {
39+
return new RestTemplate();
40+
}
41+
42+
public static void main(String[] args) {
43+
SpringApplication.run(GreetingsClientApplication.class, args);
44+
}
45+
}
46+
47+
@FeignClient("greetings-service")
48+
interface GreetingsClient {
49+
50+
@RequestMapping(method = RequestMethod.GET, value = "/greetings/{name}")
51+
Greeting greet(@PathVariable("name") String name);
52+
}
53+
54+
@RestController
55+
class GreetingsApiGatewayRestController {
56+
57+
private final GreetingsClient greetingsClient;
58+
59+
@Autowired
60+
public GreetingsApiGatewayRestController(GreetingsClient client) {
61+
this.greetingsClient = client;
62+
}
63+
64+
public String fallback(String name) {
65+
return "ohai!";
66+
}
67+
68+
@HystrixCommand(fallbackMethod = "fallback")
69+
@RequestMapping(method = RequestMethod.GET, value = "/hi/{name}")
70+
String greet(@PathVariable String name) {
71+
return this.greetingsClient.greet(name).getGreeting();
72+
}
73+
}
74+
75+
class Greeting {
76+
private String greeting;
77+
78+
public String getGreeting() {
79+
return greeting;
80+
}
81+
}
82+
83+
//@Component
84+
class RateLimitingZuulFilter extends ZuulFilter {
85+
86+
private final RateLimiter rateLimiter = RateLimiter.create(1.0 / 30.0);
87+
88+
@Override
89+
public String filterType() {
90+
return "pre";
91+
}
92+
93+
@Override
94+
public int filterOrder() {
95+
return Ordered.HIGHEST_PRECEDENCE + 100;
96+
}
97+
98+
@Override
99+
public boolean shouldFilter() {
100+
return true;
101+
}
102+
103+
@Override
104+
public Object run() {
105+
try {
106+
RequestContext currentContext = RequestContext.getCurrentContext();
107+
HttpServletResponse response = currentContext.getResponse();
108+
109+
if (!this.rateLimiter.tryAcquire()) {
110+
response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
111+
response.getWriter().append(HttpStatus.TOO_MANY_REQUESTS.getReasonPhrase());
112+
currentContext.setSendZuulResponse(false);
113+
}
114+
} catch (IOException e) {
115+
ReflectionUtils.rethrowRuntimeException(e);
116+
}
117+
return null;
118+
}
119+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.application.name = greetings-client
2+
server.port=9999
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>livelessons</groupId>
7+
<artifactId>livelessons-edge</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>livelessons-edge-greetings-service</artifactId>
11+
<properties>
12+
<main.basedir>../..</main.basedir>
13+
</properties>
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>org.springframework.cloud</groupId>
18+
<artifactId>spring-cloud-starter-eureka</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-data-jpa</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.h2database</groupId>
31+
<artifactId>h2</artifactId>
32+
<scope>runtime</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.Collections;
9+
import java.util.Map;
10+
import java.util.Optional;
11+
12+
@EnableDiscoveryClient
13+
@SpringBootApplication
14+
public class GreetingsServiceApplication {
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(GreetingsServiceApplication.class, args);
18+
}
19+
}
20+
21+
@RestController
22+
class GreetingsRestController {
23+
24+
@RequestMapping(method = RequestMethod.GET, value = "/greetings/{name}")
25+
Map<String, String> greeting(@PathVariable String name,
26+
@RequestHeader("x-forwarded-host") Optional<String> host,
27+
@RequestHeader("x-forwarded-port") Optional<Integer> port) {
28+
host.ifPresent(h -> System.out.println("host = " + h));
29+
port.ifPresent(p -> System.out.println("port = " + p));
30+
return Collections.singletonMap("greeting", "Hello, " + name + "!");
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name = greetings-service

livelessons-edge/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>livelessons</groupId>
7+
<artifactId>livelessons-parent</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<relativePath>../livelessons-parent</relativePath>
10+
</parent>
11+
<artifactId>livelessons-edge</artifactId>
12+
<packaging>pom</packaging>
13+
<properties>
14+
<main.basedir>..</main.basedir>
15+
</properties>
16+
<modules>
17+
<module>livelessons-edge-eureka-service</module>
18+
<module>livelessons-edge-greetings-client</module>
19+
<module>livelessons-edge-greetings-service</module>
20+
21+
</modules>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.cloud</groupId>
27+
<artifactId>spring-cloud-dependencies</artifactId>
28+
<version>Camden.RELEASE</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
</dependencies>
33+
</dependencyManagement>
34+
35+
</project>

0 commit comments

Comments
 (0)