美文网首页
跟着大宇学SpringCloud

跟着大宇学SpringCloud

作者: 小大宇 | 来源:发表于2021-07-21 17:47 被阅读0次

    第一节:服务注册与服务发现

    服务中心,配置文件中的两个false说明自己是【注册中心】

    server:
      port: 8761
     
    eureka:
      instance:
        hostname: localhost
      client:
    #下面两个false说明自己是一个 Eureka Server
        registerWithEureka: false
        fetchRegistry: false
        #服务中心的地址
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
     
    #此服务器名称
    spring:
      application:
        name: eurka-server
    

    【服务提供者 Eureka - Client】
    主要配置服务中心地址

    #当前项目部署的端口
    server:
      port: 8762
     
    #配置注册中心的位置,并把自己注册进去
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
     
    #当前项目名字
    spring:
      application:
        name: service-client
    
    image.png

    第二节:使用Ribbon作为服务消费者

    Ribbon是一个基于Http和TCP的负载均衡工具。
    发送给8764端口的请求,分别交替发给部署在8762与8763的服务器。


    image.png

    第三节:使用Feign作为服务消费者

    Ribbon是一个基于Http和TCP的负载均衡工具,Feign(音:菲恩)是一个声明式的伪Http客户端,它比Ribbon更加的优雅。Feign使用的是接口的方式。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

    feign服务器与ribbon服务器其实是差不多的,都是负载均衡工具。本例中向feign服务器在发送/sayHello请求的时候,feigin服务器首先处理请求,它用HelloService来获取数据。HelloService类又规定了将此请求转发给SERVICE-CLIENT服务器的/hi接口处理。

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
     
    @FeignClient(value = "SERVICE-CLIENT")
    public interface HelloService {
     
        /**
         * 从SERVICE-CLIENT服务器的/hi接口获取JSON数据
         *
         * @param name
         * @return
         */
        @RequestMapping("/hi")
        String obtainOtherServerJsonData(@RequestParam(value = "name") String name);
    }
    

    第四节:使用Hystrix熔断器

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC)。如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

    熔断方法可以直接返回一个固定值。

    在启动类上增加@EnableHystrix注解,以开启熔断功能。

    feign集成了ribbon,所以自己就能够熔断,不需要导入其它依赖。需要在配置文件中加上开启熔断功能的配置
    在接口上的@FeignClient注解里,增加fallback = ErrorHandler.class配置。

    package com.safesoft.feign.service;
     
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
     
     
    @FeignClient(value = "SERVICE-CLIENT",fallback = ErrorHandler.class)
    public interface HelloService {
     
        /**
         * 从SERVICE-CLIENT服务器的/hi接口获取JSON数据
         *
         * @param name
         * @return
         */
        @RequestMapping("/hi")
        String obtainOtherServerJsonData(@RequestParam(value = "name") String name);
    }
    
    package com.safesoft.feign.service;
     
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
     
    @Component
    public class ErrorHandler implements HelloService {
        @Value("${server.port}")
        private String port;
     
        @Override
        public String obtainOtherServerJsonData(String name) {
            return "sorry " + name + " , " + port + " server internal error";
        }
    }
    

    第五节:路由网关zuul

    Zuul的主要功能是路由转发和过滤器。这个时候,我们需要统一的入口,接口地址全部由该入口进入。比如用/api同一接口,/api/user 发给user服务器,/api/shop 发给shop服务器。

    
    #配置网关路由
    zuul:
      routes:
        api-a:
          #/api-ribbon/**请求转发到service-ribbon服务器
          path: /api-ribbon/**
          serviceId: service-ribbon
        api-b:
          #/api-feign/**请求转发到service-feign服务器
          path: /api-feign/**
          serviceId: service-feign
    

    我向网关发送一条请求,http://localhost:8769/api-feign/sayHello?name=dayu,根据网关配置文件,/api-feign开头的请求会发给部署在8765端口下的feign服务器。所以,当前请求就相当于http://localhost:8765/sayHello?name=dayu。因为开启了两个Client,所以有可能请求会被转发到8763端口的服务器上。

    zuul网关提供了Filter相关的过滤器功能

    @Component
    public class SecurityFilter extends ZuulFilter ...
    

    第六节:分布式配置中心SpringCloudConfig

    配置文件统一管理,实时更新。SpringCloudConfig组件的作用就是从GIT上加载配置文件。这样的话,就可以避免因为某个配置文件更新,导致需要重启一些微服务的麻烦。

    第七节:高可用的分布式配置中心(Spring Cloud Config)

    随着服务的增多,如果都使用同一个配置中心,万一此配置中心挂了,那么依赖此配置中心的所有微服务系统就全部崩溃了。
    所以,为了避免这种情况的出现,那么就部署多个配置中心,防止某个配置中心节点故障导致的整个系统的崩溃的情况。


    image.png

    第八节:消息总线

    基本思路就是,某个Client发送一个 刷新Git配置文件的请求。然后通过消息总线向其它服务器传播此通知,通知所有的服务器更新配置文件,从而使整个微服务集群都达到更新配置文件的目的。

    消息总线通过消息中间件,例如RabbitMQ向其它微服务Client发送消息,所以其它微服务8881Client在收到通知后重新向配置中心请求最新的配置文件信息。

    相关文章

      网友评论

          本文标题:跟着大宇学SpringCloud

          本文链接:https://www.haomeiwen.com/subject/oaijmltx.html