1. 前置条件
在开始之前,需要确保你已经具备了以下环境:
- JDK 8 或更高版本
- Maven 3.5+
- IntelliJ IDEA 或 Eclipse(推荐 IntelliJ IDEA)
- MySQL 数据库(可选,视项目需求)
2. 创建父工程
首先,我们需要创建一个父工程,用来管理整个微服务的子项目。该父项目的 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>springcloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>eureka-server</module>
<module>service-provider</module>
<module>service-consumer</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Spring Cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-boot.version>2.3.8.RELEASE</spring-boot.version>
</properties>
</project>
3. 搭建 Eureka 服务注册中心
Eureka 是 Spring Cloud Netflix 组件中的服务发现组件,负责注册和管理微服务。
1. 创建 eureka-server 子项目
在 springcloud-demo 父项目中,创建一个名为 eureka-server 的子项目。然后在 pom.xml 中添加相关依赖:
<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>
2. 配置 Eureka Server
在 application.yml 中配置 Eureka Server:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
3. 启动类
编写 EurekaServerApplication 启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4. 搭建 Service Provider 服务提供者
接下来我们搭建一个简单的服务提供者(service-provider),它将注册到 Eureka 中。
1. 创建 service-provider 子项目
在父项目中创建 service-provider 子项目,添加如下依赖:
<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>
2. 配置 Service Provider
在 application.yml 中进行配置:
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 编写服务提供接口
创建一个简单的服务接口:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Service Provider!";
}
}
5. 搭建 Service Consumer 服务消费者
我们现在创建一个消费者服务(service-consumer),它将调用 service-provider 的服务。
1. 创建 service-consumer 子项目
创建 service-consumer 子项目并添加如下依赖:
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. 配置 Service Consumer
在 application.yml 中进行如下配置:
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 使用 Feign 调用服务提供者
创建一个 Feign 客户端,调用 service-provider 提供的接口:
@FeignClient("service-provider")
public interface HelloClient {
@GetMapping("/hello")
String hello();
}
@RestController
public class HelloConsumerController {
@Autowired
private HelloClient helloClient;
@GetMapping("/hello-consumer")
public String helloConsumer() {
return helloClient.hello();
}
}
6. 启动服务
依次启动 EurekaServerApplication、ServiceProviderApplication 和 ServiceConsumerApplication,然后访问 http://localhost:8082/hello-consumer,你应该可以看到来自 service-provider 的响应。