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
s
for 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
Nacos most current version 1.0.1, Maven way after packing in the current directory will distribution/target
generate two compressed under nacos-server-1.0.1.tar.gz
and nacos-server-1.0.1.zip
any use to a decompression.
Extracting run
Here Nacos stand-alone deployment using the command -m standalone
, if the command is Windows, you can double-click startup.cmd
the file to.
When the console appears Nacos letters, and appeared Nacos started successfully in stand alone mode
when 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
.
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.
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.
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.
Add the cluster configuration file
Nacos each node conf
directory, add a profile cluster.conf
, can refer to the same directory cluster.conf.example
file, each line of a node configuration and IP ports, such asip:port
Note: The configuration file can not be used
127.0.0.1
orlocalhost
, 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_config
database, using the provided sql statement source file import initial data.
Then in the configuration file for each node Nacos conf/application.properties
add the database connection configuration:
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.out
to obtain the log file.
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.
Can be seen from the above, Nacos node cluster is divided into state FOLLOWER
, LEADER
two 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-provider
service provider programs, pom.xml
configuration is as follows:
Note that the spring-cloud-starter-alibaba-nacos-discovery
version 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
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:
After adding dependency, a class implemented in the boot Rest request method /echo
, and by @EnableDiscoveryClient
annotation indicates an Nacos client, which is primary annotation annotation Spring Cloud provided, even if the registry as Eureka switched also using this annotation.
Subproject modify configuration files application.properties
spring.application.name
Declares the service namespring.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 RestProviderApplication
to run the project, log on Nacos Server console to view the service list to see the service provider rest-provider
has been registered.
Create a service consumer
The same way we create a service consumer project rest-consumer
, pom.xml
dependencies and application.properties
configuration provider-consumer
, as here, we explicitly use LoadBalanceClient API and RestTemplate combined to consumer services.
-
First, add the boot class
@EnableDiscoveryClient
notes -
Then create a file called
RestConsumerConfiguration
Java class configuration, injectionRestTemplate
. -
Creating a named
RestConsumerController
test the Controller, a request methodecho
, as follows: -
Start
rest-cosumer
after the project, you can see the list of services Nacos Server console one more namedrest-consumer
services, and access directly in your browserhttp://localhost:8082/echo
, you'll see:
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:
Then guide the class to add annotations @EnableFeignClients
to enable Feign component functionality, a write EchoService
interface for remote service call:
Get way the interface method parameter request must not use the
@RequestParam
parameter to specify the request parameter name, otherwise Feign will call a POST, get status code 405 error.
Annotation @FeignClient
indicates the service call provider name, echo
the 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 EchoService
controller RestConsumerFeignController
class that provides an interface call interface.
After the same start to directly open http://localhost:8082/echo/hi
, the browser will get the following output:
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:
- 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
:
Producer Service
Or service producers first to add a class that DemoServiceImpl
implements the service interface
Then configure the XML configuration Dubbo, only you need to dubbo:registry
configure the server address Nacos to the elements.
Finally, a simple class BasicProvider
of main
starting direct service producer method to maintain permanent process.
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.
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:registry
configuration server address Nacos element.
Then write a simple class BasicConsumer
in main
loading the Spring container method to get DemoService
the object, direct RCP calls.
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.
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.
The sample code
All involved in this paper are in the following code snippet repository, welcome small partners interested in learning reference:
nacos-actions:github.com/wrcj12138aa…
Environment Support:
- JDK 8
- Spring Boot 2.1.0
- Maven 3.6.0
reference
- Dubbo support ecological development, Alibaba to launch a new open source project Nacos: yq.aliyun.com/articles/60...
- Nacos official document: nacos.io/zh-cn/docs/...
- Spring Cloud services to find new choice - Alibaba Nacos Discovery: www.bilibili.com/video/av321...
- Cluster deployment instructions: nacos.io/zh-cn/docs/...
- BREAD # 1189: github.com/alibaba/nac...
- Spring Cloud how to use Feign construct multi-parameter request: www.jianshu.com/p/7ce46c0eb...