学习springCloud(eurek、feign、Hystrix)组件

记录自己的SpringCloud 学习之路

一、SpringCloud Eureka服务发现 使用说明

Eureka Server 提供注册服务,各个节点启动后,会在Eureka Server中进行注
册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点
的信息可以在界面中直观的看到。

Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也
就别一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会
向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有
接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90
秒)。

Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机
制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务
的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活
性和可伸缩性。

1、服务端依赖

需要在父工程中锁定eureka的版本,父工程中springboot的版本为2.0.3依赖如下,注意:springboot与springcloud的版本有关系,具体请百度


<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">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.springcloud.demogroupId>
    <artifactId>cloud_parentartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>cloud_eurekamodule>
        <module>cloud_usermodule>
        <module>cloud_scoremodule>
    modules>
    <parent>
        
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.0.3.RELEASEversion>
             <relativePath/>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.20version>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Finchley.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

在eureka的服务端添加依赖


<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>cloud_parentartifactId>
        <groupId>com.springcloud.demogroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud_eurekaartifactId>
    <dependencies>
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>

project>

1.1、Eureka服务端配置文件

在eureka的配置文件中添加eureka配置,配置文件如下

server:
  port: 6868
spring:
  application:
    name: cloud-eureka
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
     defaultZone: http://127.0.0.1:${server.port}/eureka/
  instance:
    prefer-ip-address: true

1.2服务端启动类

修改启动类增加@EnableEurekaServer 注解

package com.springcloud.demo;

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

/**
 * Package: com.springcloud.demo
 * Date: Created in 2019/3/7 17:25
 *
 * @Company: 公司
 * Copyright: Copyright (c) 2017
 * @Version: 0.0.1
 * @author:weil-f Modified By:
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class,args);
    }
}

启动服务访问6868端口,看到如下页面,eureka启动成功

学习springCloud(eurek、feign、Hystrix)组件_第1张图片

2、eureka客户端依赖

数据可中共有两张表,user 和score学习springCloud(eurek、feign、Hystrix)组件_第2张图片

学习springCloud(eurek、feign、Hystrix)组件_第3张图片

第一个eureka客户端为user,新建模块,添加eureka客户端依赖,user的pom文件为


<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>cloud_parentartifactId>
        <groupId>com.springcloud.demogroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud_userartifactId>

    <dependencies>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
    dependencies>
project>

为user项目分别添加启动类和配置文件,配置文件为

server:
  port: 9201
spring:
  application:
    name: cloud-user
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test2?charset=UTF-8
    username: root
    password: root
  jpa:
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: none
eureka:
  client:
    register-with-eureka: true
    service-url:
     defaultZone: http://127.0.0.1:6868/eureka/
    fetch-registry: true
  instance:
    prefer-ip-address: true

2.2 客户端启动类

package com.springcloud.user;

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

/**
 * Package: com.springcloud.user
 * Date: Created in 2019/3/7 17:38
 *
 * @Company: 公司
 * Copyright: Copyright (c) 2017
 * @Version: 0.0.1
 * @author:weil-f Modified By:
 */
@EnableEurekaClient
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

在启动eureka客户端之前,必须要保证eureka服务端先起来哦,分别启动eureka服务端和客户端,最终访问eureka服务端界面如下

学习springCloud(eurek、feign、Hystrix)组件_第4张图片

看到cloud-user 已经注册到服务端了

二、Spring Feign 实现服务间的调用

eureka 负责服务的注册,feign负责服务的调用,按照user的步骤,新建score模块,现在想要在user模块中调用score模块中的内容,需要在调用方添加依赖,

在user的pom文件中添加如下依赖

>
>org.springframework.cloud>
>spring-cloud-starter-openfeign>
> 

然后在User模块的启动类上添加注解

@EnableDiscoveryClient
@EnableFeignClients 

在User模块中新建包client,添加接口,接口中写模块Score controller中的findAll方法,在接口上添加@FeignClient注解,接口如下

package com.springcloud.user.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * Package: com.springcloud.user.client
 * Date: Created in 2019/3/8 13:39
 *
 * @Company: 公司
 * Copyright: Copyright (c) 2017
 * @Version: 0.0.1
 * @author:weil-f Modified By:
 */
@Component
@FeignClient(value = "cloud-score")
public interface ScoreClient {
    @GetMapping("score/findAll")
    public List findAll();
}

将接口注入UserController,改造后的controller为

package com.springcloud.user.controller;

import com.springcloud.user.client.ScoreClient;
import com.springcloud.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Package: com.springcloud.user.controller
 * Date: Created in 2019/3/8 11:14
 *
 * @Company: 公司
 * Copyright: Copyright (c) 2017
 * @Version: 0.0.1
 * @author:weil-f Modified By:
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ScoreClient scoreClient;

    /**
     * 查询全部用户
     * @return
     */
    @GetMapping(value = "/findAll")
    public List findAll(){
        return userService.findAll();
    }

    /**
     * 查询全部成绩
     * @return
     */
    @GetMapping("/findScoreAll")
    public List findScoreAll(){
        return scoreClient.findAll();
    }
}

@FeignClient注解用于指定从哪个服务中调用功能 ,注意 里面的名称与被调用的服务
名保持一致,并且不能包含下划线

@RequestMapping注解用于对被调用的微服务进行地址映射。注意 @PathVariable注
解一定要指定参数名称,否则出错

分别启动Eureka、User、Score模块,通过user控制器来访问Score模块中的内容

在这里插入图片描述

三、熔断器Feign Hystrix

接续在Feign中的例子,如果在user模块中调用score模块,如果score模块宕掉了,然后就报错了,会影响用户的体验,

学习springCloud(eurek、feign、Hystrix)组件_第5张图片

我们在user模块中添加score项目接口的实现类,重写接口中的方法,并将该类加入spring容器。

添加的实现类为

package com.springcloud.user.client.impl;

import com.springcloud.user.client.ScoreClient;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class ScoreClientImpl implements ScoreClient {
    @Override
    public List findAll() {
        ArrayList<Object> dataList = new ArrayList<>();
        dataList.add("熔断器启动了");
        return dataList;
    }
}

修改@FeignClient(“A项目配置文件中的项目名称”) 注解,改为

package com.springcloud.user.client;

import com.springcloud.user.client.impl.ScoreClientImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;


@Component
@FeignClient(value = "cloud-score",fallback = ScoreClientImpl.class)
public interface ScoreClient {
    @GetMapping("score/findAll")
    public List findAll();
}

不要忘了修改配置文件,在user模块的配置文件中增加

#开启熔断器
feign:
  hystrix:
    enabled: true

这样如果score模块宕机了,通过user模块访问score模块项目依然可以访问,这里只是功能演示,逻辑处理按需要进行处理

学习springCloud(eurek、feign、Hystrix)组件_第6张图片

你可能感兴趣的:(springCloud)