一娃善、Nacos服務提供者
Nacos兼具注冊中心
二焚虱、前期代碼準備
后續(xù)Spring Cloud Alibaba開發(fā)都是基于本套代碼上開發(fā)
2.1订框, 構架Maven項目箭跳,取名sca-partner
- 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>
<groupId>com.ydw</groupId>
<artifactId>sca-partner</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 子工程 -->
<modules>
<module>sca-service-8081</module>
</modules>
<!--父工程打包方式為pom-->
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencyManagement>
<dependencies>
<!--SCA -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
<!--SCA -->
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web依賴-->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!--編譯插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 配置是否被傳播到子POM晨另,后續(xù)dubbo的api服務會因為pom打包方式導致無法install -->
<inherited>false</inherited>
</plugin>
</plugins>
</build>
</project>
三、Nacos服務提供者開發(fā)
3.1谱姓,構架SpringBoot項目借尿,取名sca-service-8081(取名建議帶上端口號,后續(xù)微服務太多方便查詢)
- 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">
<parent>
<artifactId>sca-partner</artifactId>
<groupId>com.ydw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sca-service-8081</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
- application.yml
server:
port: 8081 # 端口
spring:
application:
name: sca-service # 服務名,同是也會作為Nacos注冊中心的服務名
# nacos配置
cloud:
nacos:
discovery:
# 集群中各節(jié)點信息都配置在這里(集群用路翻,隔開)
server-addr: 127.0.0.1:8848
- ScaService8081Application.java啟動類
package com.ydw.sca;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //通用注冊中心注解
public class ScaService8081Application {
public static void main(String[] args) {
SpringApplication.run(ScaService8081Application.class, args);
}
}
-
啟動成功后狈癞,會在注冊中心上面看到服務名