diff --git a/spring-cloud-eureka/pom.xml b/spring-cloud-eureka/pom.xml new file mode 100644 index 0000000000..86e0354070 --- /dev/null +++ b/spring-cloud-eureka/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-eureka + 1.0.0-SNAPSHOT + + spring-cloud-eureka-server + spring-cloud-eureka-client + spring-cloud-eureka-feign-client + + pom + + Spring Cloud Eureka + Spring Cloud Eureka Server and Sample Clients + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + 1.4.0.RELEASE + + + + + diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/pom.xml b/spring-cloud-eureka/spring-cloud-eureka-client/pom.xml new file mode 100644 index 0000000000..720b49ddc2 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-client/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + spring-cloud-eureka-client + 1.0.0-SNAPSHOT + jar + + Spring Cloud Eureka Client + Spring Cloud Eureka Sample Client + + + com.baeldung.spring.cloud + spring-cloud-eureka + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.cloud + spring-cloud-starter-eureka + 1.1.5.RELEASE + + + org.springframework.boot + spring-boot-starter-web + 1.4.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + Brixton.SR4 + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java new file mode 100644 index 0000000000..e8485b537c --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/EurekaClientApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.eureka.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@EnableEurekaClient +public class EurekaClientApplication { + public static void main(String[] args) { + SpringApplication.run(EurekaClientApplication.class, args); + } +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java new file mode 100644 index 0000000000..33ee2574b7 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingController.java @@ -0,0 +1,8 @@ +package com.baeldung.spring.cloud.eureka.client; + +import org.springframework.web.bind.annotation.RequestMapping; + +public interface GreetingController { + @RequestMapping("/greeting") + String greeting(); +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingControllerImpl.java b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingControllerImpl.java new file mode 100644 index 0000000000..bc04099ae2 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/java/com/baeldung/spring/cloud/eureka/client/GreetingControllerImpl.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.cloud.eureka.client; + +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingControllerImpl implements GreetingController { + @Autowired + @Lazy + private EurekaClient eurekaClient; + + @Value("${spring.application.name}") + private String appName; + + @Override + public String greeting() { + return String.format("Hello from '%s'!", eurekaClient.getApplication(appName).getName()); + } +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml new file mode 100644 index 0000000000..08624aa159 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-client/src/main/resources/application.yml @@ -0,0 +1,13 @@ +spring: + application: + name: spring-cloud-eureka-client + +server: + port: 0 + +eureka: + client: + serviceUrl: + defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} + instance: + preferIpAddress: true \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml b/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml new file mode 100644 index 0000000000..92108a084b --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-feign-client/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + spring-cloud-eureka-feign-client + 1.0.0-SNAPSHOT + jar + + Spring Cloud Eureka - Feign Client + Spring Cloud Eureka - Sample Feign Client + + + com.baeldung.spring.cloud + spring-cloud-eureka + 1.0.0-SNAPSHOT + .. + + + + + com.baeldung.spring.cloud + spring-cloud-eureka-client + 1.0.0-SNAPSHOT + + + org.springframework.cloud + spring-cloud-starter-feign + 1.1.5.RELEASE + + + org.springframework.boot + spring-boot-starter-web + 1.4.0.RELEASE + + + org.springframework.boot + spring-boot-starter-thymeleaf + 1.4.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + Brixton.SR4 + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java new file mode 100644 index 0000000000..e4f47286b2 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/FeignClientApplication.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.cloud.feign.client; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@SpringBootApplication +@EnableEurekaClient +@EnableFeignClients +@Controller +public class FeignClientApplication { + @Autowired + private GreetingClient greetingClient; + + public static void main(String[] args) { + SpringApplication.run(FeignClientApplication.class, args); + } + + @RequestMapping("/get-greeting") + public String greeting(Model model) { + model.addAttribute("greeting", greetingClient.greeting()); + return "show-greeting"; + } +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java new file mode 100644 index 0000000000..9cb5de26cc --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/java/com/baeldung/spring/cloud/feign/client/GreetingClient.java @@ -0,0 +1,8 @@ +package com.baeldung.spring.cloud.feign.client; + +import com.baeldung.spring.cloud.eureka.client.GreetingController; +import org.springframework.cloud.netflix.feign.FeignClient; + +@FeignClient("spring-eureka-client") +public interface GreetingClient extends GreetingController { +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml new file mode 100644 index 0000000000..d053ef7a7e --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/application.yml @@ -0,0 +1,11 @@ +spring: + application: + name: spring-cloud-eureka-feign-client + +server: + port: 8080 + +eureka: + client: + serviceUrl: + defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/show-greeting.html b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/show-greeting.html new file mode 100644 index 0000000000..42cdadb487 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-feign-client/src/main/resources/templates/show-greeting.html @@ -0,0 +1,9 @@ + + + + Greeting Page + + +

+ + \ No newline at end of file diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/pom.xml b/spring-cloud-eureka/spring-cloud-eureka-server/pom.xml new file mode 100644 index 0000000000..f4d655f708 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-server/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + spring-cloud-eureka-server + 1.0.0-SNAPSHOT + jar + + Spring Cloud Eureka Server + Spring Cloud Eureka Server Demo + + + com.baeldung.spring.cloud + spring-cloud-eureka + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.cloud + spring-cloud-starter-eureka-server + 1.1.5.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + Brixton.SR4 + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java b/spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java new file mode 100644 index 0000000000..d55145448d --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-server/src/main/java/com/baeldung/spring/cloud/eureka/server/EurekaServerApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.cloud.eureka.server; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class EurekaServerApplication { + public static void main(String[] args) { + SpringApplication.run(EurekaServerApplication.class, args); + } +} diff --git a/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml b/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml new file mode 100644 index 0000000000..49c3179bb5 --- /dev/null +++ b/spring-cloud-eureka/spring-cloud-eureka-server/src/main/resources/application.yml @@ -0,0 +1,7 @@ +server: + port: 8761 + +eureka: + client: + registerWithEureka: false + fetchRegistry: false \ No newline at end of file