学习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的版本有关系,具体请百度

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

    <groupId>com.springcloud.demo</groupId>
    <artifactId>cloud_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>cloud_eureka</module>
        <module>cloud_user</module>
        <module>cloud_score</module>
    </modules>
    <parent>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
             <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

在eureka的服务端添加依赖

<?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>cloud_parent</artifactId>
        <groupId>com.springcloud.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_eureka</artifactId>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <!--eureka 服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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启动成功

eureka启动

2、eureka客户端依赖

数据可中共有两张表,user 和score在这里插入图片描述

在这里插入图片描述

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

<?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>cloud_parent</artifactId>
        <groupId>com.springcloud.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_user</artifactId>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--eureka 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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服务端界面如下

在这里插入图片描述

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

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

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

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> 

然后在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模块宕掉了,然后就报错了,会影响用户的体验,

在这里插入图片描述

我们在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模块项目依然可以访问,这里只是功能演示,逻辑处理按需要进行处理

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值