且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-10-15 02:11:24

是什么

干嘛用

怎么用

加依赖

<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);
    }
}