Loading

Nacos服务提供者注册

1.Nacos服务注册中心

服务发现是微服务架构中的关键组件之一。在这样的架构中,手动为每个客户端配置服务列表可能是一项艰巨的任务,并且使得动态扩展极其困难。Nacos Discovery 帮助您自动将您的服务注册到 Nacos 服务器,Nacos 服务器会跟踪服务并动态刷新服务列表。此外,Nacos Discovery 将服务实例的一些元数据,如主机、端口、健康检查 URL、主页等注册到 Nacos。

官方文档:https://spring.io/projects/spring-cloud-alibaba#learn

2.服务注册

创建聚合项目:由于聚合带来的诸多好处,在SpringBoot项目开发中也广泛采用,开发中将SpringBoot项目按照功能分成子模块开发,所以我们在使用Spring Cloud Alibaba完成项目的时候,也是采用聚合项目来完成。

父POM

<?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.6.4</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <groupId>com.zt.studydemo</groupId>
 <artifactId>study_demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>study_demo</name>
 <packaging>pom</packaging>
 <description>study_demo</description>

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

 <modules>
     <module>nacos-9001</module>
 </modules>

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

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

 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>2021.0.1</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>

         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-alibaba-dependencies</artifactId>
             <version>2.2.7.RELEASE</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

</project>

子项目POM

<?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>com.zt.studydemo</groupId>
     <artifactId>study_demo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <artifactId>nacos-9001</artifactId>
 <name>nacos-9001</name>
 <description>nacos-9001</description>

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

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

     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
     </dependency>
 </dependencies>

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

</project>

YML文件

server:
port: 9001
spring:
application:
	# 服务名
 name: nacos-provider
cloud:
 nacos:
   discovery:
   	# 注册中心地址
     server-addr: 192.168.56.10:8848
management:
endpoints:
 web:
   exposure:
     include: '*'

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class Nacos9001Application {

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

}

接口:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

 @Value("${server.port}")
 private String serverPort;

 @GetMapping(value = "/nacosdemo")
 public String getServerPort() {
     return "Hello Nacos Discovery" + serverPort;
 }

}

测试结果:

image

posted @ 2022-05-21 00:08  ZT丶  阅读(77)  评论(0编辑  收藏  举报