Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build spring cloud alibaba micro-service to realize Nacos service provider registration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to build spring cloud alibaba micro-services to achieve Nacos service provider registration". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Create sub-project: cloudalibaba-provider-payment9001

two。 Configure pom

Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.projectlombok lombok true org.springframework.boot spring-boot-devtools runtime true org.springframework. Boot spring-boot-starter-test test

3. Create a yml file with the following configuration:

Server: port: 9001spring: application:name: nacos-payment-provider cloud:nacos: discovery:server-addr: localhost:8848 # configure Nacos address management: endpoints:web: exposure:include:'*'# Monitoring

4. Create a startup class:

@ SpringBootApplication@EnableDiscoveryClientpublic class PaymentApplication9001 {public static void main (String [] args) {SpringApplication.run (PaymentApplication9001.class, args);}}

5. Access after starting the service: http://localhost:8848/nacos/

6. Follow the same steps above to create a subproject: cloudalibaba-provider-payment9002

Configuration is the same as above

Visit again after starting the service: http://localhost:8848/nacos/

Both services have been registered successfully!

7. Next, let's take a look at the natural load support of nacos, because nacos integrates ribbon and is creating a subproject: cloud-consumer-order80

A brief introduction to the yml configuration file

Server: port: 83spring: application:name: nacos-order-consumer cloud:nacos: discovery:server-addr: localhost:8848# the name of the microservice that the consumer is going to access (the microservice provider successfully registered with nacos). If the accessed service is configured here, the business class does not have to define constants service-url: nacos-user-service: http://nacos-payment-provider

Download other code from github: https://gitee.com/xiaowei_328/cloud2020.git

8. Start the service port 83pc9001Pol 9002 respectively

Visit: http://127.0.0.1:83/consumer/payment/nacos/1

Refresh request again

From the results, we can see that nacos also supports load.

Comparison of nacos with other registries

When to choose which mode

Generally speaking, if you do not need to store service-level information and the service instance is registered through nacos-client and can keep the heartbeat reported, you can choose AP mode. The current mainstream services such as spring cloud and Dubbo services apply AP mode, and AP mode weakens the consistency for the availability of services, so AP mode only supports registration of temporary instances.

If you need to edit or store configuration information at the service level, then it must be CP,K8S and DNS services that apply to CP mode.

Registration of persistent instances is supported in CP mode. In this case, the Raft protocol is in cluster operation mode. In this mode, the service must be registered before registering the instance. If the service does not exist, an error will be returned.

This is the end of the content of "how to build spring cloud alibaba microservices to achieve Nacos service provider registration". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report