当前位置:网站首页>[micro service]nacos

[micro service]nacos

2022-06-26 23:25:00 fate _ zore

Nacos

Sign up for service to nacos

Nacos yes SpringCloudAlibaba The components of , and SpringCloudAlibaba Also follow SpringCloud Service registration defined in 、 Service discovery specification . Therefore use Nacos And use Eureka For microservices , There's no big difference .

The main difference is :

  • Depending on the difference
  • Different service addresses

Introduce dependencies

stay cloud-demo Parent project pom In the document <dependencyManagement> Introduction in SpringCloudAlibaba Dependence :

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

And then in user-service and order-service Medium pom Introduce in the file nacos-discovery rely on :

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Be careful : Don't forget to comment it out eureka Dependence .

To configure nacos Address

cloud:
  nacos:
    server-addr: localhost:8848

Be careful : Don't forget to comment it out eureka The address of

restart

image-20220623155605477

Service hierarchical storage model

One service There can be multiple example , For example, our user-service, There can be :

  • 127.0.0.1:8081
  • 127.0.0.1:8082
  • 127.0.0.1:8083

If these examples are distributed in different computer rooms all over the country , for example :

  • 127.0.0.1:8081, In Shanghai
  • 127.0.0.1:8082, In Shanghai
  • 127.0.0.1:8083, In Hangzhou computer room

Nacos The instance in the same machine room Divide into one colony .

in other words ,user-service It's service. , A service can contain multiple clusters , Such as Hangzhou 、 Shanghai , Each cluster can have multiple instances , Form a hierarchical model , Pictured :

image-20220623155915007

When microservices access each other , You should access the same cluster instance as much as possible , Because local access is faster . When not available in this cluster , To access other clusters . for example :

image-20220623160104501

In Hangzhou computer room order-service Priority should be given to accessing the same computer room user-service.

Cluster configuration

modify user-service Of application.yml file , Add cluster configuration :

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ #  Cluster name 

Restart both user-service After the instance , We can do it in nacos The console sees the following results :

image-20220623160323623

Change load balancing policy

user-server:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #  Load balancing rules 

perhaps

@Bean
public IRule iRule(){
    
    return new NacosRule();
}

Weight configuration

This scenario will appear in the actual deployment :

There are differences in the performance of server devices , The machine where some examples are located has good performance , Others are poor , We want better machines to take on more user requests .

But by default NacosRule It is randomly selected in the same cluster , The performance of the machine will not be considered .

therefore ,Nacos Weight configuration is provided to control access frequency , The greater the weight, the higher the access frequency .

stay nacos Console , find user-service List of instances of , Click edit , You can modify the weight :

image-20220623183307723

image-20220623183323663

Be careful : If the weight is changed to 0, Then the instance will never be accessed

Environmental isolation

Nacos Provides namespace To achieve environmental isolation .

  • nacos There can be more than one namespace
  • namespace There can be group、service etc.
  • Different namespace Separate from each other , For example, different namespace Our services are not visible to each other

image-20220623204200658

establish namespace

By default , all service、data、group All in the same namespace, be known as public:

image-20220623204236337

We can click the page add button , Add one namespace:

image-20220623205639533

then , Fill out the form :

image-20220623205658899

You can see a new one on the page namespace:

image-20220623205723175

Service configuration namespace

Configure the micro service namespace This can only be achieved by modifying the configuration .

for example , modify order-service Of application.yml file :

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ
        namespace: 492a7d5d-237b-46a1-a99a-fa8e98e4b0f9 #  Namespace , fill ID

restart order-service after , Access the console :

image-20220623205759940

image-20220623205818919

Visit at this time order-service, because namespace Different , Will lead to the loss of userservice, The console will report an error

Nacos And Eureka The difference between

Nacos There are two types of service instances l type :

  • Temporary instance : If the instance goes down for more than a certain time , Will be removed from the service list , The default type .

  • Non temporary instance : If the instance goes down , Will not be removed from the service list , It can also be called permanent instance .

Configure a service instance as a permanent instance :

spring:
  cloud:
    nacos:
      discovery:
        ephemeral: false #  Set to non temporary instance 

Nacos and Eureka The overall structure is similar , Service registration 、 Service pull 、 Heartbeat waiting , But there are some differences :

image-20220623205919220

  • Nacos And eureka Common ground

    • Both support service registration and service pull
    • All support service providers' heartbeat for health detection
  • Nacos And Eureka The difference between

    • Nacos Support the server to actively detect the provider status : The temporary instance adopts heartbeat mode , Active detection mode is adopted for non temporary instances
    • Temporary instances with abnormal heartbeat will be rejected , Non temporary instances will not be rejected
    • Nacos Support the message push mode of service list change , The service list is updated more timely
    • Nacos The cluster adopts... By default AP The way , When there are non temporary instances in the cluster , use CP Pattern ;Eureka use AP The way
原网站

版权声明
本文为[fate _ zore]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262309254930.html