The new generation of Java micro-services Nacos

Foreword

Update and maintain the road re-opened from the Java development areas most widely used RPC framework by the end of 2017 began to Dubbo, Dubbo Alibaba to build a micro-service ecological sustainability of open source Sentinel , Nacos , Seata and other micro-services middleware framework, and launched Spring Cloud Alibaba to provide micro-stop solution service development, Ali Baba continued to be active in the Java community together, but also injected new vitality into the development of Java micro-services.

This article will focus on learning the micro-service components Nacos as function and usage of the registry, Nacos Alibaba in July 2018 a new open source project.

About Nacos name: the first four letters are the first two letters and Configuration Naming the final sfor the Service.

This article relates to the following:

  • Nacos basic introduction
  • Why Nacos
  • The latest version Nacos and Rest / registration and discovery services integrated Dubbo

About Nacos / nɑ: kəʊs /

Easier to build a dynamic cloud-native application service discovery, configuration management and service management platform.

Above this sentence is taken from Nacos official home page is a summary of Nacos overall functionality.

In simple terms Nacos is a combination of registry + distribution center, providing easy to use set of features that help us solve the micro-service development will be related to the registration and service discovery, service configuration, service management and other issues. Nacos is one of Spring Cloud Alibaba component responsible for service registration and discovery .

First, look at the official Nacos of properties description:

  • Service discovery and health monitoring services
  • Dynamic configuration services
  • Dynamic DNS Service
  • Service and metadata management
  • Continue to add ...

You can find all the features are inseparable from the service of the word, Service (Service) is Nacos world first-class citizens, indicating that the service is Nacos the most important role.

Why is Nacos

Now micro-ecosystem services, there have been a lot of service registration and discovery of open source components, such as Eurka, ZooKeeper, Consul, why use Nacos, let's look at a simple comparison of these frameworks:

Service registration and discovery frame CAP model Console Management Community activity
Eureka Of stand by Low (2.x version closed source)
Zookeeper CP not support in
Consul CP stand by high
nacos Of stand by high

It is said Nacos inside Alibaba has more than 100,000 instances of operation, it has been a test of a similar two-eleven other large-scale traffic.

In contrast, the current Nacos Whether deployed, or use both simple to use, more importantly, complete documentation, active community is high.

And Nacos with the current mainstream open source ecosystem provides good support:

  • Nacos is a registered center and distribution center designed for Dubbo born
  • Nacos will be fully compatible with Spring Cloud
  • Nacos Support Service Mesh integration, Kubernetes integration

In addition, Alibaba is through Dubbo + Nacos and a range of open source projects to create a service discovery, service and traffic management, shared services platform, the future also continue to develop and evolve, I believe there will be more on the use of Nacos place .

Nacos combat

Standalone Deployment

Preparing the environment

Nacos rely on Java environment to run, and the need to build the code Nacos when the executable program, but also a Maven environment, so it is necessary to ensure that environmental requirements before deployment:

  • 64 bit OS, support for Linux / Unix / Mac / Windows, recommended use Linux / Unix / Mac.
  • 64 bit JDK 1.8+
  • Maven 3.2.x+

Download and install

download

Nacos most current version 1.0.1, Maven way after packing in the current directory will distribution/targetgenerate two compressed under nacos-server-1.0.1.tar.gzand nacos-server-1.0.1.zipany use to a decompression.

Extracting run

Decompression

Here Nacos stand-alone deployment using the command -m standalone, if the command is Windows, you can double-click startup.cmdthe file to.

run

When the console appears Nacos letters, and appeared Nacos started successfully in stand alone modewhen prompted to explain the Nacos server started successfully, the console gives Nacos directly address the current console accessible http://{ip}:8848/nacos/index.html, click to enter the Nacos visual management interface, you need the account password login access, the default is all nacos.

image-20190616001254325

After logging in you can see Nacos main function menu on the site sidebar: configuration management, service management, cluster management, namespace. For registration and service discovery, we just have to look to focus on service management.

image-20190616001603599

Under Service Management is currently only a sub-menu called the list of services, content presentation is relatively simple, consisting of a service name, the number of instances group, the number of clusters, the total number of instances running.

image-20190616001806599

When a new service is registered on Nacos by Nacos client list items will increase, we can also carry out the details of the service view and edit the press of a button bar.

Cluster deployment

Nacos simple compared to stand-alone deployment, a cluster deployment little trouble, to go along with the official documentation is still a little pits, also need their own additional adjustments. For use in a production environment, we must ensure high availability Nacos, it is still necessary to practice the deployment operation in a cluster.

Prepare the environment with the same stand-alone deployment, an additional requirement is Nacos require three or more three Nacos nodes form a cluster, and uses MySQL as the data source, mainly used for data service configuration persistence.

We look at official recommended cluster deployment architecture diagram, by its domain name, such as Nginx reverse proxy to load multiple Nacos IP nodes, external clients can be directly accessed through the domain name, not only readable, and easy replacement of IP, most recommended.

deployDnsVipMode.jpg

Add the cluster configuration file

Nacos each node confdirectory, add a profile cluster.conf, can refer to the same directory cluster.conf.examplefile, each line of a node configuration and IP ports, such asip:port

Note: The configuration file can not be used 127.0.0.1or localhost, Physical IP or domain name, otherwise start the service could not be registered on the cluster nodes, see NACOS ISSUE # 1189 .

Configure MySQL database

Recommended Nacos production environment database using the recommended at least a standby mode, or employs a highly available database.

Here for simplicity only uses a database. First create a named nacos_configdatabase, using the provided sql statement source file import initial data.

image-20190616003609656

Then in the configuration file for each node Nacos conf/application.propertiesadd the database connection configuration:

application

Finally, the cluster mode is activated separately for each node, and the default background for the start, starting from the information required logs/logs/start.outto obtain the log file.

start.out

When the log file last seen Nacos started successfully in cluster mode.when his party, that description clustered mode Nacos started successfully. At this time, we can see the information of any node cluster management interface through a Nacos console login.

image-20190616015114761

Can be seen from the above, Nacos node cluster is divided into state FOLLOWER, LEADERtwo kinds, we are familiar with the master-slave architecture is similar.

Here, we build a cluster approach also completed. Took over we will look at how to use Nacos conduct service registration and discovery of it.

Implementation of the registration and discovery services

Rest registration and discovery services

The service provider creates

Create a subproject project called rest-providerservice provider programs, pom.xmlconfiguration is as follows:

rest-provider

Note that the spring-cloud-starter-alibaba-nacos-discoveryversion 0.9.0, using Nacos 1.0.0 version of the client, and the corresponding need for the Spring Boot version 2.1.x.RELEASE version, more correspondence between the reference version: Imprint Wiki

image-20190616105233354

Therefore, we use Spring Cloud Alibaba latest version 0.9.0.RELEASE, version corresponds Spring Cloud Greenwich, parent POM Maven project configuration need to add the following dependency management:

pom

After adding dependency, a class implemented in the boot Rest request method /echo, and by @EnableDiscoveryClientannotation indicates an Nacos client, which is primary annotation annotation Spring Cloud provided, even if the registry as Eureka switched also using this annotation.

Bootstrap class

Subproject modify configuration files application.properties

application

  • spring.application.name Declares the service name
  • spring.cloud.nacos.discovery.server-addr Nacos address specified registry

Nacos Starter For more information refer to configuration items Spring Cloud Alibaba Nacos Discovery

Then by the bootstrap class RestProviderApplicationto run the project, log on Nacos Server console to view the service list to see the service provider rest-providerhas been registered.

image-20190616110759521

Create a service consumer

The same way we create a service consumer project rest-consumer, pom.xmldependencies and application.propertiesconfiguration provider-consumer, as here, we explicitly use LoadBalanceClient API and RestTemplate combined to consumer services.

  1. First, add the boot class @EnableDiscoveryClientnotes

    Bootstrap class

  2. Then create a file called RestConsumerConfigurationJava class configuration, injection RestTemplate.

    RestConsumerConfiguration.java

  3. Creating a named RestConsumerControllertest the Controller, a request method echo, as follows:

    RestConsumerController.java

  4. Start rest-cosumerafter the project, you can see the list of services Nacos Server console one more named rest-consumerservices, and access directly in your browser http://localhost:8082/echo, you'll see:

    image-20190616113423983

Use point of view it is not very simple, in fact, and Eureka is not much difference, in addition to this way consumer Rest services, Nacos also supports Feign way we look at it and how to use it.

Feign way consumer Rest Service

First need to introduce Feign clients rely on:

Feign POM

Then guide the class to add annotations @EnableFeignClientsto enable Feign component functionality, a write EchoServiceinterface for remote service call:

EchoService

Get way the interface method parameter request must not use the @RequestParamparameter to specify the request parameter name, otherwise Feign will call a POST, get status code 405 error.

Annotation @FeignClientindicates the service call provider name, echothe method provided by Spring MVC annotation can be associated with the interface of the service provider's Rest, it will call the corresponding request method is executed.

Finally, we only need to write one injection EchoServicecontroller RestConsumerFeignControllerclass that provides an interface call interface.

RestConsumerFeignController

After the same start to directly open http://localhost:8082/echo/hi, the browser will get the following output:

image-20190616142413540

Dubbo registration and discovery services

Next, we look at how to use Nacos Dubbo registered as a service center, to register with the discovery, in fact, as long as the used ZooKeeper will find a very simple way.

First we create a sub-project dubbo-serivce, POM file to add dependencies are as follows:

POM

  • Here we recommend using version 2.6.5 of the official Dubbo integrated Nacos.
  • dubbo-registry-nacos Dubbo use Nacos is a registry key dependence, the current version is 0.0.1.

Then define the service interface DemoService:

DemoService

Producer Service

Or service producers first to add a class that DemoServiceImplimplements the service interface

DemoServiceImpl

Then configure the XML configuration Dubbo, only you need to dubbo:registryconfigure the server address Nacos to the elements.

XML

Finally, a simple class BasicProviderof mainstarting direct service producer method to maintain permanent process.

BasicProvider

After the normal start we can directly in the console's list of services Nacos, Dubbo producer services have successfully registered to Nacos, you can see through the details of specific data services, such as class service port, the method.

image-20190616144317355

Consumer Services

With service producers to look at how to Dubbo by Nacos calling service.

First, the service object by injection Dubbo XML configuration DemoService, the dubbo:registryconfiguration server address Nacos element.

XML

Then write a simple class BasicConsumerin mainloading the Spring container method to get DemoServicethe object, direct RCP calls.

BasicConsumer

After the run, we can see that there have been from the IDE console log output, indicating that the call succeeded by Nacos registry Dubbo service.

IDE console

Epilogue

In this paper, we learn about Nacos, and the use of Nacos service registration and discovery, how and Rest Services, Dubbo service integration use, overall ease of use, interested friends try to use it the next. I will continue in-depth follow-up study Spring Cloud Alibaba micro-services ecosystem components, welcome interested partners can focus on my small micro-channel public number, the more a week.

No public

The sample code

All involved in this paper are in the following code snippet repository, welcome small partners interested in learning reference:

nacos-actionsgithub.com/wrcj12138aa…

Environment Support:

  • JDK 8
  • Spring Boot 2.1.0
  • Maven 3.6.0

reference

Guess you like

Origin juejin.im/post/5d0603a76fb9a07ec373ec2c