- 创建maven父项目,创建maven子项目,添加springcloud的eurea依赖,配置和启动注解。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR7</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加server配置:
spring:
application:
name: server
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:7001/eureka/
添加user或product模块配置:
spring:
application:
name: product
server:
port: 9002
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:7001/eureka/
添加启动注解:
server子项目需要在启动类添加:@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class LearnServerApplication {
public static void main(String[] args) {
SpringApplication.run(LearnServerApplication.class, args);
}
}
具体业务子项目(user或product)启动类不需要添加注解(旧版本SpringCloud需要在启动类添加@EnableDiscoveryClient注解)。
@SpringBootApplication
//@EnableDiscoveryClient(旧版本springcloud需要添加此注解)
public class LearnProductApplication {
public static void main(String[] args) {
SpringApplication.run(LearnProductApplication.class, args);
}
}
- 启动测试,访问localhost:7001,成功
- 打jar包,添加插件:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
- 确定包名:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>learn-product</finalName>
</build>
- maven----lifecycle–clean;package;如果报错,检查springcloud版本与springboot版本是否匹配。具体匹配实例见项目:https://gitee.com/manongfaner/cloud-maven-learn
- 如果匹配,可以打出jar包,然后运行java -jar learn-server.jar 即可运行项目。