一步一步创建SpringCloud项目(一)——创建项目父工程、Eureka
1、File—>New—Maven Project,选择Create a simple project,点击next
2、输入Group Id,Artifact Id,Packaging选择pom,点击Finish
3、父工程创建完成后,修改父pom文件,加入SpringCloud依赖
<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.landcode</groupId> <artifactId>land</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> <spring-boot.version>2.1.6.RELEASE</spring-boot.version> <gson.version>2.6</gson.version> </properties> <!-- spring-cloud所有项目依赖包 --> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 必须引入gson 2.6版的替换2.2版的,不然会报错不兼容 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
4、开始创建子项目eureka,右键工程 New—>Maven Module,选择Create a simple project,输入Module Name,点击Next
5、输入Group Id,Packaging选择jar,点击Finish完成子项目创建
6、eureka子项目创建完成后,修改pom文件,加入springboot依赖,加入springboot-maven插件,修改成打可执行jar包
<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> <parent> <groupId>com.landcode</groupId> <artifactId>land</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>land-eureka</artifactId> <!-- 添加注册中心依赖的包文件 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <finalName>land-eureka</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <!-- 打包成可执行jar包 --> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
7、在eureka项目的src/main/java/包根目录下创建启动类如下
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
8、在eureka项目的src/main/resources根目录下创建application.yml配置文件
#本机端口
server:
port: 8761
eureka:
client:
#是否向注册中心注册自己
register-with-eureka: false
#是否从注册中心查询服务
fetch-registry: false
#注册中心地址
service-url:
defaultZone: http://localhost:8761/eureka/
9、eclipse创建springcloud项目并集成eureka完成
浏览器打开 http://192.168.0.3:8761/ 访问eureka管理页面