From 9de76e01e6c4297678e2b050f0f5fc5f7a879f67 Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 6 Oct 2025 19:41:56 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20T05/=E6=90=AD=E5=BB=BAservice-consume?= =?UTF-8?q?r=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + service-consumer/pom.xml | 50 +++++++++++++++++++ .../ServiceConsumerApplication.java | 11 ++++ .../config/RestTemplateConfig.java | 16 ++++++ .../controller/ConsumeController.java | 22 ++++++++ .../controller/HealthController.java | 19 +++++++ .../src/main/resources/bootstrap.yml | 11 ++++ 7 files changed, 130 insertions(+) create mode 100644 service-consumer/pom.xml create mode 100644 service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/ServiceConsumerApplication.java create mode 100644 service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/config/RestTemplateConfig.java create mode 100644 service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/ConsumeController.java create mode 100644 service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/HealthController.java create mode 100644 service-consumer/src/main/resources/bootstrap.yml diff --git a/pom.xml b/pom.xml index 769ea9d..986fcc4 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ eureka-server config-server service-producer + service-consumer diff --git a/service-consumer/pom.xml b/service-consumer/pom.xml new file mode 100644 index 0000000..e9d32a9 --- /dev/null +++ b/service-consumer/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + cn.hezhaohui + spring-cloud-template + 1.0-SNAPSHOT + + + service-consumer + + + 21 + 21 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.cloud + spring-cloud-netflix-eureka-client + + + + org.springframework.cloud + spring-cloud-starter-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + + + + \ No newline at end of file diff --git a/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/ServiceConsumerApplication.java b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/ServiceConsumerApplication.java new file mode 100644 index 0000000..de7ff87 --- /dev/null +++ b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/ServiceConsumerApplication.java @@ -0,0 +1,11 @@ +package cn.hezhaohui.serviceconsumer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ServiceConsumerApplication { + public static void main(String[] args) { + SpringApplication.run(ServiceConsumerApplication.class, args); + } +} diff --git a/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/config/RestTemplateConfig.java b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/config/RestTemplateConfig.java new file mode 100644 index 0000000..f1fec28 --- /dev/null +++ b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/config/RestTemplateConfig.java @@ -0,0 +1,16 @@ +package cn.hezhaohui.serviceconsumer.config; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + + @Bean + @LoadBalanced + public RestTemplate restTemplate () { + return new RestTemplate(); + } +} diff --git a/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/ConsumeController.java b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/ConsumeController.java new file mode 100644 index 0000000..2fac9f5 --- /dev/null +++ b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/ConsumeController.java @@ -0,0 +1,22 @@ +package cn.hezhaohui.serviceconsumer.controller; + +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +@RequestMapping("/api") +public class ConsumeController { + + @Resource + private RestTemplate restTemplate; + + @GetMapping("/print") + public String print() { + String number = restTemplate.getForObject("http://service-producer/api/number", String.class); + return "[Generate]: " + number; + } + +} diff --git a/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/HealthController.java b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/HealthController.java new file mode 100644 index 0000000..80fd405 --- /dev/null +++ b/service-consumer/src/main/java/cn/hezhaohui/serviceconsumer/controller/HealthController.java @@ -0,0 +1,19 @@ +package cn.hezhaohui.serviceconsumer.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +public class HealthController { + + @Value("${test.message}") + private String testMessage; + + @GetMapping("/health") + public String health() { + return "Wow, nice to meet you!" + "\n" + testMessage; + } +} diff --git a/service-consumer/src/main/resources/bootstrap.yml b/service-consumer/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..2a607aa --- /dev/null +++ b/service-consumer/src/main/resources/bootstrap.yml @@ -0,0 +1,11 @@ +spring: + application: + name: service-consumer + cloud: + config: + uri: http://localhost:8888 + fail-fast: true +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file