B站尚硅谷最新版SpringCloud 学习笔记(2)-Eureka服务注册与发现

B站尚硅谷最新版SpringCloud 学习笔记(2)-Eureka服务注册与发现

B站视频:https://www.bilibili.com/video/BV18E411x7eT

上一篇:B站尚硅谷最新版SpringCloud 学习笔记(1)

一、Eureka基础知识

1.服务治理

image-20210114163346610

2.服务注册

image-20210114163430060

image-20210114163601605

3.Eureka两组件

image-20210114163919589

二、单机Eureka构建步骤

1.IDEA生成eurekaServer端服务注册中心类似物业公司

①创建cloud-eureka-server7001

image-20210114164241251

②改POM

image-20210114164417194

<?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>mycloud2020</artifactId>
        <groupId>org.yezhinao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.yezhinao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <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>

③创建application.yml

server:
  port: 7001

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

④创建主启动

image-20210114165741596

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

⑤测试

输入http://localhost:7001/

image-20210114165813282

2.EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider

选中之前的cloud-provider-payment8001

①改写pom

image-20210114172024316

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

②改写application.yml

image-20210114172050549

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

③改写启动类

image-20210114172121397

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

④测试

启动cloud-eureka-server7001和cloud-provider-payment8001

image-20210114172500991

⑤自我保护机制

image-20210114172558509

3.EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer

选中之前的cloud-consumer-order80

①改写pom

image-20210114173020988

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

②改写yml

image-20210114173051684

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

③修改启动类

image-20210114173116044

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

④测试

先要启动EurekaServer,7001服务

再要启动服务提供者provider,8001服务

eureka服务器

image-20210114173204635

http://localhost/consumer/payment/get/31

image-20210114173313364

三、集群Eureka构建步骤

1.Eureka集群原理说明

image-20210114193458743

2.EurekaServer集群环境构建步骤

image-20210114193959382

①新建一个cloud-eureka-server7002

image-20210114210156117

②修改pom,添加application.yml,启动类

pom

<?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>mycloud2020</artifactId>
        <groupId>org.yezhinao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-eureka-server7002</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.yezhinao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <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>

application.yml

server:
  port: 7002

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

启动类

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

③测试

image-20210114194559789

④修改映射配置

找到C:\Windows\System32\drivers\etc路径下的hosts文件

image-20210114195147671

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com

测试,修改成功

image-20210114195255717

⑤修改cloud-eureka-server7001和cloud-eureka-server7002的pom文件

image-20210114195727880

同理eureka7002也是如此

cloud-eureka-server7001配置文件

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com    #eureka服务端的实例名字
  client:
    register-with-eureka: false #表示不像注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
    service-url:
      #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7002.com:7002/eureka/    #设置与eureka

cloud-eureka-server7002配置文件

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名字
  client:
    register-with-eureka: false #表示不像注册中心注册自己
    fetch-registry: false #false表示自己就是注册中心,我的职责就是维护服务实例,并不区检索服务
    service-url:
      #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka/     #设置与eureka

可见为了区别两台机器的hostname,所以修改了hosts文件

image-20210114195612577

3.cloud-provider-payment8001和cloud-consumer-order80发布到Eureka集群上

image-20210114204333017

image-20210114204351273

①修改pom

service-url:
  defaultZone: 	http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版

cloud-provider-payment8001

image-20210114203052966

cloud-consumer-order80

image-20210114203123720

②测试

先要启动EurekaServer,7001/7002服务

再要启动服务提供者provider,8001服务

再要启动消费者,80

http://eureka7001.com:7001/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gXg3M2Hl-1610679375190)(C:\Users\10618\AppData\Roaming\Typora\typora-user-images\image-20210114204244486.png)]

http://localhost/consumer/payment/get/1

image-20210114204250893

4.支付服务提供者8001集群环境构建

image-20210115092914590

新建cloud-provider-payment8002与cloud-provider-payment8001类似

①新建

image-20210114210104560

②修改pom,添加application.yml,添加启动类

pom

<?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>mycloud2020</artifactId>
        <groupId>org.yezhinao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8002</artifactId>

    <dependencies>
        <!-- 引入自己定义的api通用包 -->
        <dependency>
            <groupId>org.yezhinao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

application.yml

server:
  port: 8002


spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.yezhinao.springcloud.entities

启动类

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

③复制cloud-provider-payment8001的dao,service,controller,mapper

④修改8001/8002的Controller,修改两者

image-20210114212224685

@RestController
@Slf4j
@RequestMapping(value = "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

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


    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("****插入结果:" + result);
        if(result > 0){
            return  new CommonResult(200,"插入成功,serverPort"+serverPort,result);
        }
        else {
            return  new CommonResult(444,"插入失败");
        }
    }


    @GetMapping(value = "/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果:"+payment);
        if (payment!=null){  //说明有数据,能查询成功
            return new CommonResult(200,"查询成功,serverPort"+serverPort,payment);
        }else {
            return new CommonResult(444,"没有对应记录,查询ID:"+id,null);
        }
    }
}

⑤测试

image-20210114212759845

http://localhost:8001/payment/get/1测试访问8001能够访问到,显示自己的端口

image-20210114212243384

http://localhost:8002/payment/get/1测试访问8002能够访问到,显示自己的端口

image-20210114212340190

使用cloud-consumer-order80访问,只能访问8001端口,为什么

image-20210114212531276

原因cloud-consumer-order80的控制层地址写死了

image-20210114212636402

⑥负载均衡

解决上述问题:改造url,换成注册名

image-20210114212943875

image-20210114213032850

不能识别到CLOUD-PAYMENT-SERVICE

image-20210114213222528

使用@LoadBalanced注解赋予RestTemplate负载均衡的能力(这其实是Ribbon的功能)

image-20210114213354342

实现负载均衡

Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号

image-20210114213418156 image-20210114213439463

四、actuator微服务信息完善

两个配置,下面详解

eureka:
    instance:
      instance-id: payment8001 #主机名称:服务名称修改
      prefer-ip-address: true #访问信息有ip信息提示

问题:主机名无法辨认和没有显示ip地址

image-20210115091510508

1.主机名称:服务名称修改

image-20210115091922571

instance:
  instance-id: payment8001

将其他客户机修改

效果如下:

image-20210115092319453

2.访问信息有ip信息提示

image-20210115092651093

prefer-ip-address: true

可以看到有IP显示

image-20210115092618283

五、服务发现Discovery

一个注解,下面详解

@EnableDiscoveryClient

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

现只操作cloud-provider-payment8001。

1.修改cloud-provider-payment8001的Controller

image-20210115095055315

@GetMapping("/discovery")
public Object discovery() {
    List<String> services = discoveryClient.getServices();
    for (String service : services) {
        log.info("***service:" + service);
    }

    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    for (ServiceInstance instance : instances) {
        log.info("****instance:" + instance);

        log.info("**" + instance.getServiceId() + "\t"
                + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
    }
    return this.discoveryClient;
}

2.修改8001主启动类

@EnableDiscoveryClient

image-20210115095211987

3.测试

访问http://localhost:8001/payment/discovery

image-20210115095637688

六、Eureka自我保护

1.故障现象

image-20210115101724731

2.导致原因

某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存

image-20210115101818257

image-20210115101827965

3.禁止自我保护(一般生产环境中不会禁止自我保护)

1.配置注册中心EureakeServer端

image-20210115104250719

server:
  #    自我保护模式,当出现出现网络分区、eureka在短时间内丢失过多客户端时,会进入自我保护模式,即一个服务长时间没有发送心跳,eureka  也不会将其删除,默认为true
  enable-self-preservation: false
  #    eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
  eviction-interval-timer-in-ms: 2000

2.配置生产者客户端EureakeClient端

image-20210115104410972

#参数用于定义服务续约任务的调用间隔时间,默认30秒。
lease-expiration-duration-in-seconds: 1
#用于定义服务失效的时间,默认90秒。
lease-renewal-interval-in-seconds: 2

3.效果如下

自我保护模式已关闭。如果出现网络/其他问题,这可能无法保护实例过期。

image-20210115103745812

停止Payment8001,服务端立刻剔除

image-20210115104650139

最后一句,Eureka已经停更,但学习其知识还是很有帮助,为后面的注册中心做铺垫

代码:https://gitee.com/yezhinao/SpringCloud-Learn/tree/master/mycloud2020(2)

下一篇:B站尚硅谷最新版SpringCloud 学习笔记(3)-Zookeeper服务注册与发现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值