Spring Cloud Nacos and Eureka Differences, Including Hands-On Code

Time:2023-10-1


Spring Cloud Nacos and Eureka Differences, Including Hands-On Code

Spring Cloud Nacos and Spring Cloud Eureka are both service registration and discovery components in the Spring Cloud microservices framework, used to help developers easily build and manage microservices applications. The main differences between them are the underlying architecture, service discovery methods, configuration management, and supported programming languages.

A detailed explanation of Spring Cloud Eureka

Spring Cloud Eureka is based on Netflix Eureka’s secondary packaging for automated registration and discovery of microservice instances. It is mainly responsible for completing the service governance function in the microservice architecture. The following is a detailed explanation of Spring Cloud Eureka, including code samples.

  1. Adding Dependencies
    Add the Spring Cloud Eureka dependency to the project’s build.gradle file:
dependencies {  
   implementation 'org.springframework.cloud:spring-cloud-starter-eureka'  
}
  1. Configure application.yaml
    Configure the relevant properties of the Eureka service in the application.yaml file:
server:  
 port: 8080
eureka:  
 client:  
   serviceUrl:  
     defaultZone: http://localhost:8761/eureka/
  1. Startup classes add dependencies
    Add the @EnableDiscoveryClient annotation to the startup class to enable Eureka service discovery:
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);  
   }  
}
  1. Service Registration and Discovery
    In the service provider, enable Eureka service registration with the @EnableEurekaServer annotation and add the @Value annotation to the startup class to configure the Eureka service port number:
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);  
   }  
}

In the service consumer, expose a REST interface via the @RestController and @RequestMapping annotations, and configure the Eureka service port number in the @Value annotation. At the same time, other services are invoked using RestTemplate:

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);  
   }  
}
  1. Customizing the Eureka Interface
    You can customize the interface by modifying the default interface of Spring Cloud Eureka. In the src/main/resources/static directory, find the eureka-dashboard.html file, copy it to the static resources directory in your project, and modify it accordingly.
    In summary, Spring Cloud Eureka is a service governance component for automating the registration and discovery of microservice instances. The basic functionality of Spring Cloud Eureka can be achieved by adding dependencies to the project, configuring application.yaml, adding dependencies to startup classes, service registration and discovery, and customizing the Eureka interface.

2. Detailed explanation of Spring Cloud Nacos

You can refer to the previously written article: https://python-basketball.blog.csdn.net/article/details/132506054 “Spring_Cloud_NacosEureka_83”>III. Spring Cloud Nacos and Eureka Difference

  1. Underlying Architecture:
    Nacos is a new lightweight platform for dynamic service discovery, configuration management and service management, based on Alibaba’s self-developed Nacos technology.
    Eureka is a Netflix open source service registration and discovery framework , based on RESTful API for communication between services .
  2. Service discovery approach:
    Nacos uses DNS for service discovery, storing service registration information in a DNS server, making service discovery more efficient and reliable.
    Eureka uses the HTTP protocol for service discovery, which requires a request to the Eureka server for service registration information.
  3. Configuration Management:
    Nacos supports dynamic management of configurations, enabling configuration modifications and updates, as well as version control of configurations.
    Eureka does not support dynamic management of configurations and requires manual configuration changes and updates.
  4. Supported Programming Languages:
    Nacos supports a variety of programming languages, including Java, Python, Go, Node.js, and more.
    Eureka supports only the Java language.
    Here is a simple example to show how to use Spring Cloud Nacos and Spring Cloud Eureka:
    Nacos
    First, you need to introduce Nacos dependencies into your project:
<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>

Then, add the startup class@EnableDiscoveryClient respond in singing@NacosPropertySource Annotation:

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
First, you need to introduce Eureka dependencies in your project:

<dependency>  
   <groupId>com.netflix</groupId>  
   <artifactId>eureka-client</artifactId>  
</dependency>

Then, add the startup class@EnableDiscoveryClient annotation and configure the Eureka server address:

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);  
   }  
}

In summary, Spring Cloud Nacos and Spring Cloud Eureka are both service registration and discovery frameworks, but they differ in terms of underlying architecture, service discovery methods, configuration management, and supported programming languages. In practice, you can choose the right framework based on project requirements and team familiarity.

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]