SpringCloud学习2

1.Eureka注册中心说明

1.1 主要功能

DevOps的思想是系统可以通过一组过程方法或系统;提高应用发布和运维的效率,降低管理成本。

Eureka就好比是滴滴,负责管理、记录服务提供者的信息。服务调用者无需自己寻找服务,而是把自己的需求告诉 Eureka,然后Eureka会把符合你需求的服务告诉你。

同时,服务提供方与Eureka之间通过“心跳”机制进行监控,当某个服务提供方出现问题,Eureka自然会把它从服务 列表中剔除。
这就实现了服务的自动注册、发现、状态监控。

2.搭建eureka-server工程

2.1 服务注册

创建一个项目eureka-server,启动一个Eureka Server Application服务注册中心。

依赖

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

    <artifactId>eureka-server</artifactId>

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


</project>

 启动类

package com.itls;

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

@EnableEurekaServer   //声明当前应用为eureka服务
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class);
    }
}

 配置文件

server:
  port: 10086

spring:
  application:
    name: eureka-server   # 应用名称,会在Eureka中作为服务的id标识(serviceId)

eureka:
  client:
    service-url:   # EurekaServer的地址,现在是自己的地址,如果是集群,需要写其它Server的地址。
      defaultZone: http://127.0.0.1:10086/eureka

    register-with-eureka: false # 不注册自己
    fetch-registry: false #不拉取

启动 eureka-server 访问:http://127.0.0.1:10086

注册服务,就是在服务上添加Eureka的客户端依赖,客户端代码会自动把服务注册到EurekaServer中。

        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

通过添加@EnableDiscoveryClient来开启Eureka客户端功能

 配置文件

server:
  port: 9091

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
  application: #应用名 
    name: user-service  #spring.application.name属性来指定应用名称,将来会作为服务的id使用。

mybatis:
  type-aliases-package: com.itls.user.pojo

eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka

2.2 服务发现 

方法与服务提供者类似,需要在项目中添加EurekaClient依赖,就可以通过服务名称来获取信息了!

依赖

        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

启动类

package com.itls.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
@EnableDiscoveryClient  //开启Eureka客户端
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

配置文件

server:
  port: 8080
spring:
  application:
    name: consumer-demo # 应用名称
eureka:
  client:
    service-url: # EurekaServer地址
      defaultZone: http://127.0.0.1:10086/eureka

ConsumerController.java

package com.itls.consumer.controller;

import com.itls.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired private
    DiscoveryClient discoveryClient;

    @GetMapping("{id}")
    public User queryById(@PathVariable Long id){
        String url = "http://localhost:9091/user/" + id;

        //获取eureka中注册的user-service实例列表
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("user-service");
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/" + id;

        return restTemplate.getForObject(url, User.class);
    }
}

 url一致

3.Eureka详解

3.1 基础架构

3.2 高可用的Eureka Server

服务同步

多个Eureka Server之间也会互相注册为服务,当服务提供者注册到Eureka Server集群中的某个节点时,该节点会把 服务的信息同步给集群中的每个节点,从而实现数据同步。因此,无论客户端访问到Eureka Server集群中的任意一 个节点,都可以获取到完整的服务列表信息。

动手搭建高可用的EurekaServer

搭建两台EurekaServer的集群,端口分别为:10086和10087

server:
  port: ${port:10086}

spring:
  application: # 应用名称,会在eureka中作为服务的id(serviceId)
    name: eureka-server

eureka:
  client:
    service-url: # eureka服务地址;如果是集群则是其它服务器地址,后面要加/eureka
      defaultZone: ${defaultZone:http://127.0.0.1:10086/eureka}
      # 是否注册自己,自身不提供服务所以不注册
      #register-with-eureka: false
      # 是否拉取服务
      #fetch-registry: false

所谓的高可用注册中心,其实就是把EurekaServer自己也作为一个服务,注册到其它EurekaServer上,这样多个EurekaServer之间就能互相发现对方,从而形成集群。

注意把register-with-eureka和fetch-registry修改为true或者注释掉

在上述配置文件中的${}表示在jvm启动时候若能找到对应port或者defaultZone参数则使用,若无则使用后面的默认值

把service-url的值改成了另外一台EurekaServer的地址,而不是自己

VM options中设置 

原来组件配置 -DdefaultZone=http:127.0.0.1:10087/eureka

复制一份 -Dport=10087 -DdefaultZone=http:127.0.0.1:10086/eureka

 

3.3 Eureka客户端 

服务注册

 服务续约

 获取服务列表

当服务消费者启动时,会检测eureka.client.fetch-registry=true参数的值,如果为true,则会从Eureka Server服务的列表拉取只读备份,然后缓存在本地。并且每隔30秒会重新拉取并更新数据。可以在consumer-demo 项目中通过下面的参数来修改:

eureka: 
    client: 
        registry-fetch-interval-seconds: 30

失效剔除

服务注册中心在启动时会创建一个定时任务,默认每隔一段时间 (默认为60秒)将当前清单中超时(默认为90秒)没有续约的服务剔除,这个操作被称为失效剔除。

可以通过eureka.server.eviction-interval-timer-in-ms参数对其进行修改,单位是毫秒。

自我保护

eureka: 
    server: 
        enable-self-preservation: false # 关闭自我保护模式(缺省为打开)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值