SpringCloud的Eureka、Zuul、Ribbon、Feign、Hystix

服务提供者:

对外查询的接口:

package edu.xatu.controller;

import edu.xatu.entity.User;
import edu.xatu.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("findUserById/{uid}")
    public User findUserById(@PathVariable("uid") Integer uid){
        return userService.findUserById(uid);
    }
}

Service:

package edu.xatu.service;

import edu.xatu.dao.UserDao;
import edu.xatu.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public User findUserById(int uid){
        System.out.println(userDao.selectByPrimaryKey(uid));
        return userDao.selectByPrimaryKey(uid);
    }
}

dao:

package edu.xatu.dao;

import edu.xatu.entity.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserDao extends Mapper<User> {
}

entity:

package edu.xatu.entity;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "t_user")
@Data
public class User {
    @Id
    private int uid;
    private String username;
    private String password;
}

文件配置:


spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
mybatis:
  type-aliases-package: edu.xatu.entity
server:
  port: 8081

启动类:

package edu.xatu;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;


@SpringBootApplication
@MapperScan("edu.xatu.dao")
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class);
    }
}


项目结构:
SpringCloud的Eureka、Zuul、Ribbon、Feign、Hystix_第1张图片

Eureka:

Eureka就是注册中心,我们所有的程序在写的时候因为有各自的端口,链接,不方便统一使用,为了方便使用和管理我们将其注册在注册中心。

pom配置:

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

启动类:

package edu.xatu;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class);
    }
}

yml配置:

server:
  port: 10000
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10001/eureka
  instance:
    lease-expiration-duration-in-seconds: 90
    lease-renewal-interval-in-seconds: 30
  server:
    eviction-interval-timer-in-ms: 60000
    enable-self-preservation: false
spring:
  application:
    name: eureka_server

我们在其他的yml文件中只需要配置

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

就能将自己注册在注册中心。
然后在启动类中配置 @EnableDiscoveryClient

Ribbon

负载均衡策略,可以改变访问服务器的方法,减轻服务器负担

我们直接在Eureka的yml配置中增加:

user-service:
  ribbon:
    NFLoadBalancerRuLeClassName: com.netflix.loadbalancer.RandomRule

就变为轮询的访问方式

Hystix

熔断机制,当访问的服务出了问题,将请求快速拒绝,防止服务器雪崩

在注册中心的pom.xml中配置

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>

然后在启动类中添加 @EnableCircuitBreaker
这样就启动熔断了,我们需要在可能熔断的地方进行注解配置。
@HystrixCommand(fallbackMethod = “mapping的名称”)

Feign

Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。

伪装的时候会自己配置负载均衡,所以在配置伪装的时候就可以不需要配置Ribbon了。
pom,xml

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

在服务注册中心下再写一个接口配置 @FeignClient,里面一定要写服务名,下面的@getMapping里要写全路径。
在伪装中配置熔断
在yml中开启熔断:

feign:
  hystrix:
    enabled: true
package deu.xatu.feign;

import deu.xatu.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "user-service",fallback = UserServiceFeignFallback.class)
public interface UserServiceFeign {
    @GetMapping("/user_service/findUserById/{uid}")
    public User findUserById(@PathVariable("uid") Integer uid);
}

给@FeignClient的fallback赋值就行。
UserServiceFeignFallback:

package deu.xatu.feign;

import deu.xatu.entity.User;
import org.springframework.stereotype.Component;

@Component
public class UserServiceFeignFallback implements UserServiceFeign {
    @Override
    public User findUserById(Integer uid) {
        User u=new User();
        u.setUsername("对不起,访问太忙!请稍后再试...");
        return u;
    }
}

这样就完成了在伪装中的熔断。

Zuul

Zuul就是我们服务的统一入口。不管是来自于客户端(PC或移动端)的请求,还是服务内部调用。一切对服务的请求都会经过网关,然后再由网关来实现 鉴权、动态路由等等操作。

Zuul的简单实现
它的简单实现。

你可能感兴趣的:(Spring框架)