Skip to content

Commit e827336

Browse files
joshlongphilwebb
authored andcommitted
Lesson 8: Choreographing Microservices
1 parent 398ef2b commit e827336

File tree

55 files changed

+1424
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1424
-0
lines changed

livelessons-choreography/README.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:compat-mode:
2+
= Lesson 8: Choreographing Microservices
3+
4+
_Understand how to build robust and failure-tolerant Microservices._
5+
6+
7+
8+
This section demonstrates:
9+
10+
- how to handle service registration using Eureka (though Consul is also supported).
11+
- how to use Ribbon client-side load-balancing to dispatch calls in various ways using `DiscoveryClient`, using a blessed `RestTemplate` instance, and using the Feign & Spring MVC integration. Start everything up and then run `PassportServiceApplication` to see this working.
12+
- how to use Hystrix and the Hystrix dashboard to manage problematic service-to-service calls. You can see this in action by now disabling the `PhotoServiceApplication` (if you're in IntelliJ, remember to click the 'Exit' button which is the fifth one down from the top left - an arrow pointing to a door, under the camera icon and _not_ the 'Stop' or 'Pause' ones) and then re-running the `PassportServiceApplication`.
13+
- How to use Zuul to expose and aggregate backend microservices via an edge gateway.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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-choreography</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>livelessons-choreography-bookmark-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</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.h2database</groupId>
29+
<artifactId>h2</artifactId>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package demo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Bookmark {
9+
10+
@Id
11+
@GeneratedValue
12+
private Long id;
13+
14+
private String userId;
15+
16+
private String href;
17+
18+
private String description;
19+
20+
private String label;
21+
22+
Bookmark() {
23+
}
24+
25+
public Bookmark(String userId, String href, String description, String label) {
26+
this.userId = userId;
27+
this.href = href;
28+
this.description = description;
29+
this.label = label;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public String getUserId() {
37+
return userId;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public String getHref() {
45+
return href;
46+
}
47+
48+
public String getLabel() {
49+
return label;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package demo;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
8+
9+
List<Bookmark> findByUserId(String userId);
10+
11+
Bookmark findByUserIdAndId(String userId, Long id);
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package demo;
2+
3+
import java.util.Collection;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("/{userId}/bookmarks")
14+
public class BookmarkRestController {
15+
16+
@Autowired
17+
private BookmarkRepository bookmarkRepository;
18+
19+
@RequestMapping(method = RequestMethod.GET)
20+
public Collection<Bookmark> getBookmarks(@PathVariable String userId) {
21+
return this.bookmarkRepository.findByUserId(userId);
22+
}
23+
24+
@RequestMapping(value = "/{bookmarkId}", method = RequestMethod.GET)
25+
public Bookmark getBookmark(@PathVariable String userId,
26+
@PathVariable Long bookmarkId) {
27+
return this.bookmarkRepository.findByUserIdAndId(userId, bookmarkId);
28+
}
29+
30+
@RequestMapping(method = RequestMethod.POST)
31+
public Bookmark createBookmark(@PathVariable String userId,
32+
@RequestBody Bookmark bookmark) {
33+
Bookmark bookmarkInstance = new Bookmark(userId, bookmark.getHref(),
34+
bookmark.getDescription(), bookmark.getLabel());
35+
return this.bookmarkRepository.save(bookmarkInstance);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package demo;
2+
3+
import java.util.Arrays;
4+
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
@EnableEurekaClient
13+
public class BookmarkServiceApplication {
14+
15+
@Bean
16+
public CommandLineRunner init(BookmarkRepository bookmarkRepository) {
17+
return args -> {
18+
bookmarkRepository.deleteAll();
19+
20+
Arrays.asList("pwebb", "jlong")
21+
.forEach(n -> bookmarkRepository.save(new Bookmark(n,
22+
String.format("http://some-other-host-for-%s.com", n),
23+
String.format("A description for %s's link", n), n)));
24+
};
25+
}
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(BookmarkServiceApplication.class, args);
29+
}
30+
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
server:
3+
port: ${PORT:0}
4+
5+
spring:
6+
jpa:
7+
generate-ddl: true
8+
9+
10+
eureka:
11+
client:
12+
serviceUrl:
13+
defaultZone: ${vcap.services.eureka-service.credentials.uri:http://127.0.0.1:8761}/eureka/
14+
15+
---
16+
spring:
17+
profiles: cloud
18+
eureka:
19+
instance:
20+
hostname: ${APPLICATION_DOMAIN}
21+
nonSecurePort: 80
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring:
2+
application:
3+
name: bookmark-service
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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-choreography</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>livelessons-choreography-contact-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</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.h2database</groupId>
29+
<artifactId>h2</artifactId>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package demo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
class Contact {
9+
10+
@Id
11+
@GeneratedValue
12+
private Long id;
13+
14+
private String userId;
15+
16+
private String firstName;
17+
18+
private String lastName;
19+
20+
private String email;
21+
22+
private String relationship;
23+
24+
public Contact() {
25+
}
26+
27+
public Contact(String userId, String firstName, String lastName, String email,
28+
String relationship) {
29+
this.userId = userId;
30+
this.firstName = firstName;
31+
this.lastName = lastName;
32+
this.email = email;
33+
this.relationship = relationship;
34+
}
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public String getUserId() {
41+
return userId;
42+
}
43+
44+
public String getFirstName() {
45+
return firstName;
46+
}
47+
48+
public String getLastName() {
49+
return lastName;
50+
}
51+
52+
public String getEmail() {
53+
return email;
54+
}
55+
56+
public String getRelationship() {
57+
return relationship;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)