SpringCloud——eureka服务注册及发现

SpringCloud——Eureka 服务注册与发现
他有三端构成
1.Eureka Server 服务注册与发现中心端 提供服务注册和发现的能力(通常就是微服务中的注册中心)
2.Service Provider服务提供者端 一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)
3.Service Consumer服务调用者端 一个Java客户端,用于简化与 Eureka Server 的交互(通常就是微服务中的客户端和服务端)

在这里插入图片描述
各个微服务启动时,会通过 Eureka Client 向 Eureka Server 注册自己,Eureka Server 会存储该服务的信息,也就是说,每个微服务的客户端和服务端,都会注册到 Eureka Server,这就衍生出了微服务相互识别的话题。
同步:每个 Eureka Server 同时也是 Eureka Client(逻辑上的)多个 Eureka Server 之间通过复制的方式完成服务注册表的同步,形成 Eureka 的高可用
识别:Eureka Client 会缓存 Eureka Server 中的信息,即使所有 Eureka Server 节点都宕掉,服务消费者仍可使用缓存中的信息找到服务提供者(笔者已亲测)
续约:微服务会周期性(默认30s)地向 Eureka Server 发送心跳以Renew(续约)信息(类似于heartbeat)
续期:Eureka Server 会定期(默认60s)执行一次失效服务检测功能它会检查超过一定时间(默认90s)没有Renew的微服务,发现则会注销该微服务节点Spring Cloud 已经把 Eureka 集成在其子项目 SpringCloud Netflix 里面

**

服务方(一)

pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml

server:
  port: 56799
spring:
  application:
    name: jwxt-eureka01   #当前应用的名字(对应注册后的应用名,不设置会默认为UNKNOWN)

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/kytms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    #url: jdbc:mysql://192.168.5.240:3306/kytms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root


eureka:
  instance:
    hostname: server1     #eureka地址
    prefer-ip-address: true #是否显示ip
    instance-id: my_eureka56799     #注册后的实例id
  client:
    fetch-registry: false #是否拉取服务表
    register-with-eureka: false #是否注册进入eureka(当前的eureka不注册进入注册中心)
    service-url:          #服务的路径
      defaultZone: http://localhost:56798/eureka/   #默认注册地址,当使用security时要加入对应的用户名和密码http://g01:123456@server2:8762/eureka/    其他端使用方法一致
  server:
    enable-self-preservation: false   #Eureka的自我保护机制关闭,红字提醒(生产环境不推荐关闭)
    eviction-interval-timer-in-ms: 60000 # 默认为60 * 1000ms (一分钟)

**
**

defaultZone

**
Eureka 有一个 Region 和 Zone 的概念,你可以理解为现实中的大区(Region)和机房(Zone)
Eureka Client 在启动时需要指定 Zone,它会优先请求自己 Zone 的 Eureka Server 获取注册列表
同样的,Eureka Server 在启动时也需要指定 Zone,如果没有指定的话,其会默认使用 defaultZone
eureka.client.serviceUrl.defaultZone这个配置可以配置单个注册中心的地址,也可配置多个,逗号隔开
**

instance-id

**
是在注册中心页面显示的微服务名。
Eureka 首页显示的微服务名默认为:机器主机名:应用名称:应用端口,也就是:
s p r i n g . c l o u d . c l i e n t . h o s t n a m e : {spring.cloud.client.hostname}: spring.cloud.client.hostname:{spring.application.name}:KaTeX parse error: Expected '}', got 'EOF' at end of input: …on.instance_id:{server.port}}
**

启动类

@EnableEurekaServer
@SpringBootApplication
public class Eureka01Application {

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

**

服务方(二)

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml

server:
  port: 56798
spring:
  application:
    name: jwxt-eureka02   #当前应用的名字(对应注册后的应用名,不设置会默认为UNKNOWN)

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/kytms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    #url: jdbc:mysql://192.168.5.240:3306/kytms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root


eureka:
  instance:
    hostname: server2     #eureka地址
    prefer-ip-address: true #设置微服务调用地址为IP优先(默认为false)
    instance-id: my_eureka56798     #注册后的实例id
    lease-renewal-interval-in-seconds: 30 #心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 90 #发呆时间,即服务续约到期时间(缺省为90s)
  client:
    fetch-registry: true #是否拉取服务表
    register-with-eureka: true  #是否注册进入eureka(当前的eureka不注册进入注册中心)
    service-url:          #服务的路径
      defaultZone: http://localhost:56799/eureka/   #默认注册地址,当使用security时要加入对应的用户名和密码http://g01:123456@server2:8762/eureka/    其他端使用方法一致
  server:
    enable-self-preservation: false   #Eureka的自我保护机制关闭,红字提醒(生产环境不推荐关闭)

启动类

@SpringBootApplication
@EnableEurekaServer
public class Eureka02Application {

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

**

消费方

pom.xml

<dependencies>
    <dependency>
        <groupId>com.jwxt</groupId>
        <artifactId>jwxt-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

application.yml

#首页显示的微服务名默认为:机器主机名:应用名称:应用端口,
#也就是:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
eureka:
  client:
    service-url:
      defaultZone: http://localhost:56798/eureka/,http://localhost:56799/eureka/
  instance:
    prefer-ip-address: true
    instance-id: system8083 #在注册中心页面显示的微服务名

启动类

@SpringBootApplication
@EnableEurekaClient
public class TeacherApplication {

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

    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
}

**
**

**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值