目录
第二步 添加启动注解 @EnableEurekaServer
搭建Eureka Server
第一步添加pom
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-cloud-example</artifactId>
<groupId>dolf.zhang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-eureka-server</artifactId>
<name>spring-cloud-eureka-server</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--spring cloud 版本-->
<spring-cloud.version>Greenwich.M1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<!--导入spring cloud pom-->
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
第二步 添加启动注解 @EnableEurekaServer
package dolf.zhang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
第三步 添加配置文件
server.port=8880
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
配置Eureka Client
第一步 添加pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
第二步 添加启动注解 @EnableEureka
第三步 添加配置文件
spring.application.name=user-services
server.port=8080
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8880/eureka/
单机模式启动
# 注册中心 服务端
java -jar target/spring-cloud-eureka-server-1.0-SNAPSHOT.jar
访问 http://localhost:8880/ 此时可以看到页面中会显示出没有可用实例
# 注册中心 客户端
java -jar target/spring-cloud-eureka-client-1.0-SNAPSHOT.jar
访问 http://localhost:8880/ 此时可以看到页面中会显示出实例名称 服务状态 以及ip:port
集群模式
# 注册中心 服务端
java -jar target/spring-cloud-eureka-server-1.0-SNAPSHOT.jar --spring.config.location=classpath:/application-register1.properties
java -jar target/spring-cloud-eureka-server-1.0-SNAPSHOT.jar --spring.config.location=classpath:/application-register2.properties
java -jar target/spring-cloud-eureka-server-1.0-SNAPSHOT.jar --spring.config.location=classpath:/application-register3.properties
在执行集群命令时前面两个会报错,是由于其它的集群节点未启动。等待后面集群节点启动完毕后会自动加入
# 注册中心 客户端
java -jar target/spring-cloud-eureka-client-1.0-SNAPSHOT.jar --spring.config.location=classpath:/application-cluster.properties
在集群模式中你也可以只配置一个地址,个人建议地址都写上。
代码传送门 https://github.com/zhanghongfu/spring-cloud-example.git