springcloudaliba-nacos registration center

1. Download the installation package

On the GitHub page of Nacos, there is a download link to download the compiled Nacos server or source code:

GitHub主页:GitHub - alibaba/nacos: an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.

Release download page of GitHub: Releases · alibaba/nacos · GitHub

As shown in the picture, the windows version nacos-server-1.4.1.zipcan use the package.

2. Unzip

Unzip this package to any non-Chinese directory, as shown in the figure:

Directory description:

  • bin: startup script

  • conf: configuration file

 

3. Port configuration

The default port of Nacos is 8848. If other processes on your computer occupy port 8848, please try to close the process first.

If you cannot close the process occupying port 8848 , you can also enter the conf directory of nacos and modify the port in the configuration file:

 Modify the contents of it:

4. start

The startup is very simple, enter the bin directory, the structure is as follows:

Then execute the command:

  • windows command:

  • startup.cmd -m standalone

    The effect after execution is as follows:

5. Access

Enter the address in the browser: http://127.0.0.1:8848/nacos :

 The default account and password are both nacos, after entering:

Nacos dependencies

Parent project:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

client:

<!-- nacos客户端依赖包 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

yml file to add cloud:nacos configuration

spring:
  cloud:
    nacos:
      server-addr: localhost:8848

reboot

After restarting the microservice, log in to the nacos management page, and you can see the microservice information:

 set up cluster

A service can have multiple instances , such as our user-service, can have:

  • 127.0.0.1:8081

  • 127.0.0.1:8082

  • 127.0.0.1:8083

If these instances are distributed in different computer rooms across the country, for example:

  • 127.0.0.1:8081, in Shanghai computer room

  • 127.0.0.1:8082, in Shanghai computer room

  • 127.0.0.1:8083, in Hangzhou computer room

Nacos divides the instances in the same computer room into a cluster .

In other words, user-service is a service. A service can contain multiple clusters, such as Hangzhou and Shanghai. Each cluster can have multiple instances, forming a hierarchical model, as shown in the figure:

 

When microservices access each other, they should access the same cluster instance as much as possible, because local access is faster. Only access other clusters when the cluster is unavailable. For example:

The order-service in the computer room in Hangzhou should give priority to access the user-service in the same computer room.

2. Load balancing with the same cluster priority

By default, ZoneAvoidanceRuleit is not possible to achieve load balancing based on the priority of the same cluster.

Therefore, Nacos provides an NacosRuleimplementation that can preferentially select instances from the same cluster.

1) Configure cluster information for order-service

Modify the application.yml file of order-service and add the cluster configuration:

  cloud:
    nacos:
      server-addr: localhost:8849 # nacos服务地址
      discovery:
        cluster-name: SH

Then start several projects separately, and different clusters will appear

2) Modify the load balancing rules

Modify the application.yml file of order-service and modify the load balancing rules:

userservice:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # 负载均衡规则 

If the local service hangs up and accesses across clusters, a warning will occur. If the operation and maintenance sees a problem, just restart the service.

 

weight configuration

In actual deployment, such a scenario will appear:

The performance of server equipment is different. Some instances have better performance, while others have poorer performance. We hope that machines with better performance can bear more user requests.

But by default, NacosRule is randomly selected in the same cluster, without considering the performance of the machine.

Therefore, Nacos provides weight configuration to control the access frequency. The greater the weight, the higher the access frequency .

In the nacos console, find the instance list of user-service, and click Edit to modify the weight:

 In the edit window that pops up, modify the weight:

 

 environmental isolation

Nacos provides namespace to implement environment isolation.

  • There can be multiple namespaces in nacos

  • There can be group, service, etc. under the namespace

  • Different namespaces are isolated from each other, for example, services in different namespaces are invisible to each other

1. Create a namespace

By default, all services, data, and groups are in the same namespace named public:

We can click the Add button on the page to add a namespace:

 Then, fill out the form:

 You can see a new namespace on the page:

2. Configure namespace for microservices

Configuring namespaces for microservices can only be achieved by modifying the configuration.

For example, modify the application.yml file of order-service:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ
        namespace: 492a7d5d-237b-46a1-a99a-fa8e98e4b0f9 # 命名空间,填ID

After restarting order-service, access the console, and you can see the following results:

 At this time, when accessing order-service, because the namespace is different, the userservice will not be found, and the console will report an error:

6. The difference between Nacos and Eureka

Nacos service instances are divided into two types:

  • Temporary instance: If the instance is down for more than a certain period of time, it will be removed from the service list, the default type. (client sends heartbeat to server, recommended)

  • Non-temporary instance: If the instance goes down, it will not be removed from the service list, and it can also be called a permanent instance ( the server sends a heartbeat to the client ).

 Configure a service instance as a permanent instance:

spring:
  cloud:
    nacos:
      discovery:
        ephemeral: false # 设置为非临时实例

The overall structure of Nacos and Eureka is similar, service registration, service pull, heartbeat waiting, but there are some differences:

- Nacos与eureka的共同点
  - 都支持服务注册和服务拉取
  - 都支持服务提供者心跳方式做健康检测

- Nacos与Eureka的区别
  - Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式
  - 临时实例心跳不正常会被剔除,非临时实例则不会被剔除
  - Nacos支持服务列表变更的消息推送模式,服务列表更新更及时
  - Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP方式

Guess you like

Origin blog.csdn.net/zxc472504515/article/details/125720279