1.初始化项目
1.1.在gitee上新建项目
1.2.在idea中导入项目
1.1. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspringcloud</groupId>
<artifactId>myspringcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myspringcloud</name>
<description>聚合服务</description>
<packaging>pom</packaging>
<!--将子项目放到聚合项目中-->
<modules>
<module>user-center</module>
</modules>
</project>
1.2. .gitignore文件上传忽略文件
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
**/mvnw
**/mvnw.cmd
**/.mvn
**/target/
.idea
**/.gitignore
2.用户中心
2.1.新建module
2.2.pom.xml
springboot版本和spring cloud alibaba的版本关系注意,避免踩坑,如果不符合要求,服务是无法注册到nacos中的,我这里用的是springboot 2.2.11,alibaba 是2.2.1.
<?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.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myspringcloud</groupId>
<artifactId>user-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-center</name>
<description>用户中心</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--nacos服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos配置中心做配置管理的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3.bootstrap.yml (resources目录下)
server:
port: 8080
#向nacos注册中心注册服务
spring:
cloud:
nacos:
#配置nacos注册中心
discovery:
server-addr: localhost:8848
#配置nacos配置中心
config:
file-extension: yaml
server-addr: 127.0.0.1:8848
#配置服务名
application:
name: user-center
#通过以下名称匹配不同的开发和生产环境
#如果不写则当前匹配 user-center.yaml
#若写了则匹配 user-center-dev.yaml 或 user-center-dev.yaml
profiles:
active: dev #代表开发环境
#active: pro #代表生产环境
2.4.启动项:UserCenterApplication
记得加 @EnableDiscoveryClient
package com.myspringcloud.usercenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserCenterApplication {
public static void main(String[] args) {
SpringApplication.run(UserCenterApplication.class, args);
}
}
2.5.测试Controller
加上这个注解可以实时获取nacos上的配置信息@RefreshScope,否则项目一运行,你修改配置文件将不生效
package com.myspringcloud.usercenter.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class index {
@Value("${server.port}")
private String port;
@Value("${test.message}")
private String message;
@RequestMapping("/index")
public String index(){
return "测试:"+message+" 端口号为:"+port;
}
}
2.6 nacos注册中心
Data ID如何命名才能让项目默认匹配可以看下 2.3里的注解