springcloud入门与实践

发布于:2024-06-17 ⋅ 阅读:(20) ⋅ 点赞:(0)

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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>config-server</module>
        <module>eureka-server</module>
        <module>service-provider</module>
        <module>service-consumer</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
2. 创建配置服务 (Config Server)

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

config-server/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>config-server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>

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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>eureka-server</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>service-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.m aaven.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>service-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter

-web</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-openfeign</artifactId>
        </dependency>
    </dependencies>
</project>

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 的高级特性和最佳实践,满足实际项目的需求。