建立maven工程
工程结构如下:
pom文件如下:
<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>eureka</groupId> <artifactId>eureka</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>eureka</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> </parent> <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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Dalston.SR1</version> <!--Brixton.SR3官网为Angel.SR4版本,但是我使用的时候总是报错 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>eureka</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.chiwei.eureka.Bootstrap</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> <excludes> <exclude>config/**</exclude> <exclude>templates/**</exclude> <exclude>static/**</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <configuration> <excludes> </excludes> </configuration> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-source-plugin</artifactId> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-tgz</id> <!-- 绑定到package生命周期阶段上 --> <phase>package</phase> <goals> <!-- 绑定到package生命周期阶段上 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路径 --> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> </project>
其中注意spring-boot-starter-parent和spring-boot-starter-parent的版本号要对应起来,否则运行可能报错,楼主踩过坑
配置文件中重要配置项如下:
#是否向服务注册中心注册自己 eureka.client.register-with-eureka=false #是否检索服务 eureka.client.fetch-registry=false #服务注册中心实例的主机名 eureka.instance.hostname=myinstance logging.level.com.netflix.eureka=OFF logging.level.com.netflix.discovery=OFF #服务注册中心端口号 server.port=23456启动代码如下:
package com.chiwei.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Type Bootstrap.java * @Desc * @author chiwei * @date 2017年11月9日 下午5:24:53 * @version */ /** * @author chiwei * */ @EnableEurekaServer @SpringBootApplication public class Bootstrap { /** * 主函数入口 * @param args */ public static void main(String[] args) { SpringApplication app = new SpringApplication(Bootstrap.class); // 不启动web服务 // app.setWebEnvironment(false); app.run(args); } } /** * Revision history * ------------------------------------------------------------------------- * * Date Author Note * ------------------------------------------------------------------------- * 2017年11月9日 chiwei create */
至此,eureka的服务注册中服务端启动成功了。