注册中心Eureka

注册中心Eureka

eureka的依赖 
 
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
​
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

引入依赖

在这里插入图片描述

  
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
​

        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

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,用法上基本一致

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/43f848f7f3.html