Nacos, which is used in Spring Cloud Alibaba, is described in the official documentation:
Nacos is an easy-to-use platform for dynamic service discovery, configuration, and management when building cloud-native applications.
With Spring Cloud Alibaba Nacos Discovery, you can quickly access Nacos service registration through Spring Cloud’s programming model.
Nacos implements service discovery, configuration, and management based on the following figure
Nacos Discovery service registration and Discovery
Service registration finds resolved problems
In microservices architecture, multiple services often need to call each other. Without a registry, each service has to maintain a list of services that it needs to call, which is very cumbersome, non-dynamic, and difficult to scale.
Nacos is used to solve this problem as a registry. The service provider automatically registers the service with the Nacos server, which maintains and dynamically refreshes the list of services, registering the service instance metadata: host address, port, health check address, Nacos administration home page.
Install and start Nacos
See Nacos installation and startup
The default user name and password are nacos and nacos
Introduce into the project
Nacos service has been started, and service providers and service consumers need to register themselves in Nacos actively in order to be called by the services registered in Nacos and the services called in Nacos.
How does this registration process work?
-
Spring – Cloud -starter- Alibaba – Nacos-Discovery is introduced into service applications
<! Start the project as a Spring Boot Web project --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! -- For service health check --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <! -- To register yourself with Nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> Copy the code
-
Configure the Nacos service address and the name to register the service as
spring: cloud: nacos: discovery: server-addr: 192.168162.1.: 8848 application: name: nacos-provider # Web endpoint exposure management: endpoints: web: exposure: include: "*" Copy the code
-
Add an @enableDiscoveryClient annotation to the classic Spring Boot application configuration
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } @RestController public class EchoController{ @GetMapping("/echo/{string}") public String echo(@PathVariable String string){ return "Hello Nacos Discovery "+ string; }}}Copy the code
After successful startup, you can see that the service has been registered under the service list on the Nacos management page:
A small pit
During startup, I did not pay attention to the corresponding version of Spring Cloud and Spring Boot, and errors were reported all the time. You can refer to the result returned by this link to get the corresponding version of Spring Cloud and Spring Boot
Service consumer
The dependency configuration of the service provider is also required by the service consumer in order to access the services provided by the service provider through the registry.
After checking maven dependency, it is found that the spring-Cloud-starter-Alibaba-Nacos-Discovery module relies on the Spring-Cloud-starter-Netflix-ribbon module. Ribbon provides load balancing functions. We can inject the RibbonLoadBalancerClient to obtain the services in the Nacos registry and then use Spring’s RestTemplate to access:
// Spring does automatic configuration, direct injection
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
RestTemplate restTemplate; // Omit the process of creating a bean
@GetMapping("/echo/app-name")
public String echoAppName(a){
// Load balancing takes a service provider service instance
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
// Access with RestTemplate
String requestUrl = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), applicationName);
return restTemplate.getForObject(requestUrl, String.class);
}
Copy the code
After starting the consumer, you can start multiple service providers on different ports (IDEA needs to edit the startup configuration, select Allow Parallel Run) to access the external interface of the consumer, and you can find that the requests are evenly distributed to different service providers.
Nacos Discovery endpoint
For clients connected to Nacos Discovery, such as service providers and service consumers, Spring Cloud Alibaba Nacos Discovery provides an EndPoint:
http://ip-addr/actuator/nacos-discovery
Copy the code
You can return a list of services to which the service subscribes and the nacOS Discovery configuration information for that service.
This EndPoint is implemented through the EndPoint in the Spring Boot Actuator module. The implementation class is NacosDiscoveryEndpoint
/ / NacosDiscoveryEndpoint. Java / / core code (omitted) public Map < String, Object> nacosDiscovery() {// nacosDiscovery will generate a local NamingService instance for the registered service. Through this interface can go to a subscription service NamingService NamingService = nacosDiscoveryProperties. NamingServiceInstance (); / / here had NamingService using the subscription service. List the subscribe = NamingService getSubscribeServices (); }Copy the code
conclusion
- Nacos was installed and started as instructed by the official documentation
- Practice Nacos service discovery, service providers, service consumers through
spring cloud alibaba nacos discovery
The module + configuration registers the Nacos server to be started and can load balance calls to each other - Nacos Discovery generates an instance of NamingService locally
spring boot actuator
To obtain Nacos Discovery configuration information and a list of subscribed services through NamingService
Try Nacos Config configuration Management in the next article
reference
Spring – the cloud – alibaba – group. Making. IO/lot – page…
www.funtl.com/zh/spring-c…
Nacos. IO/useful – cn/docs /…