接下来我们进入今天的正题
1、创建maven父工程
File—>new—>other—>Maven—>Maven project
点击“next”
2、创建子项目
打开项目中的pom.xml文件选择overview
第一种创建方式
点击create直接在父工程中创建
使用同样的方法创建eurekaclient。创建完成的项目结构如图
3、父工程pom.xml配置
在spring cloud官方文档上面有一段maven父工程pom.xmlde示例配置,可以直接引用过来。
我们可以看得到,spring-boot-starter-parent的版本是1.3.5.RELEASE,版本有些低我们将它替换到2.0.2.RELEASE的版本,同时需要替换spring-cloud-dependencies的版本,替换配置如下:
官方文档中缺少很多引用,需要根据项目进行添加,但首先我们需要先添加spring-boot-starter-web jar包。因为刚开始我没有引用这个jar包,导致客户端无法注册到注册中心中。
4、eurekaserver Pom.xml配置
需要在eurekaserver的pom.xml文件中配置:eurekaserver的jar包。
注意:项目应该是jar,如果你的
package com.test.eurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
}
}
这个需要我们在src/main/resources中创建application.yml
点击new—>File
将代码复制进去
server:
port: 8761 #端口号
eureka:
instance:
hostname: localhost #主机ip
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl: #注册中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在启动类中运行程序
右键点击启动类—>Run as —>java application
在浏览器中输入http://localhost:8761/进入注册中心
4、eurekaclient 配置
和服务器的配置一样,根据官方文档操作即可。直接看配置
pom.xml
启动类
package com.test.eurekaClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
application.yml
server:
port: 8762 #端口号
eureka:
client:
serviceUrl: #注册中心地址
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: bwx-common #在注册中心显示的名字
客户端完成