Update: T05/搭建service-consumer模块

This commit is contained in:
2025-10-06 19:41:56 +08:00
parent 6d0967f2df
commit 9de76e01e6
7 changed files with 130 additions and 0 deletions
+1
View File
@@ -14,6 +14,7 @@
<module>eureka-server</module> <module>eureka-server</module>
<module>config-server</module> <module>config-server</module>
<module>service-producer</module> <module>service-producer</module>
<module>service-consumer</module>
</modules> </modules>
<properties> <properties>
+50
View File
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hezhaohui</groupId>
<artifactId>spring-cloud-template</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-consumer</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
@@ -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);
}
}
@@ -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();
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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