The rise of microservices architecture has changed the way developers build and deploy applications. Spring Cloud is part of the Spring ecosystem designed to simplify the complexity of developing and managing microservices. In this comprehensive guide, we will explore Spring Cloud and its capabilities and demonstrate its capabilities by building a simple microservices application.
Spring Cloud is a set of tools and libraries that provide solutions to common patterns and challenges in distributed systems, such as configuration management, service discovery, circuit breakers, and distributed track. It is built on Spring Boot and can easily create scalable, fault-tolerant microservices.
In this example, we will create a simple microservice An application that contains two services: auser-service
and an order-service
. We will also use Spring Cloud Config and Eureka for centralized configuration and service discovery.
Make sure you have the following software installed on your computer:
<!-- maven --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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-netflix-eureka-server</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-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
or
##
//Gradle implementation 'org.springframework.cloud:spring-cloud-config-server' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' implementation 'org.springframework.cloud:spring-cloud-starter-config' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org.springframework.boot:spring-boot-starter-web'
Config Server and
Eureka Discovery dependencies. Name the projectconfig-server
.
application.yml file:
通过将以下注释添加到您的主类中来启用 和 使用Spring Initializr创建一个新的 Spring Boot 项目并添加 将以下属性添加到您的
通过将以下注释添加到主类来启用 Eureka Server: 使用 Spring Initializr 创建一个新的 Spring Boot 项目并添加 将以下属性添加到您的
创建一个简单 使用 Spring Initializr 创建一个新的 Spring Boot 项目并添加 将以下属性添加到您的
创建一个简单 按以下顺序启动 您现在可以访问用户服务 在本综合指南中,我们探索了 Spring Cloud 及其功能,并通过构建简单的微服务应用程序展示了其功能。通过利用 Spring Cloud 的强大功能,您可以简化微服务的开发和管理,使它们更具弹性、可扩展性并且更易于维护。使用 Spring Cloud 拥抱微服务世界,将您的应用程序提升到新的高度。server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/your-username/config-repo.git # Replace with your Git repository URL
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
Config Server
:Eureka Client
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
第2步:设置Spring Cloud Eureka服务器
Eureka Server
依赖项。为项目命名eureka-server
。application.yml
文件中:server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
registerWithEureka: false
fetchRegistry: false
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
第三步:创建用户服务
Config Client
、Eureka Discovery
和Web
依赖项。为项目命名user-service
。bootstrap.yml
文件中:spring:
application:
name: user-service
cloud:
config:
uri: http://localhost:8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
REST controller
的User Service
:import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/users/{id}")
public String getUser(@PathVariable("id") String id) {
return "User with ID: " + id;
}
}
第四步:创建订单服务
Config Client
、Eureka Discovery
和Web
依赖项。为项目命名order-service
。bootstrap.yml
文件中:spring:
application:
name: order-service
cloud:
config:
uri: http://localhost:8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
REST controller
的Order Service
:import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@GetMapping("/orders/{id}")
public String getOrder(@PathVariable("id") String id) {
return "Order with ID: " + id;
}
}
第 5 步:运行应用程序
config-server
、eureka-server
、user-service
和应用程序。order-service
所有服务运行后,您可以访问 Eureka 仪表板http://localhost:8761
并查看已注册的服务。http://localhost:0b167090b1669846200c0675170164bd/users/1
和订单服务http://localhost:a31569b7d36261b492b7c8937c63c67c/orders/1
。结论
The above is the detailed content of Unleash the power of microservices with Spring Cloud. For more information, please follow other related articles on the PHP Chinese website!