一、新建一个maven项目
二、在pom.xml 中加入如下配置
<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.purchase</groupId>
<artifactId>bt-purchase-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bt-purchase-service</name>
<description>bt-purchase-service</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
三、在src/main/resources 下新建 application.yml 加入如下配置
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9999/eureka/
spring:
application:
name: service-purchase
注: defaultZone: http://localhost:9999/eureka/ 为注册中心地址 name: service-purchase 为该服务在注册中心的名称
四、编写启动类
package com.purchase.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PurchaseApp {
public static void main(String[] args) {
SpringApplication.run(PurchaseApp.class, args);
}
}
注:@EnableEurekaClient 为必须加入的注解
启动后,如下图
五、打开注册中心界面,就会发现服务已经注册进来