代码提交
This commit is contained in:
53
spring-cloud-demo/user-service/pom.xml
Normal file
53
spring-cloud-demo/user-service/pom.xml
Normal file
@@ -0,0 +1,53 @@
|
||||
<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>com.mangmang</groupId>
|
||||
<artifactId>spring-cloud-demo</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<name>user-service</name>
|
||||
<artifactId>user-service</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-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.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.mangmang;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
|
||||
/**
|
||||
* 用户服务
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
public class UserServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.mangmang.controller;
|
||||
|
||||
import com.mangmang.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
@Value("${server.port}")
|
||||
private String port;
|
||||
|
||||
private final List<User> users = Arrays.asList(
|
||||
new User(1L, "张三", "zhangsan@example.com"),
|
||||
new User(2L, "李四", "lisi@example.com"),
|
||||
new User(3L, "王五", "wangwu@example.com")
|
||||
);
|
||||
|
||||
@GetMapping
|
||||
public List<User> getAllUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public User getUserById(@PathVariable Long id) {
|
||||
return users.stream()
|
||||
.filter(user -> user.getId().equals(id))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
||||
}
|
||||
|
||||
@GetMapping("/port")
|
||||
public String getPort() {
|
||||
return "User service running on port: " + port;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mangmang.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
# server: 配置服务器相关的设置
|
||||
server:
|
||||
# port: 指定服务器监听的端口号,默认为8081
|
||||
port: 8081
|
||||
|
||||
# eureka: 配置Eureka客户端相关的设置
|
||||
eureka:
|
||||
client:
|
||||
serviceUrl:
|
||||
# defaultZone: 指定Eureka服务器的默认注册地址,默认为http://localhost:8761/eureka/
|
||||
defaultZone: http://localhost:8761/eureka/
|
||||
|
||||
# management: 配置Spring Boot Actuator相关的设置
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# include: 指定暴露的Actuator端点,*表示暴露所有端点
|
||||
include: "*"
|
||||
@@ -0,0 +1,19 @@
|
||||
# Spring 应用配置
|
||||
spring:
|
||||
# 应用名称配置
|
||||
application:
|
||||
# 设置应用的名称为 user-service
|
||||
name: user-service
|
||||
# Spring Cloud 配置
|
||||
cloud:
|
||||
config:
|
||||
# 配置服务发现相关设置
|
||||
discovery:
|
||||
# 启用配置服务发现功能
|
||||
enabled: true
|
||||
# 指定配置服务的服务ID为 config-server
|
||||
service-id: config-server
|
||||
# 启用快速失败机制,当配置服务不可用时,应用将快速失败而不是等待
|
||||
fail-fast: true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user