spring cloud 后端 springcloud入门与实践 - server

📅 2026-12-01 👤 拼多多助力营销技巧 📂 2026拼多多助力 👁️ 154 次阅读

Spring Cloud 是一个基于 Spring Boot 的框架,专注于微服务架构下的常见问题,如服务发现、负载均衡、断路器、分布式配置、消息驱动的微服务等。以下是 Spring Cloud 的入门指南和实践示例。

1. 环境准备

首先,确保你已经安装了以下工具:

JDK 1.8+Apache Maven 3.3+Docker (可选,用于运行部分服务,如配置中心)

2. 创建一个简单的 Spring Cloud 项目

项目结构

我们将创建一个简单的 Spring Cloud 项目,包括配置服务、服务注册中心、服务提供者和服务消费者。项目结构如下:

spring-cloud-demo

├── config-server

├── eureka-server

├── service-provider

├── service-consumer

└── pom.xml

1. 创建父POM文件

首先,在项目根目录创建 pom.xml 文件,定义父POM:

pom.xml:

4.0.0

com.example

spring-cloud-demo

1.0-SNAPSHOT

pom

config-server

eureka-server

service-provider

service-consumer

org.springframework.cloud

spring-cloud-dependencies

2021.0.3

pom

import

2. 创建配置服务 (Config Server)

配置服务用于集中管理分布式系统中的配置文件。

config-server/pom.xml:

4.0.0

com.example

spring-cloud-demo

1.0-SNAPSHOT

config-server

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-config-server

config-server/src/main/resources/application.yml:

server:

port: 8888

spring:

cloud:

config:

server:

git:

uri: https://github.com/your-repo/config-repo

search-paths: config

management:

endpoints:

web:

exposure:

include: "*"

config-server/src/main/java/com/example/config/ConfigServerApplication.java:

package com.example.config;

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. 创建服务注册中心 (Eureka Server)

服务注册中心用于服务的注册和发现。

eureka-server/pom.xml:

4.0.0

com.example

spring-cloud-demo

1.0-SNAPSHOT

eureka-server

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-web

eureka-server/src/main/resources/application.yml:

server:

port: 8761

eureka:

client:

register-with-eureka: false

fetch-registry: false

server:

wait-time-in-ms-when-sync-empty: 0

eureka-server/src/main/java/com/example/eureka/EurekaServerApplication.java:

package com.example.eureka;

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);

}

}

4. 创建服务提供者 (Service Provider)

服务提供者提供具体的服务实现。

service-provider/pom.xml:

4.0.0

com.example

spring-cloud-demo

1.0-SNAPSHOT

service-provider

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

service-provider/src/main/resources/application.yml:

server:

port: 8080

spring:

application:

name: service-provider

eureka:

client:

service-url:

defaultZone: http://localhost:8761/eureka/

service-provider/src/main/java/com/example/provider/ServiceProviderApplication.java:

package com.example.provider;

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);

}

}

service-provider/src/main/java/com/example/provider/controller/HelloController.java:

package com.example.provider.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

@GetMapping("/hello")

public String sayHello(@RequestParam String name) {

return "Hello, " + name;

}

}

5. 创建服务消费者 (Service Consumer)

服务消费者调用服务提供者提供的服务。

service-consumer/pom.xml:

4.0.0

com.example

spring-cloud-demo

1.0-SNAPSHOT

service-consumer

org.springframework.boot

spring-boot-starter

-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-openfeign

service-consumer/src/main/resources/application.yml:

server:

port: 8081

spring:

application:

name: service-consumer

eureka:

client:

service-url:

defaultZone: http://localhost:8761/eureka/

service-consumer/src/main/java/com/example/consumer/ServiceConsumerApplication.java:

package com.example.consumer;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients

public class ServiceConsumerApplication {

public static void main(String[] args) {

SpringApplication.run(ServiceConsumerApplication.class, args);

}

}

service-consumer/src/main/java/com/example/consumer/client/HelloClient.java:

package com.example.consumer.client;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "service-provider")

public interface HelloClient {

@GetMapping("/hello")

String sayHello(@RequestParam(name = "name") String name);

}

service-consumer/src/main/java/com/example/consumer/controller/HelloController.java:

package com.example.consumer.controller;

import com.example.consumer.client.HelloClient;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

private final HelloClient helloClient;

public HelloController(HelloClient helloClient) {

this.helloClient = helloClient;

}

@GetMapping("/hello")

public String sayHello(@RequestParam String name) {

return helloClient.sayHello(name);

}

}

3. 构建和运行

首先,通过Maven构建项目。在项目根目录下运行以下命令:

mvn clean install

启动配置服务

进入 config-server 目录,运行以下命令启动配置服务:

mvn spring-boot:run

启动服务注册中心

进入 eureka-server 目录,运行以下命令启动服务注册中心:

mvn spring-boot:run

启动服务提供者

进入 service-provider 目录,运行以下命令启动服务提供者:

mvn spring-boot:run

启动服务消费者

进入 service-consumer 目录,运行以下命令启动服务消费者:

mvn spring-boot:run

如果一切正常,你应该可以在浏览器中访问 http://localhost:8081/hello?name=World,并看到以下输出:

Hello, World

4. 配置中心与负载均衡

你可以进一步配置配置中心以存储和分发配置文件,并在服务消费者中启用负载均衡,以在多个服务实例之间分发请求。

配置中心示例

在 config-repo 仓库中创建配置文件 service-provider.yml: 拼多多帮助力入口

spring:

application:

name: service-provider

server:

port: 8080

在 service-consumer.yml 文件中配置负载均衡:

spring:

application:

name: service-consumer

server:

port: 8081

eureka:

client:

service-url:

defaultZone: http://localhost:8761/eureka/

spring:

cloud:

loadbalancer:

ribbon:

OkToRetryOnAllOperations: true

总结

通过以上步骤,你已经成功创建了一个简单的 Spring Cloud 项目,包括配置服务、服务注册中心、服务提供者和服务消费者模块,并了解了如何使用 FeignClient 调用远程服务。Spring Cloud 提供了丰富的功能,如服务发现、负载均衡、断路器、分布式配置、消息驱动的微服务等,可以帮助你构建高性能、可伸缩的分布式系统。通过深入学习和实践,可以更好地掌握 Spring Cloud 的高级特性和最佳实践,满足实际项目的需求。

好文推荐

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧