分布式-Eurka
Eureka是一种基于REST(具像状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移
本文将演示服务注册与发现(完整项目地址:点击跳转)
eureka-server
依赖:
<!-- 引入spring-cloud依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
</dependency>
<!-- eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
配置文件
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${spring.cloud.client.ip-address}:${server.port}/eureka/
#关闭自我保护
eureka.server.enable-self-preservation=false
#清理间隔
eureka.server.eviction-interval-timer-in-ms=10000
启动项加入@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
eureka-client
依赖:
<!-- 引入spring-cloud依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
配置文件:
#eureka注册地址
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
#开启健康检查
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(单位秒,默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=10
# 续约到期时间(单位秒,默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=10
#注册在eurka信息
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=http://${spring.cloud.client.ip-address}:${server.port}
eureka.instance.hostname= ${spring.cloud.client.ip-address}
启动项加入@@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
@RestController
public class MyspringbootApplication{
public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
}
/**
* 访问首页
*/
@GetMapping("/index")
public String index(){
return "hello springboot!";
}
}
启动项目:
题外话:idea 可以alt+8展示使用过的service,便于开启服务