大学生涯springSpringCloud核心组件(一):项目搭建与注册中心对比
Jie1.配置文件配置方式
父工程 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nianxi</groupId> <artifactId>springcloud_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
<modules> <module>provider</module> <module>consumer</module> </modules>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.5</version> <relativePath/> </parent>
<properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
provider 子模块 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <artifactId>springcloud_demo</artifactId> <groupId>com.nianxi</groupId> <version>0.0.1-SNAPSHOT</version> </parent>
<modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
|
application.yml
1 2 3 4 5
| spring: application: name: provider server: port: 8081
|
pojo.User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.nianxi.provider.pojo;
import lombok.*;
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder public class User { private Long id; private String username; }
|
controller.UserController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.nianxi.provider.controller;
import com.nianxi.provider.pojo.User; 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("/simple/{id}") public User findById(@PathVariable("id") Long id) { return User.builder() .id(id) .username("hello" + id) .build(); } }
|
consumer 子模块 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <artifactId>springcloud_demo</artifactId> <groupId>com.nianxi</groupId> <version>0.0.1-SNAPSHOT</version> </parent>
<modelVersion>4.0.0</modelVersion> <artifactId>consumer</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
|
application.yml
1 2 3 4 5 6 7
| spring: application: name: consumer server: port: 8082 user: userServicePath: http://localhost:8081/simple/
|
pojo.user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.nianxi.consumer.pojo;
import lombok.*;
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Builder public class User { private Long id; private String username; }
|
controller.UserController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.nianxi.consumer.controller;
import com.nianxi.consumer.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;
@RestController public class UserController { @Autowired private RestTemplate restTemplate;
@Value("${user.userServicePath}") private String userServicePath;
@GetMapping("user/{id}") public User findById(@PathVariable Long id) { return this.restTemplate.getForObject(this.userServicePath + id, User.class); } }
|
config.MyConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.nianxi.consumer.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;
@Configuration public class MyConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
|
测试
打开 ProviderApplication 与 ConsumerApplication
调用http://localhost:8082/user/2发现可以返回 id 跟 username,而这些是通过 provide 子模块提供的
2.Eureka 与 Nacos 区别
Eureka 是什么?
Eureka 是 Netflix 开发的服务发现框架,本身是一个基于 REST 的服务,主要用于定位运行在 AWS 域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
详解:
Eureka 包含两个组件:Eureka Server 和 Eureka Client。
Eureka Server:Eureka Server 提供服务注册服务,各个节点启动后,会在 Eureka Server 中进行注册,这 EurekaServer 中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Client:Eureka Client 是一个 java 客户端,用于简化与 Eureka Server 的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
在 Euraka 的 GitHub 上,宣布 Eureka 2.x 闭源。近这意味着如果开发者继续使用作为 2.x 分支上现有工作 repo 一部分发布的代码库和工件,则将自负风险。
Nacos 是什么?
Nacos 是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。简单来说 Nacos 就是 注册中心 + 配置中心的组合,提供简单易用的特性集,帮助我们解决微服务开发必会涉及到的服务注册 与发现,服务配置,服务管理等问题。 Nacos 还是 Spring Cloud Alibaba 组件之一,负责服务注册与发现。
Namespace 隔离设计:
Nacos 提供了 namespace 来实现环境隔离功能。
- nacos 中可以有多个 namespace
- namespace 下可以有 group
- 不同 namespace 之间相互隔离,例如不同 namespace 的服务互相不可见
- 命名空间用于进行隔离,Namespace 的常用场景之一是不同环境的隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。
Nacos 和 Eureka 的区别
a. 支持的 CAP
Consistency(一致性) :数据一致更新,所有数据的变化都是同步的
Availability(可用性) :在集群中一部分节点故障后,集群整体是否还能响应客户端的读写请求
Partition tolerance(分区容忍性) :某个节点的故障,并不影响整个系统的运行
| 注册中心 |
AP |
CP |
| Eureka |
支持 |
不支持 |
| Nacos |
支持 |
支持 |
注:nacos 是根据配置识别 CP 或 AP 模式,如果注册 Nacos 的 client 节点注册时是 ephemeral=true 即为临时节点,那么 Naocs 集群对这个 client 节点效果就是 AP,反之则是 CP,即不是临时节点
b. 连接方式
Nacos 使用的是 netty 和服务直接进行连接,属于长连接
Eureka 是使用定时发送和服务进行联系,属于短连接
c. 服务异常剔除
Nacos:
Nacos client 通过心跳上报方式告诉 nacos 注册中心健康状态,默认心跳间隔 5 秒,nacos 会在超过 15 秒未收到心跳后将实例设置为不健康状态,可以正常接收到请求超过 30 秒 nacos 将实例删除,不会再接收请求
Eureka:
Eureka client 在默认情况每隔 30s 想 Eureka Server 发送一次心跳,当 Eureka Server 在默认连续 90s 秒的情况下没有收到心跳, 会把 Eureka client 从注册表中剔除,在由 Eureka-Server 60 秒的清除间隔,把 Eureka client 给下线,也就是在极端情况下 Eureka 服务 从异常到剔除在到完全不接受请求可能需要 30s+90s+60s=3 分钟左右(还是未考虑 ribbon 缓存情况下)
所以 Nacos 服务异常剔除效率高于 Eureka
d. 操作实例方式
Nacos:
提供了 nacos console 可视化控制话界面,可以对实例列表进行监听,对实例进行上下线,权重的配置,并且 config server 提供了对服务实例提供配置中心,且可以对配置进行 CRUD,版本管理
Eureka:
仅提供了实例列表,实例的状态,错误信息,相比于 nacos 过于简单
所以 Nacos 相对于 Eureka 操作更详细,多元化
这里我们先学习 eureka
3.Eureka 注册中心
a.创建子模块 eureka,导入对应 server 依赖 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="UTF-8"?> <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.nianxi</groupId> <artifactId>springcloud_demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
<artifactId>eureka-01</artifactId>
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<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-server</artifactId> </dependency>
</dependencies>
</project>
|
b.配置父工程的 springCloud 依赖 pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <properties> <spring-cloud.version>2023.0.2</spring-cloud.version> </properties> <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>
|
c.在 eureka 模块中创建启动类 EurekaApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.nianxi.eureka;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaServer;
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
|
d.在 eureka 模块中创建配置文件 application.yml
1 2 3 4 5 6 7 8 9 10 11
| server: port: 8083
eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
|

e.修改提供者跟消费者模块
1.在 provide 模块的 pom.xml 文件中导入 eureka-client 依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
|
2.在 provide 模块的启动类 ProviderApplication 上添加@EnableEurekaClient 注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.nianxi.provider;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication @EnableEurekaClient public class ProviderApplication {
public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }
}
|
emm,由于使用的是 springboot3,没有该注解,会自动导入

所以
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.nianxi.provider;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class ProviderApplication {
public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }
}
|
即可
3.在 provide 模块的配置文件 application.yml 中添加 eureka 相关配置
1 2 3 4 5 6 7 8 9 10
| spring: application: name: service-provider server: port: 8081
eureka: client: service-url: defaultZone: http://localhost:8083/eureka/
|

consumer 模块
同理,consumer 模块也是如此
配置文件
1 2 3 4 5 6 7 8 9 10 11
| spring: application: name: service-consumer server: port: 8082 user: userServicePath: http://SERVICE-PROVIDE/simple/ eureka: client: service-url: defaultZone: http://localhost:8083/eureka/
|
在 myConfig 的 RestTemplate 上添加@LoadBalanced 注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.nianxi.consumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;
@Configuration public class MyConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
|
