開發環境使用IDEAjava
新建Eureka Server,新建maven項目,配置pom.xmlgit
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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>
啓動類修改github
@EnableEurekaServer @SpringBootApplication public class EurekaserviceApplication { public static void main(String[] args) { SpringApplication.run(EurekaserviceApplication.class, args); } }
添加配置文件spring
server.port=8761 eureka.instance.hostname=0.0.0.0 spring.application.name = eureka_server eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/
啓動tomcat
打jar包,放到服務器上執行 後臺掛起服務服務器
nohup java -jar eurekaservice-0.0.1-SNAPSHOT.jar &app
eureka客戶端註冊服務maven
新建一個項目,修改配置ide
server.port =1001 spring.application.name=service-hi #註冊eureka服務 eureka.client.serviceUrl.defaultZone=http://地址/eureka/
啓動類中添加spring-boot
@EnableEurekaClient
@EnableEurekaClient @SpringBootApplication public class EurekaclientApplication { public static void main(String[] args) { SpringApplication.run(EurekaclientApplication.class, args); } }
訪問eureka服務能夠看到service-hi 已經註冊