第四节:cloud-eureka-server7001

1.Eureka服务注册与发现:
服务治理,实现服务调用、负载均衡、容错等,实现服务注册与发现。传统rpc远程调框架,管理服务与服务之间依赖关系和管理比较复杂。
组件:
Eureka Server:提供服务注册服务
Eureka Client:通过注册中心进行访问

4.10

单机Eureka构建步骤:

2.IDEA生成EurekaServer端服务注册中心,类似于物业公司。

(1)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.jiao.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7001</artifactId>

    <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引用自己定义的api通用包,可以使用Payment支付entity -->
        <dependency>
            <groupId>com.jiao.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- boot web actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 一般通用配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

(2)application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    #false代表不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例额,并不需要去检查服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

(3)EurekaMain.java

package com.jiao.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author jyl
 * @create 2021-01-23
 */

@EnableEurekaServer     //Eureka服务注册中心
@SpringBootApplication
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

(4)运行7001端口测试

4.1

3.EurekaClient端cloud-provider-payment8001,将注册进EurekaServer成为服务提供者provider,类似于学校对外提供的的授课服务。

(1)修改8001端口的pom.xml,加入EurekaClient依赖

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

4.2

(2)修改8001端口的application.yml,注册EurekaClient

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

4.3

(3)修改8001端口的PaymentMain8001.java,添加注解@EnableEurekaCLient

4.4

(4)运行测试【7001端口和8001端口】

4.5

4.EurekaClient端cloud-consumer-order80,将注册进EurekaServer成为服务消费者consumer,类似于在学校上课消费的各位同学

(1)修改80端口的pom.xml中,EurekaClient依赖

4.6

(2)修改application.yml中的springApplicationName和EurekaClient

4.7

(3)修改OrderMain80.java添加注解@EnableEurekaClient

4.8

(4)测试【7001、8001、80端口都开启】

4.9

集群Eureka构建步骤:

Eureka集群实现负载均衡+故障容错【互相注册,相互守望】

4.11

(1)pom.xml的依赖与7001相同。

(2)修改添加计算机的映射配置文件host。

4.12

(3)修改application.yml文件,7001和7002的都要改。

4.13

A.7001端口的application.yml:

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com

  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/

B.7002端口的application.yml:

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com

  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

(4)主启动类。EurekaMain7002.java

package com.jiao.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer     //Eureka服务注册中心
@SpringBootApplication
public class EurekaMain7002 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7002.class,args);
    }
}

(5)测试:运行7001和7002端口。

4.14

将支付服务8001微服务发布到上面两台Eureka集群配置中。

defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

将订单服务80微服务发布到上面两台Eureka集群配置中。

测试:启动步骤-7001/7002---》8001---》80

4.15

构建8002模块,与8001服务提供者一样。

4.18

pom.xml,application.yml(注意改端口为8002),com.jiao.springcloud下的文件

修改添加8001/8002下的Controller:

@Value("${server.port}")
private String serverPort;

//修改
return new CommonResult(200,"插入数据库成功,serverPort:"+serverPort,result);

return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);

负载均衡BUG:订单服务访问地址80不能写死。(修改80端口的Controller,在config.ApplicationContextCofig.java添加注解@LoadBalanced)

//public static final String PAYMENT_URL = "http://localhost:8001";
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

4.16

测试:7001/7002-》8001/8002-》80

http://eureka7001.com:7001/			
http://eureka7002.com:7002/

http://localhost:8001/payment/get/3
http://localhost:8002/payment/get/3

http://localhost/consumer/payment/get/5

4.17

actuator微服务信息完善。

(1)主机名称 :服务名称修改

修改cloud-provider-payment8001和8002的application.yml:

eureka:下的
    instance:
        instance-id: payment8001 或payment8002

4.19

4.20

(2)访问信息有IP信息提示

eureka:
    instence:下的
        prefer-ip-address: true #访问路径可以显示IP地址

4.21

 

服务发现Discovery

功能:对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息

修改cloud-provider-payment8001下的Controller:【加注解】

@Resource
    private DiscoveryClient discoveryClient;
...
@GetMapping(value = "/payment/discovery")
public Object discovery(){
    List<String> services = discoveryClient.getServices();
    for (String element : services) {
        log.info("*****Element:"+element);
    }
    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    for (ServiceInstance instance : instances) {               
 log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
    }
    return this.discoveryClient;
}

在主启动类下加注解@EnableDiscoveryClient:

4.22

自测:

4.23

4.24

Eureka自我保护:

故障现象

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server 将会尝试保护其服务注册表中的数据,也就是不会注销任何微服务。

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

导致原因

某一时刻某一个微服务不可用了,Eureka不会立即清理,依旧会对该微服务的信息进行保存。属于CAP里面的AP分支。

怎么禁止自我保护

(1)注册中心EurekaServer端7001/7002:

application.yml:

eureka:
    server:
        #关闭自我保护机制,保证不可用服务被及时剔除
        enable-self-preservation: false
        eviction-interval-timer-in-ms: 2000
THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

(2)生产者客户端EurekaClient端8001/8002:

application.yml:

eureka:
    instence:下的    
        #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认30秒)
        lease-renewal-interval-in-seconds: 1
        #Ereuka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
        lease-expiration-duration-in-seconds: 2

Eureka停更了。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值