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 a microservice using SpringCloudAlibaba and Nacos

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how to use SpringCloudAlibaba and Nacos to build a micro-service, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Introduction to SpringCloudAlibaba

Spring Cloud Alibaba is committed to providing one-stop solutions for microservice development. This project contains the necessary components for the development of distributed application micro-services, making it easy for developers to use these components to develop distributed application services through the Spring Cloud programming model. Relying on Spring Cloud Alibaba, you only need to add some annotations and a small amount of configuration to connect the Spring Cloud application to the Ali micro service solution, and quickly build the distributed application system through Ali middleware. [more]

Introduction to Nacos

Nacos is dedicated to helping you discover, configure, and manage micro services. Nacos provides an easy-to-use set of features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Nacos helps you build, deliver, and manage micro-service platforms more quickly and easily. Nacos is a service infrastructure for building modern application architectures with "service" as the center, such as micro-service paradigm and cloud native paradigm. [more]

Construction of Microservice starter instance based on SpringCloudAlibaba and Nacos

Description: to query bank card information as the business background, write service provider services.

1. Write a service provider: myclouds-nacos-discovery-provider-example1.1. Service code @ RestController@RequestMapping ("bankCard") public class BankCardController {@ GetMapping ("findCardInfo") public BankCard findCardInfo (String cardNo, HttpServletRequest request) {System.out.println (cardNo); BankCard bankCard = new BankCard () .setCardNo ("5301001"). SetName ("XC") .setBalance (BigDecimal.valueOf (1000000L)); return bankCard;}}

1.2. Service startup class

@ EnableDiscoveryClient@SpringBootApplicationpublic class ProviderApplication {public static void main (String [] args) {SpringApplication.run (ProviderApplication.class, args);}}

1.3. Configuration files and other project dependencies

You can access the instance source code directly.

two。 Write service consumers: myclouds-nacos-discovery-consumer-example2.1. Service consumption interface @ FeignClient (value = "myclouds-nacos-discovery-provider-example") public interface BankCardFeignClient {@ GetMapping ("bankCard/findCardInfo") BankCard findCardInfo (@ RequestParam ("cardNo") String cardNo);} 2.2. Service consumption code @ RestController@RequestMapping ("bankcard") public class BankCardConsumerController {@ Autowired private BankCardFeignClient bankCardFeignClient; @ GetMapping ("findCardInfo") public BankCard findCardInfo (String cardNo) {BankCard bankCard = bankCardFeignClient.findCardInfo (cardNo); System.out.println (bankCard); return bankCard;}}

2.3. Consumer service startup class

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

2.4. Configuration files and other project dependencies

You can access the instance source code directly.

3. Service Registration & Discovery

3. 1 start nacos.

Startup.cmd

Nacos console: http://127.0.0.1:9999/nacos account / password: nacos/nacos

3.2 start the service provider and the service consumer respectively

Go to the nacos console and click "Service Management"-> "Service list" menu to see the information about the provider service and consumer service of the bank card query service in this example. This is shown in the following figure.

4. Verification service

Access the service through the service consumer: http://127.0.0.1:19000/bankcard/findCardInfo?cardNo=5301

So much for sharing about how to build a microservice using SpringCloudAlibaba and Nacos. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report