[Alibaba微服务技术入门]_服务的注册_第2讲
Spring Cloud Alibaba Nacos Discovery
服务注册:通过 spring-cloud-starter-alibaba-nacos-discovery 自动配置为Spring Boot应用程序在服务注册与发现方面提供和Nacos的无缝集成,在通过一些简单的注解,就可以快速的实现服务注册与发现
搭建项目环境
1、创建项目父工程,名称:alibaba2,父工程的pom.xml文件代码如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>1-nacos-provider9001</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> </parent> <groupId>com.liuyangjava</groupId> <artifactId>alibaba</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
2、创建 1-nacos-provider9001 子工程,即:编写一个服务提供者
(1)向 pom.xml 文件中加入 spring-cloud-starter-alibaba-nacos-discovery 依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>alibaba</artifactId> <groupId>com.liuyangjava</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.liuyangjava</groupId> <artifactId>1-nacos-provider9001</artifactId> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
(2)创建 application.yml 文件,配置服务端口号;注册中心地址;应用程序名称
server:
port: 9001
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: nacos-provider
management:
endpoints:
web:
exposure:
include: '*'
(3)创建 NacosProvider9001 启动类,添加 @SpringBootApplication;@EnableDiscoveryClient 注解,启动应用程序
package com.liuyangjava; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class NacosProvider9001 { public static void main(String[] args) { SpringApplication.run(NacosProvider9001.class, args); } }
注:后台提示 nacos registry, DEFAULT_GROUP nacos-provider9001 192.168.9.5:9001 register finished,则表示此应用程序成功注册到 Nacos 服务中
最后,打开Nacos的后台,可以查看服务是否注册成功
(4)创建 NacosProviderController
package com.liuyangjava.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class NacosProviderController { @Value("${server.port}") private String port; @GetMapping("/nacos/provider") public String getInfo() { return "nacos register, server port: " + port; } }