1、创建Eureka-Server服务端
引入项目依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</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>
</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>
启动类上开启注册中心注解:将项目作为SpringCloud中的注册中心
@EnableEurekaServer
public class EurekaServerAppRun {
public static void main(String[] args) {
SpringApplication.run(EurekaServerAppRun.class, args);
}
}
配置文件:
spring:
application:
name: eureka-server #指定服务名称
server:
port: 7001 #指定运行端口
eureka:
instance:
hostname: localhost #指定主机地址
client:
fetch-registry: false #表示是否将自己注册到Eureka Server -注册中心不需要开启
register-with-eureka: false #表示是否从Eureka Server获取注册的服务信息 -注册中心不需要开启
server:
enable-self-preservation: false #关闭保护模式
启动服务:进入Eureka注册中心的界面
http://localhost:7001/
2、创建Eureka-Client端
引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</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>
启动类注解:@EnableDiscoveryClient 表示这是一个Eureka客户端,另外从Spring Cloud Edgware开始@EnableDiscoveryClient 或@EnableEurekaClient 可省略,只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientAppRun {
public static void main(String[] args) {
SpringApplication.run(EurekaClientAppRun.class, args);
}
}
配置文件:客户端需要向注册中心进行注册,并且需要配置 注册中心的 地址
spring:
application:
name: eureka-client #服务名称
server:
port: 7002 #运行端口号
eureka:
client:
register-with-eureka: true #表示是否将自己注册到Eureka Server
fetch-registry: true #表示是否从Eureka Server获取注册的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/ #配置注册中心地址
启动服务:此时客户端服务已经被注册中心集成进去