spring cloud放弃系列之--2-eureka

是什么

干嘛用

怎么用

加依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

Application 入口类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

yml配置文件

server:
  port: 7004
eureka:
  instance:
    hostname: localhost
  client:
     serviceUrl:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

访问浏览器,发现ok了, 这里没有配置忽略自己成为服务,启动会报错,

配置高可用

host文件

127.0.0.1 x1
127.0.0.1 x2
127.0.0.1 x3

配置中心

partition:
   host1:
      name: x1
      port: 7003
   host2:
      name: x2
      port: 7004
   host3:
      name: x3
      port: 7005
   host4:
      name: localhost
      port: 7006 #

eureka配置文件 x3(eureka记得配置文件的名字改为bootstrap.yml)

server:
  port: ${partition.host3.port}
eureka:
  instance:
    hostname: ${partition.host3.name}
  client:
     serviceUrl:
        defaultZone: http://${partition.host1.name}:${partition.host1.port}/eureka/,http://${partition.host2.name}:${partition.host2.port}/eureka/

spring:
  cloud:
    config:
      allow-override: false
      #label: master
      profile: dev
      uri: http://10.10.8.101:7000
  application:
    name: eureka-service3

eureka配置文件 x2(eureka记得配置文件的名字改为bootstrap.yml)

server:
  port: ${partition.host2.port}
eureka:
  instance:
    hostname: ${partition.host2.name}
  client:
     serviceUrl:
        defaultZone: http://${partition.host1.name}:${partition.host1.port}/eureka/,http://${partition.host3.name}:${partition.host3.port}/eureka/

spring:
  cloud:
    config:
      allow-override: false
      #label: master
      profile: dev
      uri: http://10.10.8.101:7000
  application:
    name: eureka-service2

eureka配置文件 x1(eureka记得配置文件的名字改为bootstrap.yml)

server:
  port: ${partition.host1.port}
eureka:
  instance:
    hostname: ${partition.host1.name}
  client:
     serviceUrl:
        defaultZone: http://${partition.host2.name}:${partition.host2.port}/eureka/,http://${partition.host3.name}:${partition.host3.port}/eureka/

spring:
  cloud:
    config:
      allow-override: false
      #label: master
      profile: dev
      uri: http://10.10.8.101:7000
  application:
    name: eureka-service1

三台eureka 实现俩俩互联。

客户端的使用

引入jar

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

配置文件

eureka:
  client:
    service-url:
      defaultZone: http://x1:7003/eureka/,http://x2:7004/eureka/,http://x3:7005/eureka/

Application 入口类加入 @EnableEurekaClient


@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulServiceApplication.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值