前言
开发软件:IntelliJ IDEA 2022.2.1 (以下简称 idea)
java 版本:1.8
Spring boot 版本:2.7.15
Spring Cloud 版本:2021.0.8
Spring Cloud Eureka 用于服务注册与发现
一、创建父工程
(1) 在 idea 打开的情况下,依次选择 File -> New -> Project,新建项目
(2) 选择 Spring Initializr,输入项目名,以及相关配置,点击 next 进行下一步
(3) 选择自己所需的 spring boot 版本,这里我选择 2.7.15 ,接着选择Spring Cloud 中的 Cloud BootStrap,点击 Create,等待项目创建完成
(4)删除父工程的 src 文件夹,修改 pom.xml 文件,以下为 pom.xml 文件简要说明
<!-- 如果没有,则新增,表示以 jar 的方式打包 -->
<packaging>jar</packaging>
<!-- 用于控制依赖的版本,根据自己所需加入 -->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<!-- 在多模块项目中,通过在父工程引入dependencyManagement控制,来管理子工程依赖的版本,
子工程直接引入依赖,不再需要填写版本号,由父工程统一管理,父工程自己并不会引入这些依赖的 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 子模块的 artifactId,eg:存在子模块xxx,则 -->
<modules>
<module>xxx</module>
</modules>
二、创建 Eureka
1. 服务端
(1)在父工程中,依次选择 File -> New -> Module,新建模块
(2) 选择 Spring Initializr,输入项目名,以及相关配置,点击 next 进行下一步
(3) 选择自己所需的 spring boot 版本,这里我选择 2.7.15 ,接着选择 Spring Cloud Discovery 中的 Eureka Server,点击 Create,等待项目创建完成
(4)修改项目 pom.xml 文件,继承父工程
子模块 pom.xml, 具体修改如下(ps:其他依赖修改根据自己需求修改)
<!-- 修改 parent 标签,继承父工程 -->
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.wal</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
父工程 pom.xml 添加 eureka-server 模块名,具体修改如下
<!-- module 标签的值为子模块的 artifactId -->
<modules>
<module>eureka-server</module>
</modules>
(5)修改 eureka-servver 的配置文件 application.yml (文件类型为 .properties, .yml, .yaml 皆可)
server:
port: 8081 # 服务端口
spring:
application:
name: eureka-server # 服务名
eureka:
client:
service-url:
defaultZone: http://localhost:${server.port}/eureka/ # eureka 注册和发现 url
(6)在启动类加入注解 @EnableEurekaServer ,启用 eureka server 服务
package com.wal.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
(7)打开网址 http://localhost:8081,查看效果
2. 客户端
(1)在父工程中,依次选择 File -> New -> Module,新建模块
(2) 选择 Spring Initializr,输入项目名,以及相关配置,点击 next 进行 下一步
(3) 选择自己所需的 spring boot 版本,这里我选择 2.7.15 ,接着选择 Spring Cloud Discovery 中的 Eureka Discovery Client (ps:请根据自己所需加入相关依赖,我这里加入下图2所示依赖),点击 Create,等待项目创建完成
(4)修改项目 pom.xml 文件,继承父工程
子模块 pom.xml, 具体修改如下(ps:其他依赖修改根据自己需求修改)
<!-- 修改 parent 标签,继承父工程 -->
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.wal</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
父工程 pom.xml 添加 userservice 模块名,具体修改如下
<!-- module 标签的值为子模块的 artifactId -->
<modules>
<module>userservice</module>
</modules>
(5)修改 userservice 的配置文件 application.yml (文件类型为 .properties, .yml, .yaml 皆可)
server:
port: 8082 # 服务端口
spring:
application:
name: user-service # 服务名
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/ # eureka 注册和发现 url
(6)打开网址 http://localhost:8081,查看效果
三、微服务调用
在原有基础上新建 order-service 模块,通过 user-service 调用 order-service
(1)在 user-service 新建配置类,创建 RestTemplate 类,并交给 IOC 容器管理,部分代码如下
@Bean
@LoadBalanced // 用于负载均衡,默认为轮询
public RestTemplate getInstance() {
return new RestTemplate();
}
(2)分别给 order-service,user-service 创建 controller,用于接口调用
user-service 代码如下
package com.wal.userservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/{id}")
public String testUser(@PathVariable String id) {
// order-service 为服务提供者的服务名,在请求时会去 eureka-server 拉取该注册信息,并自动转化为相应 IP 和端口
// eg:假设 order-service 只有一个注册实列,其中 IP 为 123,端口为 8083,则 http://order-service/order/order_1 = http://123:8083/order/order_1
String order = restTemplate.getForObject("http://order-service/order/order_1", String.class);
return "User Id:" + id + ", " + order;
}
}
order-service 代码如下
package com.wal.orderservice.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("order")
public class OrderController {
@GetMapping("/{id}")
public String TestOrder(@PathVariable String id) {
System.out.println(id);
return "Order Id: " + id;
}
}
(3)打开浏览器,输入网址 http://localhost:8082/user/1,查看效果
user-service 调用了 order-service
四、Idea 模拟多个相同服务
依次选择左下角 Services -> 对应 service -> Edit Configuration,配置 VM 参数即可启动
若有不足,欢迎指出,开发之路,与君共勉!