IDEA之搭建SpringCloud项目

IDEA之搭建SpringCloud项目,第1张

IDEA之搭建SpringCloud项目

目录

一、简介

1.1 Spring-Cloud Euraka介绍1.2 Euraka介绍 二、部署Eureka Server

2.1 创建项目2.2 配置文件2.3 启动类2.4 测试 三、部署Eureka Client

3.1 部署Service Provider

3.1.1 创建项目3.1.2 配置文件3.1.3 启动类3.1.4 请求接口3.1.5 测试 3.2 部署Servcie Customer

3.2.1 创建项目3.2.2 配置文件3.2.3 启动类3.2.4 请求接口3.2.5 测试 3.2.6 补充 四、自我保护机制4.1 Eureka Server端属性4.2 Eureka Client端属性

一、简介

SpringCloud是基于SpringBoot的一整套实现微服务的框架。他提供了微服务开发所需的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等组件。

1.1 Spring-Cloud Euraka介绍

Spring-Cloud Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。

1.2 Euraka介绍

Eureka由多个instance(服务实例)组成,这些服务实例可以分为两种:Eureka Server和Eureka Client。为了便于理解,我们将Eureka client再分为Service Provider和Service Consumer。

Eureka Server 提供服务注册和发现Service Provider 服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到Service Consumer服务消费方,从Eureka获取注册服务列表,从而能够消费服务 二、部署Eureka Server 2.1 创建项目

以下 *** 作基于idea 2020.1.2版本

    选择Spring Initializr

    选择Eureka Server
2.2 配置文件

这里我用的.yml格式的配置文件

server:
  port: 8086

eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8086/eureka/

2.3 启动类

对启动类添加注解**@EnableEurekaServer**,将该服务标识为Eureka Server

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
2.4 测试

访问localhost:8086

三、部署Eureka Client

1、Eureka Client包括两个服务模块:Service Provider(服务提供方)和Service Consumer(服务消费方)。
2、Eureka Client和Eureka Server目录类似, 不同点在于:

启动类,使用@EnableDiscoveryClient 标识该服务为Euraka Client配置文件,需要指定Euraka Server地址和当前服务注册时的名称。 3.1 部署Service Provider 3.1.1 创建项目

创建方法与Eureka Server一致。

3.1.2 配置文件

这里用的application.yml

server:
  port: 8082

spring:
  application:
    name: service-provider1

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

参数说明:

eureka.client.serviceUrl.defaultZone:指定Eureka Server的地址spring.application.name:当前服务注册在Eureka Server的名称。 3.1.3 启动类

使用@EnalbeDiscoveryClinet标识当前服务为Euraka Client。

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
3.1.4 请求接口
@RestController
public class TestController {

    @RequestMapping(value = "/sayHi")
    public void sayHi(){
        System.out.println("Hello,this is the provider 1");
    }
}
3.1.5 测试

打开服务注册中心,能看到新注册得服务名称

3.2 部署Servcie Customer 3.2.1 创建项目

创建方法与Eureka Server一致。

3.2.2 配置文件

这里用的application.yml

server:
  port: 8081

spring:
  application:
    name: service-customer1

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

3.2.3 启动类

使用@EnalbeDiscoveryClinet标识当前服务为Euraka Client。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
3.2.4 请求接口

从Euraka Server中获取服务提供方的服务地址信息

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ClientControlelr {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/queryService")
    @ResponseBody
    public String query() {
        List instances =discoveryClient.getInstances("service-provider1");
        StringBuilder urls= new StringBuilder();
        for(ServiceInstance instance : instances){
            urls.append(instance.getHost()+":"+instance.getPort()).append(",");
        }
        return urls.toString();
    }
}
3.2.5 测试

3.2.6 补充

如果Service Customer只是从Eureka Server中获取注册服务的地址信息,可以不用注册到Eureka Server中,如果本身也是一个Service Porvider,那么此时就需要注册服务了。

四、自我保护机制

在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 提供的一个特性,在默认的情况下是打开的。当Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)

4.1 Eureka Server端属性
# 设为false,关闭自我保护。默认是打开的。
eureka.server.enable-self-preservation=false
# 清理实例失效间隔(单位毫秒,默认是60*1000)
eureka.server.eviction-interval-timer-in-ms=4000

注意:

服务端的“eureka.server.eviction-interval-timer-in-ms” 值 要比 客户端配置 “lease-expiration-duration-in-seconds ”的时间短。

4.2 Eureka Client端属性
# 开启健康检查,默认是开启的
eureka.client.healthcheck.enabled=true
# 单位是秒,默认30秒。此客户端发送心跳的频率
eureka.instance.lease-renewal-interval-in-seconds=30
# 单位是秒,默认90秒,表示eureka server在收到此client上次心跳之后,间隔多久没有收到,就摘除此服务。
eureka.instance.lease-expiration-duration-in-seconds=10

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5707417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存