Spring Cloud浅谈个人尝鲜------Eureka 服务治理(二)

Spring Cloud浅谈个人尝鲜------Eureka 服务治理(二)

准备工作:创建Maven的父子结构

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.22</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>

            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>6.5.0</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.5.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.54</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-spring</artifactId>
                <version>1.4.0</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.10</version>
            </dependency>
            <dependency >
                <groupId >com.sun.mail</groupId >
                <artifactId >javax.mail</artifactId >
                <version >1.5.4 </version >
            </dependency >
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

1. Eureka注册中心

1.1 需求分析

​ 在前后端分离架构中,服务层被拆分成了很多的微服务,微服务的信息如何管理?Spring Cloud中提供服务注册中心来管理微服务信息。

为什么要用注册中心?

1、微服务数量众多,要进行远程调用就需要知道服务端的ip地址和端口,注册中心帮助我们管理这些服务的ip和端口。

2、微服务会实时上报自己的状态,注册中心统一管理这些微服务的状态,将存在问题的服务踢出服务列表,客户端获取到可用的服务进行调用。

1.2 Eureka注册中心

1.2.1 Eureka介绍

​ Spring Cloud Eureka 是对Netflix公司的Eureka的二次封装,它实现了服务治理的功能,Spring Cloud Eureka提供服务端与客户端,服务端即是Eureka服务注册中心,客户端完成微服务向Eureka服务的注册与发现。服务端和客户端均采用Java语言编写。下图显示了Eureka Server与Eureka Client的关系:
在这里插入图片描述
1、Eureka Server是服务端,负责管理各各微服务结点的信息和状态。

2、在微服务上部署Eureka Client程序,远程访问Eureka Server将自己注册在Eureka Server。

3、微服务需要调用另一个微服务时从Eureka Server中获取服务调用地址,进行远程调用。

1.2.2 Eureka Server搭建

1、创建openapi-eureka工程:

包结构:com.lw.openapi.eureka
2、添加依赖

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

3、启动类

@SpringBootApplication
@EnableEurekaServer//开启eurekaserver,会自动帮我们配置
public class EurekaApplication {

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

4、@EnableEurekaServer
需要在启动类上用@EnableEurekaServer标识此服务为Eureka服务

5 application.yml的配置内容如下

server:
  port: 8000
spring:
  application:
    name: eureka-server

eureka:
  client:
    # 表示是否将自己注册到Eureka Server,默认为true。
    register-with-eureka: false
    # 表示是否从Eureka Server获取注册信息,默认为true。
    fetch-registry: false
    # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8000/eureka ;多个地址可使用,分隔
    service-url:
      defaultZone: http://localhost:8000/eureka/
  server:
    eviction-interval-timer-in-ms: 1000     # 续期时间,即扫描失效服务的间隔时间(单位毫秒,默认是60*1000)测试时关闭自我保护机制,保证不可用服务及时踢出
    enableSelfPreservation: false    # 设为false,关闭自我保护

registerWithEureka:被其它服务调用时需向Eureka注册

fetchRegistry:需要从Eureka中查找要调用的目标服务时需要设置为true

serviceUrl.defaultZone 配置上报Eureka服务地址高可用状态配置对方的地址,单机状态配置自己

enable-self-preservation:自保护设置,下边有介绍。

eviction-interval-timer-in-ms:清理失效结点的间隔,在这个时间段内如果没有收到该结点的上报则将结点从服务列表中剔除。

5、启动Eureka Server

启动Eureka Server,浏览8000端口
在这里插入图片描述

1.2.3 服务注册

1.2.3.1 将 test-client1 注册到Eureka Server

下边实现test-client1向Eureka Server注册。

1、在test-client1 服务中添加依赖

<!-- 导入Eureka客户端的依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
  </dependency>

2、在application.properties配置

spring:
  application:
    name: test-client1
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3、在启动类上添加注解

在启动类上添加注解 @EnableDiscoveryClient ,表示它是一个Eureka的客户端

4、刷新Eureka Server查看注册情况
发现已经成功的将服务注册上去了

1.2.3.2 将test-client2注册到Eureka Server

方法同上。

1、在test-client2工程中添加spring-cloud-starter-eureka依赖:

2、在application.property配置eureka

3、在启动类上添加注解 @EnableDiscoveryClient

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NotFoundObject.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值