注册中心Eureka
eureka的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
eureka的配置文件
spring:
application:
name: eureka-server
server:
port: 8001
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
fetch-registry: false #禁止当前项目写入eureka服务中
register-with-eureka: false
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 4000 #剔除时间
register-with-eureka: false false表示不向注册中心注册自己
fetch-registry: false false表示自己就是注册中心,不需要从注册中心获取注册列表信息
service-url 设置eureka server交互的地址查询服务和注册服务都需要用到这个地址(单机用)
主启动类
@SpringBootApplication
@EnableEurekaServer // 声明当前springboot应用是一个eureka服务中心
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class);
}
}
搭建生产者Provider
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 在provider的pom文件中添加监控依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
server:
port: 9070
spring:
application:
name: user-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port} #显示注册的ip和端口
lease-renewal-interval-in-seconds: 5 #心跳时间
lease-expiration-duration-in-seconds: 20 #续约时间
order-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
management: #hystrix的数据流文件,存储监控数据
endpoints:
web:
exposure:
include: hystrix.stream
主启动类
@SpringBootApplication
@EnableEurekaClient // 或 @EnableDiscoveryClient
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class);
}
}
@EnableEurekaClient和@EnableDiscoveryClient区别 在使用Spring Cloud feign使用中在使用服务发现的时候提到了两种注解: 一种为@EnableDiscoveryClient; 一种为@EnableEurekaClient,用法上基本一致