Skip to content

Commit d97a894

Browse files
Eureka Server and Clients
1 parent 21550cd commit d97a894

14 files changed

Lines changed: 363 additions & 0 deletions

File tree

‎spring-cloud-eureka/pom.xml‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.baeldung.spring.cloud</groupId>
7+
<artifactId>spring-cloud-eureka</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<modules>
10+
<module>spring-cloud-eureka-server</module>
11+
<module>spring-cloud-eureka-client</module>
12+
<module>spring-cloud-eureka-feign-client</module>
13+
</modules>
14+
<packaging>pom</packaging>
15+
16+
<name>Spring Cloud Eureka</name>
17+
<description>Spring Cloud Eureka Server and Sample Clients</description>
18+
19+
<parent>
20+
<groupId>com.baeldung</groupId>
21+
<artifactId>parent-modules</artifactId>
22+
<version>1.0.0-SNAPSHOT</version>
23+
<relativePath>..</relativePath>
24+
</parent>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<build>
31+
<pluginManagement>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.5.1</version>
37+
<configuration>
38+
<source>1.8</source>
39+
<target>1.8</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
<version>1.4.0.RELEASE</version>
46+
</plugin>
47+
</plugins>
48+
</pluginManagement>
49+
</build>
50+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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+
<artifactId>spring-cloud-eureka-client</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring Cloud Eureka Client</name>
11+
<description>Spring Cloud Eureka Sample Client</description>
12+
13+
<parent>
14+
<groupId>com.baeldung.spring.cloud</groupId>
15+
<artifactId>spring-cloud-eureka</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
<relativePath>..</relativePath>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.cloud</groupId>
23+
<artifactId>spring-cloud-starter-eureka</artifactId>
24+
<version>1.1.5.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
<version>1.4.0.RELEASE</version>
30+
</dependency>
31+
</dependencies>
32+
33+
<dependencyManagement>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-parent</artifactId>
38+
<version>Brixton.SR4</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.spring.cloud.eureka.client;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication
10+
@EnableEurekaClient
11+
public class EurekaClientApplication {
12+
public static void main(String[] args) {
13+
SpringApplication.run(EurekaClientApplication.class, args);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.cloud.eureka.client;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
5+
public interface GreetingController {
6+
@RequestMapping("/greeting")
7+
String greeting();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.cloud.eureka.client;
2+
3+
import com.netflix.discovery.EurekaClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Lazy;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class GreetingControllerImpl implements GreetingController {
11+
@Autowired
12+
@Lazy
13+
private EurekaClient eurekaClient;
14+
15+
@Value("${spring.application.name}")
16+
private String appName;
17+
18+
@Override
19+
public String greeting() {
20+
return String.format("Hello from '%s'!", eurekaClient.getApplication(appName).getName());
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
application:
3+
name: spring-cloud-eureka-client
4+
5+
server:
6+
port: 0
7+
8+
eureka:
9+
client:
10+
serviceUrl:
11+
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
12+
instance:
13+
preferIpAddress: true
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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+
<artifactId>spring-cloud-eureka-feign-client</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring Cloud Eureka - Feign Client</name>
11+
<description>Spring Cloud Eureka - Sample Feign Client</description>
12+
13+
<parent>
14+
<groupId>com.baeldung.spring.cloud</groupId>
15+
<artifactId>spring-cloud-eureka</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
<relativePath>..</relativePath>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.baeldung.spring.cloud</groupId>
23+
<artifactId>spring-cloud-eureka-client</artifactId>
24+
<version>1.0.0-SNAPSHOT</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.cloud</groupId>
28+
<artifactId>spring-cloud-starter-feign</artifactId>
29+
<version>1.1.5.RELEASE</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-web</artifactId>
34+
<version>1.4.0.RELEASE</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
39+
<version>1.4.0.RELEASE</version>
40+
</dependency>
41+
</dependencies>
42+
43+
<dependencyManagement>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-starter-parent</artifactId>
48+
<version>Brixton.SR4</version>
49+
<type>pom</type>
50+
<scope>import</scope>
51+
</dependency>
52+
</dependencies>
53+
</dependencyManagement>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.spring.cloud.feign.client;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
8+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
14+
@SpringBootApplication
15+
@EnableEurekaClient
16+
@EnableFeignClients
17+
@Controller
18+
public class FeignClientApplication {
19+
@Autowired
20+
private GreetingClient greetingClient;
21+
22+
public static void main(String[] args) {
23+
SpringApplication.run(FeignClientApplication.class, args);
24+
}
25+
26+
@RequestMapping("/get-greeting")
27+
public String greeting(Model model) {
28+
model.addAttribute("greeting", greetingClient.greeting());
29+
return "show-greeting";
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.cloud.feign.client;
2+
3+
import com.baeldung.spring.cloud.eureka.client.GreetingController;
4+
import org.springframework.cloud.netflix.feign.FeignClient;
5+
6+
@FeignClient("spring-eureka-client")
7+
public interface GreetingClient extends GreetingController {
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring:
2+
application:
3+
name: spring-cloud-eureka-feign-client
4+
5+
server:
6+
port: 8080
7+
8+
eureka:
9+
client:
10+
serviceUrl:
11+
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}

0 commit comments

Comments
 (0)