Spring Cloud Nacos 和 Eureka區(qū)別解析
Spring Cloud Nacos 和 Spring Cloud Eureka 都是 Spring Cloud 微服務(wù)框架中的服務(wù)注冊和發(fā)現(xiàn)組件,用于幫助開發(fā)者輕松地構(gòu)建和管理微服務(wù)應(yīng)用。它們之間的主要區(qū)別在于底層架構(gòu)、服務(wù)發(fā)現(xiàn)方式、配置管理和支持的編程語言等方面。
一、Spring Cloud Eureka詳解
Spring Cloud Eureka 是基于 Netflix Eureka 的二次封裝,用于實(shí)現(xiàn)微服務(wù)實(shí)例自動(dòng)化注冊與發(fā)現(xiàn)。它主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能。以下是 Spring Cloud Eureka 的詳解,包括代碼示例。
1.添加依賴
在項(xiàng)目的 build.gradle 文件中添加 Spring Cloud Eureka 依賴:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-eureka'
}2.配置 application.yaml
在 application.yaml 文件中配置 Eureka 服務(wù)的相關(guān)屬性:
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ 3.啟動(dòng)類添加依賴
在啟動(dòng)類上添加 @EnableDiscoveryClient 注解,以啟用 Eureka 服務(wù)發(fā)現(xiàn)功能:
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoApplication.class, args);
}
}4.服務(wù)注冊與發(fā)現(xiàn)
在服務(wù)提供者中,通過 @EnableEurekaServer 注解啟用 Eureka 服務(wù)注冊功能,并在啟動(dòng)類上添加 @Value 注解配置 Eureka 服務(wù)端口號(hào):
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableEurekaServer(port = 8761)
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}在服務(wù)消費(fèi)者中,通過 @RestController 和 @RequestMapping 注解暴露一個(gè) REST 接口,并在 @Value 注解中配置 Eureka 服務(wù)端口號(hào)。同時(shí),使用 RestTemplate 調(diào)用其他服務(wù):
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class EurekaClientApplication {
@Value("${eureka.client.serviceUrl.defaultZone}")
private String defaultZone;
@GetMapping("/hi")
public String home(@RequestParam String name) {
String url = defaultZone + "/eureka-client/hello";
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(url, String.class, name);
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}5.自定義 Eureka 界面
- 可以通過修改 Spring Cloud Eureka 的默認(rèn)界面來實(shí)現(xiàn)自定義界面。在 src/main/resources/static 目錄下,找到 eureka-dashboard.html 文件,將其復(fù)制到項(xiàng)目中的靜態(tài)資源目錄,并進(jìn)行相應(yīng)的修改。
- 綜上所述,Spring Cloud Eureka 是一個(gè)用于實(shí)現(xiàn)微服務(wù)實(shí)例自動(dòng)化注冊與發(fā)現(xiàn)的服務(wù)治理組件。通過在項(xiàng)目中添加依賴、配置 application.yaml、啟動(dòng)類添加依賴、服務(wù)注冊與發(fā)現(xiàn)以及自定義 Eureka 界面,可以實(shí)現(xiàn) Spring Cloud Eureka 的基本功能。
二、Spring Cloud Nacos詳解
可以參考之前寫的文章:https://python-basketball.blog.csdn.net/article/details/132506054?spm=1001.2014.3001.5502
三、Spring Cloud Nacos和Eureka區(qū)別
1.底層架構(gòu):
Nacos 是一個(gè)全新的輕量級動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理的平臺(tái),基于阿里巴巴自研的 Nacos 技術(shù)。
Eureka 是 Netflix 開源的一個(gè)服務(wù)注冊和發(fā)現(xiàn)框架,基于 RESTful API 進(jìn)行服務(wù)之間的通信。
2.服務(wù)發(fā)現(xiàn)方式:
Nacos 使用 DNS 方式進(jìn)行服務(wù)發(fā)現(xiàn),將服務(wù)注冊信息存儲(chǔ)在 DNS 服務(wù)器中,使得服務(wù)發(fā)現(xiàn)更加高效和可靠。
Eureka 則使用 HTTP 協(xié)議進(jìn)行服務(wù)發(fā)現(xiàn),需要通過請求 Eureka 服務(wù)端來獲取服務(wù)注冊信息。
3.配置管理:
Nacos 支持配置的動(dòng)態(tài)管理,可以實(shí)現(xiàn)配置的修改和更新,同時(shí)支持配置的版本控制。
Eureka 不支持配置的動(dòng)態(tài)管理,需要通過人工的方式進(jìn)行配置的修改和更新。
4.支持的編程語言:
Nacos 支持多種編程語言,包括 Java、Python、Go、Node.js 等。
Eureka 僅支持 Java 語言。
下面是一個(gè)簡單的示例來展示如何使用 Spring Cloud Nacos 和 Spring Cloud Eureka:
Nacos
首先,需要在項(xiàng)目中引入 Nacos 的依賴:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
然后,在啟動(dòng)類上添加 @EnableDiscoveryClient 和 @NacosPropertySource 注解:
import com.alibaba.cloud.spring.boot.context.annotation.config.NacosPropertySource;
import com.alibaba.cloud.spring.boot.context.annotation.EnableDiscoveryClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDiscoveryClient
@NacosPropertySource(dataId = "your-data-id", autoRefreshed = true)
public class NacosDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDemoApplication.class, args);
}
}Eureka
首先,需要在項(xiàng)目中引入 Eureka 的依賴:
<dependency> <groupId>com.netflix</groupId> <artifactId>eureka-client</artifactId> </dependency>
然后,在啟動(dòng)類上添加 @EnableDiscoveryClient 注解,并配置 Eureka 服務(wù)端地址:
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDiscoveryClient(eurekaServerUrl = "http://localhost:8761/eureka/")
public class EurekaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoApplication.class, args);
}
}綜上所述,Spring Cloud Nacos 和 Spring Cloud Eureka 都是服務(wù)注冊和發(fā)現(xiàn)框架,但它們在底層架構(gòu)、服務(wù)發(fā)現(xiàn)方式、配置管理和支持的編程語言等方面有所不同。在實(shí)際應(yīng)用中,可以根據(jù)項(xiàng)目需求和團(tuán)隊(duì)熟悉程度選擇合適的框架。
到此這篇關(guān)于Spring Cloud Nacos 和 Eureka區(qū)別的文章就介紹到這了,更多相關(guān)Spring Cloud Nacos 和 Eureka內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License Activation"的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之寵物醫(yī)院與商城一體的系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+Springboot+Jsp+maven+Mysql開發(fā)的寵物醫(yī)院與商城一體的系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有寵物醫(yī)院和寵物商城該有的所有功能,感興趣的朋友快來看看吧2022-02-02
BootStrap Jstree 樹形菜單的增刪改查的實(shí)現(xiàn)源碼
這篇文章主要介紹了BootStrap Jstree 樹形菜單的增刪改查的實(shí)現(xiàn)源碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
使用SpringBoot-JPA進(jìn)行自定義保存及批量保存功能
這篇文章主要介紹了使用SpringBoot-JPA進(jìn)行自定義的保存及批量保存功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
java序列化與ObjectOutputStream和ObjectInputStream的實(shí)例詳解
這篇文章主要介紹了java序列化與ObjectOutputStream和ObjectInputStream的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Security框架:如何使用CorsFilter解決前端跨域請求問題
這篇文章主要介紹了Security框架:如何使用CorsFilter解決前端跨域請求問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段
這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

