A, Nacos Profile
Why is it called Nacos?
The first four letters are the first two letters of Naming and Configuration, s is the last Service. Therefore, the composition of NACOS .
What is?
Nacos is an easy to build dynamic cloud-native application service discovery, configuration management and service management platform. Use Spring Cloud Alibaba Nacos Discovery, you can Spring Cloud-based programming model for quick access Nacos Service registration.
-
Nacos:Dynamic Naming and Configuration Service。
-
Nacos: in fact, a combination of Eureka + Config service registry service configuration center.
Second, install and run Nacos
Where to download?
After entering Nacos official website, then click the Release Note of V1.2.0 version:
Run Nacos:
After extracting installation package Nacos success, to find their own download services Nacos bin directory, there is a startup.cmd command, double click start to run:
In this case, a standard stand-alone mode Nacos service started successful, Nacos Tomcat default port number is 8848. 8848 saw this figure, involuntary reminds me a long time ago advertise regressed 8848 Titanium phone, Tucao wave! ! !
How to verify that it ran successfully?
Very simple, direct access to the command runs successfully HTTP: // localhost: 8848 / nacos / , into Nacos Home management interface, see Nacos Logo After this interface, on behalf of your Nacos service without any problems, so the interface a bit like we usually do admin page of the project.
I'm using version 1.2.0, you do not need to enter Nacos login page can be used. At this point no matter which menu are no data to show!
Third, the service provider based Nacos
Creating gradle module provider-nacos and add the web, actuator monitoring and alibaba-nacos-discovery dependence
dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' compile group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery', version: '2.1.0.RELEASE' }
Server: Port: 8081 the Spring: the Application: name: Provider-nacos Cloud: nacos: Discovery: Server-addr: 127.0.0.1:8848 # Nacos service address specified Management: Endpoints: Web: Exposure: the include: '*'
3) Start class ProviderNacosApplication.java
package org.wesson.cloudalibaba.nacos; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ProviderNacosApplication { public static void main(String[] args) { SpringApplication.run(ProviderNacosApplication.class, args); } }
package org.wesson.cloudalibaba.nacos.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/client") public class ProviderNacosController { @Value("${server.port}") private String serverPort; @GetMapping("/info") public String info() { return "hello, nacos registry center serverPort:" + serverPort; } }
Step1: above the service has been successfully started Nacos
Step2: directly run provider-nacos startup class, port 8081
Step3: first visit HTTP: // localhost: 8081 / Client / info , output results are as follows:
-
hello, nacos registry center serverPort:8081
Step4: then visit HTTP: // localhost: 8848 / nacos , find the list of services under service management, you can see a service application called provider-nacos registered to Nacos the service management page:
At this point, Nacos + service registry service providers to build success.
Fourth, consumer services based Nacos
1) build.gradle project dependencies
dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' compile group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery', version: '2.1.0.RELEASE' }
Server: Port: 8000 the Spring: the Application: name: Consumer-nacos Cloud: nacos: Discovery: Server-addr: 127.0.0.1:8848 # Nacos service address specified Management: Endpoints: Web: Exposure: the include: '*' Service-url : provider-nacos: HTTP: micro-services // provider-nacos # name consumers will pay a visit to the (registered into the micro Nacos service provider)
package org.wesson.cloudalibaba.nacos; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class ConsumerNacosApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerNacosApplication.class, args); } }
package org.wesson.cloudalibaba.nacos.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/client") public class ConsumerNacosController { private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerNacosController.class); @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalancerClient; @Value("${service-url.provider-nacos}") private String consumerServiceUrl; @GetMapping("/info") public String info() { // getForObject Method return restTemplate.getForObject (consumerServiceUrl + "/ Client / info", String. Class ); } @GetMapping ( "/ log-info-instance" ) public void the logInfo () { ServiceInstance ServiceInstance = the this .loadBalancerClient.choose ( "Provider-nacos" ); // Print which node is selected ConsumerNacosController.LOGGER.info ( "{}: {}: {}" , serviceInstance.getServiceId (), serviceInstance.getHost (), serviceInstance.getPort ( )); } }
5) Test
Step1: above the service has been successfully started Nacos
Step2: Run provider-nacos start classes two instances, ports 8081,8082
Step3: Run consumer-nacos startup class, port 8000
Step4: many visits to HTTP: // localhost: 8000 / Client / info , returned the following results:
-
hello, nacos registry center serverPort:8081
Step5: many visits to HTTP: // localhost: 8000 / Client / log-info-instance , the console will print the following log information:
6) Why Nacos support load balancing?
Because Ali behind technology integration is very good, Spring Cloud Alibaba will absorb technology advantages of the previous Spring Cloud Netflix technology, it naturally comes with default load balancing. We open the far right of the IDEA find Gradle dependent on:
As long as it used the Netflix Ribbon is clear, first support load balancing (Load Balance), the second can call RestTemplate, perform remote REST style calls.