spring-eureka&&Nacos&&Consul

微服务架构

  • 定义:把一个独立的服务拆分为多个功能独立可运行的服务
  • 优点:
    • 降低耦合度
    • 便于服务的横向扩容

什么是SpringCloud

  • 定义:是一些列组件的集合
  • 组件:
    • 注册中心【★★★★★】
    • 配置中心【★★☆☆☆】
    • 熔断器【★★☆☆☆】
    • 网关【★★★★★】
    • 负载均衡【★★☆☆☆】
    • 消息总线【★☆☆☆☆】
    • 数据监控【★☆☆☆☆】

SpringCloud与dubbo【精通】

  • Dubbo是基于RPC协议实现远程调用的,同时要求所用语言必须是Java
  • SpringCloud规定服务之间通过http协议进行通信
  • Dubbo性能较好,SpringCloud功能全

Eureka

  • 作用:服务的注册与发现(在微服务之间降低了服务之间的耦合度)
    • 服务提供者注册服务到注册中心
    • 服务消费者从注册中心获取服务信息(IP、端口)

Resttemplate

@Bean
public RestTemplate restTemplate2(){
    return new  RestTemplate(new OkHttp3ClientHttpRequestFactory());
}

在这里插入图片描述

搭建Eureka Server【精通】

  • 导包

    <!-- eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
  • 配置

    eureka:
      instance:
        hostname: localhost # 主机名
      client:
        register-with-eureka: false # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
        fetch-registry: false # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
    
  • 启动类

    @EnableEurekaServer
    

项目名中导入父工程的两种方式【精通】

  • 父工程集成

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>
    
  • Import导入

    <!--引入Spring Cloud 依赖-->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

搭建Eureka client【精通】

  • 导包

    <!-- eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  • 配置

    eureka:
      instance:
        hostname: localhost # 主机名
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
        register-with-eureka: true # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
        fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
    
  • 启动类

    @EnableEurekaClient
    

Eureka客户端如何获取服务列表

@Autowired
DiscoveryClient discoveryClient;

List<ServiceInstance> ins = discoveryClient.getInstances("服务名称");
String host = ins.get(0).getHost();

Eureka的重要配置属性【精通】

在这里插入图片描述

Eureka高可用的集群搭建【熟练】

Eureka服务端相互注册即可组成集群

  • server1

    eureka:
      client:
      	register-with-eureka: true #开启客户端功能
      	service-url:
          defaultZone: http://localhost:8762/eureka,http://localhost:8763/eureka   #注册服务到其它集群阶段,多台则逗号分隔
    
  • server2

    eureka:
      client:
      	register-with-eureka: true #开启客户端功能
      	service-url:
          defaultZone: http://localhost:8761/eureka,http://localhost:8763/eureka   #注册服务到其它集群阶段,多台则逗号分隔
    
  • client

    eureka:
      client:
      	service-url:
          defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka   #集群所有服务器的地址,逗号分隔
    

Consul客户端配置[了解]

  • 服务ip和地址
  • 服务名称
spring:
  cloud:
    consul:
      host: localhost # consul 服务端的 ip
      port: 8500 # consul 服务端的端口 默认8500
      discovery:
        service-name: ${spring.application.name} # 当前应用注册到consul的名称
        prefer-ip-address: true # 注册ip

  application:
    name: consul-provider # 应用名称

Nacos客户端配置[精通]

spring:
  cloud:
    nacos:
      discovery:
        server-addr:  127.0.0.1:8848 # 配置nacos 服务端地址
  application:
    name: nacos-consumer # 服务名称

Ribbon负载均衡器【了解】

  • 定义:解决客户端的服务负载均衡算法处理器

  • 功能:

    • 负载均衡
    • 简化远程调用过程
  • 使用:

    • 开启负载均衡功能(默认轮询策略)

      @LoadBanlance
      
    • 修改主机名为服务名

      http://服务名称/login.do
      
  • 怎么修改负载均衡策略

    • 注解方式

      ### xxx 是一个JavaConfig类,其中㤇配置使用的策略Bean
      @RibbonClient(name="",configuration = XXX.Class)
      
    • 配置方式

      [服务名称1]:
        ribbon:
         NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
      [服务名称2]:
        ribbon:
         NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule
      

ion = XXX.Class)
```

  • 配置方式

    [服务名称1]:
      ribbon:
       NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    [服务名称2]:
      ribbon:
       NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule
    

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值