Home  >  Article  >  Java  >  Unleash the power of microservices with Spring Cloud

Unleash the power of microservices with Spring Cloud

DDD
DDDOriginal
2023-09-08 10:23:09740browse

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.

What is Spring Cloud?

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.

The main features of Spring Cloud

  1. Configuration management: Spring Cloud Config is distributed The application provides centralized configuration management.
  2. Service discovery: Spring Cloud Netflix Eureka supports service registration and discovery to achieve better load balancing and fault tolerance .
  3. Circuit breaker: Spring Cloud Netflix Hystrix helps prevent cascading failures by isolating access points between services.
  4. Distributed Tracing: Spring Cloud Sleuth and Zipkin support tracing requests across multiple services for better observability and debugging.

Build a simple microservice application using Spring Cloud

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.

Prerequisites

Make sure you have the following software installed on your computer:

  • Java 8 or higher
  • Maven or Gradle
  • The IDE of your choice

Dependencies

<!-- 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 &#39;org.springframework.cloud:spring-cloud-config-server&#39;
implementation &#39;org.springframework.cloud:spring-cloud-starter-netflix-eureka-client&#39;
implementation &#39;org.springframework.cloud:spring-cloud-starter-netflix-eureka-server&#39;
implementation &#39;org.springframework.cloud:spring-cloud-starter-config&#39;
implementation &#39;org.springframework.cloud:spring-cloud-starter-netflix-eureka-client&#39;
implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;


##Step 1: Set up Spring Cloud Configuration Server

Use Spring Initializr (https://start.spring.io/) to create a new Spring Boot project and add

Config Server and Eureka Discovery dependencies. Name the projectconfig-server.

Add the following properties to your

application.yml file:

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 ServerEureka 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服务器

使用Spring Initializr创建一个新的 Spring Boot 项目并添加Eureka Server依赖项。为项目命名eureka-server

将以下属性添加到您的application.yml文件中:

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

通过将以下注释添加到主类来启用 Eureka Server:

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

第三步:创建用户服务

使用 Spring Initializr 创建一个新的 Spring Boot 项目并添加Config ClientEureka DiscoveryWeb依赖项。为项目命名user-service

将以下属性添加到您的bootstrap.yml文件中:

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://localhost:8888
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

创建一个简单REST controllerUser 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;
    }
}

第四步:创建订单服务

使用 Spring Initializr 创建一个新的 Spring Boot 项目并添加Config ClientEureka DiscoveryWeb依赖项。为项目命名order-service

将以下属性添加到您的bootstrap.yml文件中:

spring:
  application:
    name: order-service
  cloud:
    config:
      uri: http://localhost:8888
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

创建一个简单REST controllerOrder 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-servereureka-serveruser-service和应用程序。order-service所有服务运行后,您可以访问 Eureka 仪表板http://localhost:8761并查看已注册的服务。

您现在可以访问用户服务http://localhost:0b167090b1669846200c0675170164bd/users/1和订单服务http://localhost:a31569b7d36261b492b7c8937c63c67c/orders/1

结论

在本综合指南中,我们探索了 Spring Cloud 及其功能,并通过构建简单的微服务应用程序展示了其功能。通过利用 Spring Cloud 的强大功能,您可以简化微服务的开发和管理,使它们更具弹性、可扩展性并且更易于维护。使用 Spring Cloud 拥抱微服务世界,将您的应用程序提升到新的高度。

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn