第一步:创建父项目,引入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.idea</groupId>
<artifactId>springcloud_myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springclould_regist</module>
<module>springcloud_provider</module>
<module>springcloud_consumer</module>
</modules>
<packaging>pom</packaging>
<!--继承springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<name>springcloud_myproject Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<!--指定spring cloud 的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</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>
第二步:创建eureka注册中心,注册服务,注册中心的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>springcloud_myproject</artifactId>
<groupId>com.idea</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springclould_regist</artifactId>
<packaging>war</packaging>
<name>springclould_regist Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
第三步:创建Eureka入口类
@EnableEurekaServer//开启eurekaServer
@SpringBootApplication
public class RegistApplication {
public static void main(String[] args) {
SpringApplication.run(RegistApplication.class,args);
}
}
第四步:创建yml配置文件
application.yml
spring:
application:
name: springcloud_eureka #应用名
profiles:
active: dev #指定启动项目对应的分支
server:
port: 8090 #指定服务启动的端口
application-dev.yml
spring:
application:
name: register-cloud
management:
server:
servlet:
context-path: / #配置的根路径
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false #注册中心不需要注册自己
fetch-registry: false #注册中心不需要去发现服务
server:
enable-self-preservation: false #自我保护机制,关闭
配置完之后启动入口类,访问http://localhost:8761/,出现以下页面