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 does SpringBoot connect to Nacos?

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of SpringBoot how to access Nacos, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this SpringBoot article on how to access Nacos. Let's take a look at it.

Introduction:

Nacos supports DNS-based and RPC-based service discovery (which can be used as a registry for springcloud). This paper will configure the registry by combining theory with practice.

1.Nacos introduction

If you check the official website https://nacos.io/, you can see it on the home page.

1). Nacos is an easy-to-use dynamic service discovery 2). Nacos provides a management service management platform for applications

Deployment of 2.Nacos

1) go to the Nacos download page and download the server of Nacos

If windows downloads the zip package, if it is Linux, download the tar.gz package

The decompression is completed as shown in the image above.

2) if you are a window user, double-click startup.cmd directly

If it is Linux/Unix/Mac, execute the command sh startup.sh-m standalone

Open the browser http://localhost:8848/nacos/#/login

Enter the default account nacos password nacos

You can log in to the main interface

Click on the service management on the left to see the service list page

3.SpringBoot project connects to Nacos

1) introduction of dependency com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2) configuration to add @ EnableDiscoveryClient annotations to the application.java of the project as shown below @ SpringBootApplication @ EnableDiscoveryClient public class ProviderApplication {

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

} 3) add Nacos de configuration to / src/main/resources/application.properties and name spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.application.name=mail-coupon for the application

4) just start the springBoot project, and see the launched service on the management page.

The 4.SpringBoot project calls other projects in the Nacos

1) introduce declarative remote invocation dependency openFegin (https://spring.io/projects/spring-cloud-openfeign) org.springframework.cloud spring-cloud-starter-openfeign2) to add the test interface @ RequestMapping ("/ member/list") public R membecoupons () {CouponEntity couponEntity = new CouponEntity (); couponEntity.setCouponName ("100 minus 10"); return R.ok () .put ("coupons", Arrays.asList (couponEntity)) } 3) establish the interface CouponFeignService through @ FeignClient ("mail-coupon") specify the full address @ FeignClient ("mail-coupon") public interface CouponFeignService {to invoke the service corresponding to Nacos via @ RequestMapping ("/ coupon/coupon/member/list").

@ RequestMapping ("/ coupon/coupon/member/list") public R membecoupons ();} 4) add the package @ EnableFeignClients (basePackages = "person.zxc.mall.member.feign") to application.java to open the remote call and specify the interface) set up a test interface in the member service to test the value obtained from the member service

@ RequestMapping ("/ coupons") public R test () {MemberEntity memberEntity = new MemberEntity (); memberEntity.setNickname ("Zhang San")

R membecoupons = couponFeignService.membecoupons ()

Return R.ok () .put ("member", memberEntity) .put ("coupons", membecoupons.get ("coupons"));}

6) finally visit the member service interface

Http://localhost:8000/member/member//coupons

As you can see, the full 100 minus 10 information obtained from the coupon service

This is the end of the article on "how to access SpringBoot to Nacos". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to access SpringBoot to Nacos". If you want to learn more, you are welcome to follow the industry information channel.

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

Servers

Wechat

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

12
Report