美文网首页SpringCloud
【SpringCloud】Eureka使用

【SpringCloud】Eureka使用

作者: 扮鬼之梦 | 来源:发表于2019-08-08 18:01 被阅读0次

一、注册中心的搭建

1.创建EurekaServer项目

2.启动类添加开启EurekaServer的注解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoEurekaApplication.class, args);
    }
}

3.添加yml配置文件

server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka #localhost一般为服务器ip
    register-with-eureka: false #对于注册中心的优化,不注册本服务
    fetch-registry: false

4.启动项目并访问

访问地址:http://localhost:8761/

二、客户端的搭建

1.创建EurekaClient项目

2.添加yml配置文件

server:
  port: 8080
spring:
  application:
    name: demo-member #项目名
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka #注册中心地址
    register-with-eureka: true #注册该服务,默认为true
    fetch-registry: true #获取服务列表,默认为true

3.启动项目,在Eureka管理页里就可以看到注册的服务

三、Eureka的高可用

1.原理图

2.只用修改配置文件即可,启动三个Eureka-Server在不同端口上,服务同时注册到这三个Eureka-Server上。

# erurek的注册中心地址
server:
  port: 876[1:3]  #注册中心的地址
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url: 
      defaultZone: [http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka](http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka)
    register-with-eureka: true # 把自己注册到别的eureka
    fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表

相关文章

网友评论

    本文标题:【SpringCloud】Eureka使用

    本文链接:https://www.haomeiwen.com/subject/yczxjctx.html