Skip to content

Commit a96c68e

Browse files
committed
Add message-service demo application
1 parent c4fdb38 commit a96c68e

File tree

11 files changed

+242
-1
lines changed

11 files changed

+242
-1
lines changed

eureka-server/src/main/resources/application.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
server:
33
port: 8761
44

5-
spring.cloud.ser
5+
eureka:
6+
client:
7+
register-with-eureka: false
8+
fetch-registry: false
9+
# set the info about where this eureka server is running useful to set when
10+
# running eureka across availability zones and regions
11+
datacenter: laptop
12+
environment: dev

message-service/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
6+
<groupId>com.example</groupId>
7+
<artifactId>message-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>message-service</name>
12+
<description>Demo project showing how to register a service with Eureka</description>
13+
14+
<parent>
15+
<groupId>com.example</groupId>
16+
<artifactId>spring-cloud-eureka-basics</artifactId>
17+
<version>0.0.1-SNAPSHOT</version>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.cloud</groupId>
23+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.h2database</groupId>
35+
<artifactId>h2</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.flywaydb</groupId>
39+
<artifactId>flyway-core</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-actuator</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
<plugin>
59+
<groupId>pl.project13.maven</groupId>
60+
<artifactId>git-commit-id-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.messageservice;
2+
3+
import org.springframework.http.HttpEntity;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
@RestController
14+
public class MessageController {
15+
16+
private final QuoteRepository quoteRepository;
17+
18+
public MessageController(QuoteRepository quoteRepository) {
19+
this.quoteRepository = quoteRepository;
20+
}
21+
22+
@GetMapping("/")
23+
public Quote radomQuote()
24+
{
25+
return quoteRepository.findRandomQuote();
26+
}
27+
28+
@GetMapping("/quotes")
29+
public List<Quote> getAll()
30+
{
31+
return quoteRepository.findAll();
32+
}
33+
34+
@GetMapping("/quotes/{id}")
35+
public ResponseEntity<Quote> getQuote(@PathVariable("id") Integer id) {
36+
Optional<Quote> quote = quoteRepository.findById(id);
37+
if (quote.isPresent()) {
38+
return new ResponseEntity<>(quote.get(), HttpStatus.OK);
39+
} else {
40+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
41+
}
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.messageservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
7+
@EnableDiscoveryClient
8+
@SpringBootApplication
9+
public class MessageServiceApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(MessageServiceApplication.class, args);
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.messageservice;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
8+
@Entity
9+
@Table(name = "quotes")
10+
public class Quote
11+
{
12+
@Id
13+
@Column(name = "id")
14+
private Integer id;
15+
16+
@Column(name="quote")
17+
private String quote;
18+
19+
@Column(name="author")
20+
private String author;
21+
22+
public Integer getId() {
23+
return id;
24+
}
25+
26+
public void setId(Integer id) {
27+
this.id = id;
28+
}
29+
30+
public String getQuote() {
31+
return quote;
32+
}
33+
34+
public void setQuote(String quote) {
35+
this.quote = quote;
36+
}
37+
38+
public String getAuthor() {
39+
return author;
40+
}
41+
42+
public void setAuthor(String author) {
43+
this.author = author;
44+
}
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.messageservice;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
6+
public interface QuoteRepository extends JpaRepository<Quote,Integer> {
7+
8+
@Query( nativeQuery = true, value =
9+
"SELECT id,quote,author FROM quotes ORDER BY RANDOM() LIMIT 1")
10+
Quote findRandomQuote();
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Set this service to start on a random port number that is not being
2+
# used, this will allow us to launch multiple versions of the service
3+
# and register them with Eureka for discovery
4+
server:
5+
port: 0
6+
7+
eureka:
8+
client:
9+
fetch-registry: false # This service does not call any other service so no need to cache list of registered services
10+
register-with-eureka: true # This service should register itself in eureka
11+
instance:
12+
instance-id: ${spring.application.name}:${random.int} # unique id for app instances registered with eureka
13+
14+
# expose all the spring boot actuators good for demos.
15+
# NEVER DO THIS in production
16+
management:
17+
endpoints:
18+
web:
19+
exposure:
20+
include: "*"
21+
endpoint:
22+
health:
23+
show-details: always
24+
25+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring:
2+
application:
3+
name: message-service
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE quotes(
2+
id INTEGER PRIMARY KEY,
3+
quote VARCHAR(1024),
4+
author VARCHAR(256)
5+
);
6+
7+
INSERT INTO quotes (id,quote,author) VALUES (1,'Never, never, never give up','Winston Churchill');
8+
INSERT INTO quotes (id,quote,author) VALUES (2,'While there''s life, there''s hope','Marcus Tullius Cicero');
9+
INSERT INTO quotes (id,quote,author) VALUES (3,'Failure is success in progress','Anonymous');
10+
INSERT INTO quotes (id,quote,author) VALUES (4,'Success demands singleness of purpose','Vincent Lombardi');
11+
INSERT INTO quotes (id,quote,author) VALUES (5,'The shortest answer is doing','Lord Herbert');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.messageservice;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class MessageServiceApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)