在去年遭遇失业危机的时候,在一些JD上看到了“熟悉SpringCloud开发”这么一条要求;然后最近在新岗位上也遇到了类似的开发需求,就是在一个已有的工作平台上新增几个微服务,在聊怎么集成实现的时候,被同事问了一句“你知道SpringCloud开发怎么搞么?”我回答“听说过,但一直用的SpringBoot+Docker,没有SpringCloud的经验”,同事说“没关系,差别不大”。
请先给出一个SpringBoot的demo,再step by step地给出一份转SpringCloud的教程
下面是DeepSeek的答案:
第一步:创建一个简单的 Spring Boot 应用
1. 创建 Spring Boot 项目
使用 Spring Initializr 创建一个 Spring Boot 项目,选择以下依赖:
Spring Web
Spring Boot Actuator
生成项目并导入到你的 IDE 中。
2. 编写一个简单的 REST 接口
在项目中创建一个 HelloController 类,提供一个简单的 REST 接口:
package com.example.demo;
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 HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring Boot!";
}
}
3. 配置应用
在 application.properties 或 application.yml 中配置应用端口:
server.port=8080
spring.application.name=springboot-demo
4. 运行应用
运行项目,访问 http://localhost:8080/api/hello,你会看到返回的字符串:
Hello from Spring Boot!
第二步:将 Spring Boot 应用改造为 Spring Cloud 微服务
接下来,我们将这个 Spring Boot 应用改造为 Spring Cloud 微服务,使用以下组件:
Eureka Server:服务注册与发现
Feign Client:服务间通信
Spring Cloud Config:集中配置管理
Step 1: 创建 Eureka Server
1. 创建 Eureka Server 项目
使用 Spring Initializr 创建一个新的 Spring Boot 项目,选择以下依赖:
- Eureka Server
2. 启用 Eureka Server
在启动类上添加 @EnableEurekaServer 注解:
package com.example.eurekaserver;
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);
}
}
3. 配置 Eureka Server
在 application.yml 中配置 Eureka Server:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4. 运行 Eureka Server
启动 Eureka Server,访问 http://localhost:8761,你会看到 Eureka 仪表盘。
Step 2: 将 Spring Boot 应用注册到 Eureka
1. 添加 Eureka Client 依赖
在原来的 Spring Boot 项目中,添加 Eureka Client 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. 启用 Eureka Client
在启动类上添加 @EnableEurekaClient 注解:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3. 配置 Eureka Client
在application.yml 中配置 Eureka Server 地址:
server:
port: 8080
spring:
application:
name: springboot-demo
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4. 重启应用
重启 Spring Boot 应用,它将会注册到 Eureka Server。你可以在 Eureka 仪表盘中看到 springboot-demo服务。
Step 3: 使用 Feign 实现服务间调用
1. 创建另一个 Spring Boot 服务
创建一个新的 Spring Boot 项目,命名为 service-client,选择以下依赖:
Eureka Client
OpenFeign
2. 编写 Feign Client
在 service-client 项目中,创建一个 Feign Client 接口,调用 springboot-demo 服务的 /api/hello 接口:
package com.example.serviceclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "springboot-demo")
public interface HelloClient {
@GetMapping("/api/hello")
String callHello();
}
3. 编写 Controller
在 service-client 项目中,创建一个 Controller,使用 Feign Client 调用 springboot-demo 服务:
package com.example.serviceclient;
import org.springframework.beans.factory.annotation.Autowired;
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 ClientController {
@Autowired
private HelloClient helloClient;
@GetMapping("/call-hello")
public String callHello() {
return helloClient.callHello();
}
}
4. 配置 service-client
在 application.yml 中配置 Eureka Server 地址和应用名称:
server:
port: 8081
spring:
application:
name: service-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
5. 运行 service-client
启动 service-client 项目,访问 http://localhost:8081/api/call-hello,你会看到返回的结果:
Hello from Spring Boot!
Step 4: 使用 Spring Cloud Config 管理配置
1. 创建 Config Server
使用 Spring Initializr 创建一个新的 Spring Boot 项目,选择以下依赖:
- Config Server
2. 启用 Config Server
在启动类上添加 @EnableConfigServer 注解:
package com.example.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3. 配置 Config Server
在 application.yml 中配置 Git 仓库地址:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/your-repo/config-repo.git
4. 将 Spring Boot 应用配置为 Config Client
在 springboot-demo 项目中,添加 Config Client 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
5. 配置 Config Client
在 bootstrap.yml 中配置 Config Server 地址:
spring:
application:
name: springboot-demo
cloud:
config:
uri: http://localhost:8888
6. 将配置移动到 Git 仓库
在 Git 仓库中创建 springboot-demo.yml 文件,将配置移动到该文件中。
总结
通过以上步骤,你已经将一个简单的 Spring Boot 应用改造为 Spring Cloud 微服务架构:
1.使用 Eureka 实现服务注册与发现。
2.使用 Feign 实现服务间通信。
3.使用 Spring Cloud Config 实现集中配置管理。
你可以根据需要扩展这个架构,例如添加 Hystrix 熔断器、Zuul API 网关等组件。