服务注册
<!-- 引入SpringCloud Eureka server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- client端,开启健康检查(需要spring-boot-starter-actuator依赖)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 约束整个项目的SpringCloud版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server:
port: 8012
eureka:
instance:
hostname: localhost
# 设置租期更新时间(心跳),默认30秒
lease-renewal-interval-in-seconds: 10
# 设置租期到期时间(心跳无应答时间),默认90秒
lease-expiration-duration-in-seconds: 15
client:
# 在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
# eureka.client. register-with-eureka:由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
registerWithEureka: false
# eureka.client . fetch-registry:由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false。 不主动发现别人
fetchRegistry: false
# 声明注册中心的地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# client端,开启健康检查
healthcheck:
enabled: true
server:
# 关闭注册中心得自我保护机制
# enable-self-preservation: false
# 注册中心清理间隔,单位为毫秒
eviction-interval-timer-in-ms: 1000
# 给当前应用起个服务名称 在微服务中代表当前服务
spring:
application:
name: eureka-server
/**
* @EnableEurekaServer 声明当前项目为Eureka服务端
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
服务发现
<!-- 引入SpringCloud Eureka server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 约束整个项目的SpringCloud版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
server:
port: 8013
# 指定当前服务名称 这个名称会注册到注册中心
spring:
application:
name: eureka-client
# 指定 服务注册中心的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8012/eureka
@SpringBootApplication
/**
* 声明当前项目为Eureka客户端
* 只能注册到Eureka Servser注册中心
*/
// @EnableEurekaClient
/**
* 声明当前客户端为可被发现
* 不仅可以注册到Eureka Server 也可以注册到其他注册中心
*/
@EnableDiscoveryClient
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
}