Getting started with Nacos+SpringBoot microservices

About Nacos:

The installation and introduction of nacos can be viewed on the official website. Nacos is a product launched by Ali, and the documentation is also in Chinese. Address: nacos official website I
personally recommend using Docker. Those who don’t know how to use it can take a look at this video tutorial. Address: Mad God said JAVA
This is the docker video tutorial uploaded by the main Mad God said JAVA at station B. It feels easy to understand. Those who are interested can take a look.

Nacos uses:

1. If it is installed locally, visit after successful startup: localhost:8848/nacos can enter the home page of nacos, there is only one public namespace by default

Insert picture description here

2. Create a new namespace, it is best to build according to the project name, I will name it with my own name here

Insert picture description here

3. After saving, return to the menu "Configuration Management"-"Configuration List", this time there is a new namespace added above

Insert picture description here

4. Switch to the namespace you just created, click on the "+" on the right, write the configuration file, and save it

Insert picture description here
Insert picture description here

Now we start to write the code to see how our code reads the configuration file from naocs and registers it. The only port number in our nacos configuration file is: 8088

5. Build a basic springboot project, import dependencies, pom.xml is as follows, pay attention to the version number

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--nacos依赖-->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
			<version>2.2.1.RELEASE</version>
		</dependency>

		<!--nacos依赖-->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
			<version>2.2.1.RELEASE</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

6. Newly added bootstrap.yml configuration file, and the rules are explained in detail in naocs official website. I will make a brief comment here.

# 我们在nacos中编写的配置文件名是 service-api-dev.yaml
spring:
  application:
    name: springboot-api # 这边写什么 注入到nacos服务列表中的服务名就是什么
  profiles:
    active: dev # 读取配置文件中
  cloud:
    nacos:
      config:
        server-addr: 192.168.1.54:8848 # nacos地址根据自己配置的地址修改 本地就是localhost
        group: DEFAULT_GROUP # nacos中配置管理-配置列表中的Group
        prefix: service-api # Data Id 前缀
        file-extension: yaml # 同上 后缀
        namespace:  MaoJiaFeng # 这边跟上面第二张图片标红色箭头的必须一致
        refreshable-dataids:
      discovery:
        server-addr: 192.168.1.54:8848 # 注册到nacos
        namespace:  MaoJiaFeng # 同上

7. Remember to add the opening nacos comment on the startup item, start the project, you can see that the current startup is dev, and the port number is 8088

Insert picture description here

There is still the last step here, to check whether our service is successfully injected into nacos, or click on the details to view the specific information of the service

Insert picture description here
Source address: https://gitee.com/mao_jiafeng/springboot-demo-nacos.git
If there is something wrong, welcome to discuss QQ:770850769

Guess you like

Origin blog.csdn.net/weixin_45452416/article/details/109679818