SpringCloud项目开发学习入门指南
本文将详细介绍如何进行Spring Cloud项目开发学习,涵盖环境搭建、快速搭建第一个项目、服务发现与配置中心、负载均衡与路由、服务容错与断路器、安全性模块集成等内容。
Spring Cloud项目开发学习入门指南 Spring Cloud简介与环境搭建Spring Cloud是什么
Spring Cloud是一组框架的集合,它基于Spring Boot提供了一整套微服务解决方案,让开发人员能够快速构建分布式系统中的各个组件。Spring Cloud包括服务发现、配置中心、负载均衡、断路器、服务网关、数据访问、集群管理等,每个组件都是一个独立的子项目。
开发环境准备
开发Spring Cloud项目需要以下环境:
- Java开发环境(推荐Java 8及以上版本)
- IDE(推荐IntelliJ IDEA或Eclipse)
- Maven或Gradle构建工具
- Git版本控制工具(可选)
安装Java
确保安装了Java开发环境。可以通过以下命令检查Java版本:
java -version
如果未安装,请下载并安装Java 8或更高版本。
安装IDE
安装IntelliJ IDEA或Eclipse。以下是安装IntelliJ IDEA的步骤:
- 访问官网下载页面:https://www.jetbrains.com/idea/download/
- 下载并安装IntelliJ IDEA。
- 打开IntelliJ IDEA,创建一个新的Maven项目。
配置Maven
确保Maven已经安装。可以通过以下命令检查Maven版本:
mvn -version
如果未安装,请访问官网下载并安装Maven:https://maven.apache.org/download.cgi
配置Git
如果使用Git进行版本控制,请安装Git并配置好用户信息:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
快速搭建第一个Spring Cloud项目
- 创建一个新的Maven项目,命名为
spring-cloud-starter
。 - 在
pom.xml
中添加Spring Cloud的依赖。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 在主类
Application.java
中添加@EnableEurekaClient
注解来注册服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 在
application.yml
配置文件中配置服务注册中心。
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 启动服务注册中心。创建一个新的Spring Boot应用,在
pom.xml
中添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
并在主类EurekaServerApplication.java
中添加@EnableEurekaServer
注解来启动服务注册中心。
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);
}
}
- 运行服务注册中心和客户端应用,检查服务注册中心是否能成功注册客户端服务。
服务发现概述
服务发现是微服务架构中的一个关键组件,它允许服务实例之间动态地发现和通信。Spring Cloud提供了多种服务发现的实现方式,如Eureka和Consul。
Eureka服务注册与发现
Eureka是Netflix开源的一个基于REST的服务注册与发现框架,它提供了服务注册、服务发现、健康检查等功能。Spring Cloud集成了Eureka,使其可以被Spring Boot应用轻松使用。
Eureka服务注册与发现的实现
- 在服务提供者中添加
@EnableEurekaClient
注解,表示该服务提供者会将自身注册到Eureka服务注册中心。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
- 在服务消费者中添加
@EnableDiscoveryClient
注解,表示该服务消费者会从Eureka服务注册中心获取服务提供者的地址列表。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
- 在服务提供者中实现服务提供接口。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping("/hello")
public String hello() {
return "Hello, Eureka!";
}
}
- 在服务消费者中实现服务调用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadBalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/call-service")
public String callService() {
ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
// 这里可以使用RestTemplate等工具来调用服务
return "Calling service at " + url;
}
}
Config配置中心的使用
Spring Cloud Config提供了集中式的配置管理功能,可以将配置文件集中存放到Git或SVN等版本控制系统中,并通过Spring Cloud Config Server提供相应的配置访问接口。
使用GitHub作为配置中心
-
在GitHub上创建一个仓库,例如
spring-cloud-config-repo
,并将配置文件存放在/config
目录下。 - 在Spring Cloud Config Server中配置GitHub仓库地址。
spring:
cloud:
config:
server:
git:
uri: https://github.com/your-account/spring-cloud-config-repo
search-paths: config
- 创建Config Server应用,添加相关依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
.<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 配置
@EnableConfigServer
注解来启动配置服务。
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);
}
}
- 在客户端应用中引入
spring-cloud-starter-config
依赖,并配置bootstrap.yml
来获取配置信息。
spring:
application:
name: config-client
config:
server:
git:
uri: https://github.com/your-account/spring-cloud-config-repo
search-paths: config
username: your-github-username
password: your-github-password
配置中心的使用示例
- 在配置中心的GitHub仓库中创建一个配置文件
application.yml
。
spring:
application:
name: config-client
- 在客户端应用中使用
@Value
注解来获取配置信息。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
@Value("${spring.application.name}")
private String appName;
@GetMapping("/app-name")
public String getAppName() {
return appName;
}
}
负载均衡与路由
负载均衡概念
负载均衡是一种通过将网络或应用流量分发到多台后端服务上来提升可用性和性能的技术。在微服务架构中,负载均衡可以确保请求均匀地分布到各个服务实例上,提高系统稳定性和响应速度。
使用Ribbon实现客户端负载均衡
Ribbon是Netflix开发的客户端负载均衡器和服务调用库,它内置了多种负载均衡策略,如轮询、随机、响应时间权重等。
使用Ribbon实现服务调用
- 创建一个简单的服务提供者应用
ServiceProviderApplication
,并实现一个ProviderController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping("/hello")
public String hello() {
return "Hello, Ribbon!";
}
}
- 创建一个服务消费者应用
ServiceConsumerApplication
,并在控制器中使用Ribbon进行服务调用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/call-service")
public String callService() {
ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
return "Calling service at " + url;
}
}
- 在服务消费者应用的
application.yml
配置文件中配置服务提供者的地址列表。
spring:
application:
name: service-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 运行服务提供者和消费者应用,检查服务调用是否能成功执行。
Zuul作为API网关的基础使用
Zuul是Netflix开发的基于Java的服务路由和过滤器的框架,它提供了动态路由、过滤器等功能,常用于构建微服务的API网关。
使用Zuul构建API网关
- 创建一个新的Spring Boot应用,并添加
spring-cloud-starter-netflix-zuul
依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
- 配置
@EnableZuulProxy
注解来启动API网关。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
- 在
application.yml
配置文件中配置服务发现地址。
spring:
application:
name: api-gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
service-provider:
path: /service-provider/**
serviceId: service-provider
- 运行API网关应用,检查配置的路由是否能正常使用。
服务容错的重要性
在分布式系统中,服务之间的通信可能会因为网络延迟、服务超时等原因导致请求失败。服务容错是指系统能够容忍局部故障并继续提供服务的能力。服务容错对于构建高可用、可伸缩的微服务架构至关重要。
Hystrix断路器的基本原理与使用
Hystrix是Netflix开源的一款服务容错与断路器组件,它通过隔离服务调用的依赖性来提高系统的可用性和响应性。Hystrix可以快速失败并恢复服务调用,避免因为某个服务的故障影响整个系统。
使用Hystrix进行服务调用
- 创建一个新的服务提供者应用
ServiceProviderApplication
,并实现一个ProviderController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping("/hello")
public String hello() {
return "Hello, Hystrix!";
}
}
- 创建一个新的服务消费者应用
ServiceConsumerApplication
,并在控制器中使用Hystrix进行服务调用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableCircuitBreaker
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/call-service")
public String callService() {
ServiceInstance serviceInstance = loadBalancerClient.choose("service-provider");
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/hello";
return "Calling service at " + url;
}
}
- 在服务消费者应用的
application.yml
配置文件中配置服务提供者的地址列表。
spring:
application:
name: service-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 运行服务提供者和消费者应用,检查服务调用是否能成功执行。
使用Feign简化网络请求
Feign是Netflix开源的一个声明式、模板化的HTTP客户端,它提供了丰富的注解来简化网络请求的实现。
使用Feign进行服务调用
- 在服务提供者应用中创建一个Feign客户端接口。
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "service-provider")
public interface ServiceProviderClient {
@GetMapping("/hello")
String hello();
}
- 在服务消费者应用中注入并使用Feign客户端接口。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableFeignClients
public class ConsumerController {
@Autowired
private ServiceProviderClient serviceProviderClient;
@GetMapping("/call-service")
public String callService() {
return serviceProviderClient.hello();
}
}
- 运行服务提供者和消费者应用,检查服务调用是否能成功执行。
微服务中的安全性问题
在微服务架构中,安全性是一个关键问题,包括身份验证、授权、数据保护、通信加密等。Spring Cloud提供了多种安全组件来帮助开发人员构建安全的微服务应用。
OAuth2认证机制简介
OAuth2是一种开放标准协议,它允许第三方应用在获得用户授权的情况下访问用户资源。OAuth2被广泛应用于微服务的安全认证中。
使用OAuth2实现安全认证
- 创建一个新的安全认证服务
OAuth2ServerApplication
,并实现一个OAuth2授权服务。
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtTokenEnhancer;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableResourceServer
public class OAuth2ServerConfiguration {
@Autowired
private JwtTokenStore tokenStore;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtTokenEnhancer enhancer = new JwtTokenEnhancer();
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setTokenEnhancer(enhancer);
return converter;
}
}
- 在
application.yml
配置文件中配置OAuth2服务。
spring:
security:
oauth2:
resource:
tokenStore: jwt
jwt:
tokenEnhancer:
key: secret-key
tokenEnhancer: JwtTokenEnhancer
accessTokenValiditySeconds: 3600
refreshTokenValiditySeconds: 86400
- 在服务提供者和消费者应用中添加
spring-security-oauth2
依赖,实现认证和授权。
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>
- 在服务提供者和消费者应用中配置
@EnableResourceServer
注解来启动OAuth2资源服务器。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@SpringBootApplication
@EnableResourceServer
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
如何在Spring Cloud中集成安全性模块
Spring Cloud提供了多种安全模块来帮助开发人员构建安全的微服务应用,如Spring Security、OAuth2、JWT等。
在Spring Cloud中集成Spring Security
- 添加
spring-boot-starter-security
依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 配置
@EnableWebSecurity
注解来启动Spring Security。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/oauth/authorize").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
- 在服务提供者和消费者应用中配置安全认证。
spring:
security:
oauth2:
client:
clientId: client-id
clientSecret: client-secret
accessTokenUri: http://localhost:8081/oauth/token
userAuthorizationUri: http://localhost:8081/oauth/authorize
scope: read
autoApprove: true
- 运行安全认证服务、服务提供者和消费者应用,检查认证和授权是否能正常工作。
案例需求分析
本案例将构建一个简单电商微服务系统,包括用户服务、商品服务、订单服务等模块。系统需要实现用户注册、登录、商品浏览、下单、支付等功能。
分模块实现微服务功能
用户服务
用户服务实现用户注册、登录、获取用户信息等功能。
- 创建一个新的用户服务应用
UserServiceApplication
,并实现一个UserController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PostMapping("/register")
public String register(@RequestBody User user) {
// 实现用户注册逻辑
return "User registered successfully!";
}
@PostMapping("/login")
public String login(@RequestBody User user) {
// 实现用户登录逻辑
return "User logged in successfully!";
}
@GetMapping("/user")
public User getUser() {
// 实现获取用户信息逻辑
return new User();
}
}
- 在
UserServiceApplication
主类中使用@EnableDiscoveryClient
注解来注册服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
商品服务
商品服务实现商品信息查询、商品添加、商品删除等功能。
- 创建一个新的商品服务应用
ProductServiceApplication
,并实现一个ProductController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@GetMapping("/products")
public List<Product> getProducts() {
// 实现查询商品信息逻辑
return new ArrayList<>();
}
@PostMapping("/products")
public Product addProduct(@RequestBody Product product) {
// 实现添加商品逻辑
return product;
}
@PostMapping("/products/{id}")
public void deleteProduct(@PathVariable Integer id) {
// 实现删除商品逻辑
}
}
- 在
ProductServiceApplication
主类中使用@EnableDiscoveryClient
注解来注册服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
订单服务
订单服务实现订单创建、订单查询、订单支付等功能。
- 创建一个新的订单服务应用
OrderServiceApplication
,并实现一个OrderController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@PostMapping("/orders")
public Order createOrder(@RequestBody Order order) {
// 实现创建订单逻辑
return order;
}
@GetMapping("/orders/{id}")
public Order getOrder(@PathVariable Integer id) {
// 实现查询订单信息逻辑
return new Order();
}
@PostMapping("/orders/{id}/pay")
public void pay(@PathVariable Integer id) {
// 实现订单支付逻辑
}
}
- 在
OrderServiceApplication
主类中使用@EnableDiscoveryClient
注解来注册服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
集成测试与部署
集成测试
集成测试主要验证各个微服务之间的交互是否能正常工作。
- 创建一个测试应用
IntegrationTestApplication
,并实现一个IntegrationTestController
控制器。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IntegrationTestController {
@GetMapping("/test")
public String test() {
// 实现测试逻辑
return "Integration test passed!";
}
}
- 使用Feign或RestTemplate等工具进行服务调用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IntegrationTestController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance serviceInstance = loadBalancerClient.choose("user-service");
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user";
// 实现服务调用逻辑
return "Integration test passed!";
}
}
部署
部署微服务应用时,可以使用Docker、Kubernetes等工具进行容器化部署。
- 将各个微服务应用打包成可执行的JAR文件。
mvn clean package
- 使用Docker创建镜像并运行容器。
docker build -t user-service .
docker run -p 8081:8081 -d user-service
- 使用Kubernetes创建部署和副本集。
apiVersion: apps/v1
kind: Deployment
metadata:
name: user-service
spec:
replicas: 1
selector:
matchLabels:
app: user-service
template:
metadata:
labels:
app: user-service
spec:
containers:
- name: user-service
image: user-service:latest
ports:
- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
name: user-service
spec:
selector:
app: user-service
ports:
- protocol: TCP
port: 80
targetPort: 8081
- 运行部署和副本集。
kubectl apply -f deployment.yaml
总结
通过本指南的学习,你已经掌握了Spring Cloud的基本概念、服务发现与配置中心、负载均衡与路由、服务容错与断路器、微服务的安全性等知识。希望你在实际项目开发中能够灵活运用这些知识,构建出高效、稳定、可维护的微服务系统。
共同学习,写下你的评论
评论加载中...
作者其他优质文章