Reprint please indicate the source: blog.csdn.net/forezp/arti… This article is from Fang Zhipeng’s blog

What is a Nacos?

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Is A service registry discovery component in Spring Cloud A, similar to Consul and Eureka. It also provides A distributed configuration center function, similar to Consul’s config, which supports hot loading.

Key features of Nacos include:

  • Service discovery and service health monitoring
  • Dynamic configuration service, with management interface, supports rich configuration dimensions.
  • Dynamic DNS Service
  • Services and their metadata management

Nacos download

Nacos relies on the Java environment, so it must be installed. Then download the Nacos decompression package from the official website, install the stable version, download the address: github.com/alibaba/nac…

In this case, the 1.0.0 version is downloaded. Decompress the decompressed file. In the /bin directory of the decompressed file, press startup. CMD in Windows to start nacOS. For Linux or MAC, run the following command to start nacOS.

sh startup.sh -m standalone
Copy the code

Logs will be printed on the console during startup. The boot port of nacOS is 8848. Ensure that port 8848 is not occupied during startup. Humulama’s height is 8844 and NacOS’s port is 8848, somewhat coincidentally.

Start-up success, on the browser visit: http://localhost:8848/nacos, will jump to the login page, the default login user name nacos, password for nacos.

After a successful login, the following interface is displayed:

According to the interface, no service is registered with Nacos at this time.

Register and discover using the Nacos service

Service registration and discovery is the foundation of microservice governance. Service registration and discovery components are the soul of the entire microservice system. It is of great importance to select appropriate service registration and discovery components. With Eureka’s closed source, The Spring Cloud Netflix-OSS component has entered the maintenance phase on a large scale and no longer provides new functions. Spring Cloud Alibaba is strongly supported by the open source community.

The service registry

In this case, two services are registered with Nacos, namely, NACOS-Provider and NACOS-Consumer.

Build the service provider nacOS-Provider

Create a new Spring Boot project, the Spring Boot version is 2.1.4.RELEASE, and the Spring Cloud version is Greenwich.RELEASE.

<dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - discovery < / artifactId > < version >. 0.9.0 RELEASE < / version > < / dependency >Copy the code

Make relevant configuration in the project configuration file application.yml as follows:

Server: port: 8762 Spring: Application: name: nacos-provider cloud: nacos: Discovery: server-addr: 127.0.0.1:8848Copy the code

In the above configuration, the program starts on port 8762, the application name is nacos-provider, and the address registered with the NACOS server is 127.0.0.1:8848.

Then annotate @enableDiscoveryClient in the NacosProviderApplication file of Spring Boot as follows:


@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

	public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); }}Copy the code

Build service consumer Nacos-Consuer

As with nacOS-provider, build the service consumer nacOS-Consumer, nacOS-Cosumer’s startup port 8763. The build process is the same as nacOS-Provider, which is omitted here.

The validation service registers a discovery

After the project is successfully started, the nacOS-Provider and nacOS-consumer are registered with nacOS-server when accessing localhost:8848, as shown in the following figure:

The service call

When nacOS acts as a service registration and discovery component, you can choose RestTemplate and Feign for service consumption. This is the same as registering and discovering components using Eureka and Consul as services and is no different. This is because the Spring-cloud-starter-Alibaba-nacos-Discovery dependency implements the relevant interface for Spring Cloud service registration and discovery, and can be seamlessly switched with other service registration discovery components.

To provide services

In the nacOS-provider project, write a Controller to provide the API service as follows:


@RestController
public class ProviderController {

Logger logger= LoggerFactory.getLogger(ProviderController.class);

@GetMapping("/hi")
public String hi(@RequestParam(value = "name",defaultValue = "forezp",required = false)String name){

        return "hi "+name; }}Copy the code

Consumer services

There are two ways to consume services here, RestTemplate and Feign.

Use RestTemplate to consume the service

RestTemplate can use Ribbon as a load balancing component to introduce Ribbon dependencies in the NACOS-Consumer project:


<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>


Copy the code

Inject the RestTemplate Bean in the NacosConsumerApplication startup file as follows:

	@LoadBalanced
	@Bean
	public RestTemplate restTemplate(a){
		return new RestTemplate();
	}

Copy the code

To enable LoadBalanced load balancing on the RestTemplate, add the @loadBalanced annotation.

Write a ConsumerController for the service as follows:


@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

 @GetMapping("/hi-resttemplate")
    public String hiResttemplate(a){
        return restTemplate.getForObject("http://nacos-provider/hi? name=resttemplate",String.class);

    }
Copy the code

Restart the project and visit http://localhost:8763/hi-resttemplate in the browser. The correct response is displayed in the browser. Nacos-consumer successfully invokes the nacos-Provider service.

FeignClient invokes the service

Introduce the following dependencies in the NACos-Consumer POM file:

<dependency>
	<groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Copy the code

EnableFeignClient by annotating @enableFeignClients to the NacosConsumerApplication startup file.

@EnableFeignClients
public class NacosConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosConsumerApplication.class, args);
	}

Copy the code

Create a FeignClient to invoke the nacOS-Provider service as follows:

@FeignClient("nacos-provider")
public interface ProviderClient {

    @GetMapping("/hi")
    String hi(@RequestParam(value = "name", defaultValue = "forezp", required = false) String name);
}

Copy the code

Write a consumer API that uses ProviderClient to invoke the NACOS-Provider API service as follows:



@RestController
public class ConsumerController {


    @Autowired
    ProviderClient providerClient;

    @GetMapping("/hi-feign")
    public String hiFeign(a){
       return providerClient.hi("feign"); }}Copy the code

Restart the project and visit http://localhost:8763/hi-feign in the browser. The correct response is displayed in the browser. Nacos-consumer successfully invokes the nacos-Provider service.

conclusion

This article describes in some detail how to use Nacos as a service registry, and uses cases to show how to consume services when using Nacos as a service registry. The next tutorial will show you how to use NACOS as a distributed configuration hub.

Download the source code

Github.com/forezp/Spri…

The resources

Nacos. IO/useful – cn/docs /…



Scan and support the author

(Please indicate the author and source of the article reproduced from the blog of Fang Zhipeng)