抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

SpringCloud五大组件

SpringCloud常用组件Eureka、Feign、Hystrix、Ribbon、Zuul

Eureka

Eureka:服务注册与发现中心
Eureka Server和Eureka Client
avatar

SpringBoot引入Eureka(使SpringBoot拥有Eureka Server的能力)

1.引入依赖
父模块的pom.xml标识SpringCloud版本

1
2
3
4
5
6
7
8
9
10
11
12
<!--标识Spring Cloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

子模块的pom.xml引入依赖

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

2.配置文件
application.properties

1
2
3
4
5
6
7
8
9
spring.application.name=eureka-server
server.port=8000
#主机名称
eureka.instance.hostname=localhost
#fetch-registry:获取注册表。不需要同步其他节点数据。
eureka.client.fetch-registry=false
#register-with-eureka:是否将自己注册到Eureka Server,默认使true。
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

3.启动注解
@EnableEurekaServer开启Eureka Server服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.silkage.course;

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

/*
* 描述 : Eureka服务端
* */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}

注册服务到Eureka Server

1.引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.配置文件
application.properties

1
2
#找到Eureka Server的地址
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

Feign

Feign是一种声明式模板化的http客户端,在SpringCloud中使用Feign组件可以非常方便的调用http的远程请求
avatar

集成Feign

1.引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.配置文件
使用Ribbon在客户端方面处理负载均衡
application.properties

1
course-list.ribbon.NFloadBanlancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

3.注解
@EnableFeignClients

1
2
3
4
5
6
7
@SpringBootApplication
@EnableFeignClients
public class CoursePriceApplication {
public static void main(String[] args) {
SpringApplication.run(CoursePriceApplication.class,args);
}
}

4.建立需要使用服务的客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.silkage.course.client;

import com.silkage.course.entity.Course;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/*
* 描述 : 课程列表的Feign客户端
* */
@FeignClient("course-list")
public interface CourseListClient {
//使用courselist服务的方法
@GetMapping("/courses")
List<Course> courseList();
}

Ribbon

Ribbon用于客户端的负载均衡,由调用服务者处理负载均衡的问题(Ribbon处理服务与服务之间相互调用的负载均衡)

负载均衡策略

1.RandomRule表示随机策略
2.RoundRobinRule表示轮询策略
3.ResponseTimeWeightedRule加权,根据每个Server平均响应时间动态加权

Hystrix

当在分布式的系统中,某个服务出现问题,会导致线程无法调用故障服务,并且还会持续占用其他资源最后导致系统崩溃。
Hystrix用于实现断路器,断路器作用是当某个服务出现问题不可用时,使用断路器来断路

实现断路功能

1.引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.配置文件开启功能

1
feign.hystrix.enabled=true

3.SpringBoot启动类上添加注解
@EnableCircuitBreaker
4.编写Hystrix断路类

1
2
3
4
5
6
7
8
//这是默认调用的服务,当此服务出现异常,会调用fallback,fallback是断路类
@FeignClient(value = "course-list",fallback = CourseListClientHystrix.class)
@Primary
public interface CourseListClient {
@GetMapping("/courses")
List<Course> courseList();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.silkage.course.client;

import com.silkage.course.entity.Course;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class CourseListClientHystrix implements CourseListClient{
@Override
public List<Course> courseList() {
List<Course> defaultCourses = new ArrayList<>();
Course course = new Course();
course.setCourseId(1);
course.setId(1);
course.setCourseName("默认课程");
course.setValid(1);
defaultCourses.add(course);
return defaultCourses;
}
}

5.调用服务类

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class CoursePriceController {
@Autowired
CourseListClient courseListClient;

@GetMapping("/coursesInPrice")
public List<Course> getCourseListInPrice(Integer courseId){
List<Course> courseList = courseListClient.courseList();
return courseList;
}
}

网关Zuul

Zuul主要的功能:
1.解决签名校验、登录校验冗余的问题
2.API网关允许您将API请求(内部或外部)路由到正确的位置
avatar

集成网关

1.引入zuul依赖,和eureka-client依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

2.编写配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring.application.name=course-gateway
server.port=9000
#日志格式
logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}
#注册到Eureka服务中
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#配置路由访问服务
zuul.prefix=/silkage
#course-list是一个服务,自定义服务的访问路径(访问路径:http://localhost:9000/silkage/list/courses)
# **表示服务中的方法
zuul.routes.course-list.path=/list/**
zuul.routes.course-list.service-id=course-list
zuul.routes.course-price.path=/price/**
zuul.routes.course-price.service-id=course-price

3.网关启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.silkage.course;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/*
* 描述 : 网关启动类
* */
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class,args);
}
}

利用网关实现过滤器

1.pre过滤器在路由请求之前运行
2.route过滤器可以处理请求的实际路由
3.post路由请求后运行过滤器
4.error如果在处理请求的过程中发生错误,则过滤器将运行