一:创建项目 project
①:第一步选择 Create New Project 也可以在Idea中选择File -> New ->project
②:第二步选择创建springBoot项目
③:第三步 填写项目信息(可以自定义 Group、Artifact、Name、Package)
④:第四步选择常用的依赖Jar(根据个人需要选择或后期补充到pom.xml中,本文选择几个常用的做版本管理演示)
⑤:第五步建完之后项目结构如下初级版本
⑥:第六步修改项目结构作为其他所有子项目即modle的父工程,删掉src,只留下pom.xml
⑦:第七步创建子工程
⑧:第八步和第二步一样,后面流程参考②、③、④、⑤
⑨:第九步 最终项目结构(如果有多个子项目,过程参考⑦、⑧)
二:启动注册中心
①:父工程的pom.xml(有多余的jar包依赖,为后面的扩展铺垫)(springcloud-templates-v1的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lpf</groupId>
<artifactId>springcloud-templates-v1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-templates-v1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</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>
</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
②:子工程的pom.xml (springcloud-eureka-server的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>
<parent>
<groupId>com.lpf</groupId>
<artifactId>springcloud-templates-v1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lpf</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-eureka-server</name>
<description>Demo project for Spring Boot</description>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
③:配置文件application.yml
server:
#指定服务端口
port: 8671
spring:
application:
#服务名称
name: springcloud-eureka-server
eureka:
instance:
#指定主机名称
hostname: localhost
client:
#由于自己是服务器,不需要注册到自己
register-with-eureka: false
#由于自己是服务器,不需要从服务器获取注册信息
fetch-registry: false
#服务地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
④:启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
}
}
⑤:输入地址 localhost:8761 访问成功,代表注册中心启动成功
后续
后续会有eureka注册中心得使用和各级组件的使用