目录
????????夫依赖?
????????eureka配置
????????zuul网关
????????接口提供方
????????接口调用方
????????feign调用接口与熔断
springcloud五大组件
- 服务发现——Netflix Eureka
- 客服端负载均衡——Netflix Ribbon
- 断路器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
- 注: feign包含ribbon与hystrix
夫依赖?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<!-- 此处定义公用的pom依赖, 若模块要使用 继承此pom即可 -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.5</version>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
eureka配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 8080
spring:
application:
name: ku-eureka
eureka:
client:
service-url:
defaultZone: http://localhost:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
# 轮训检查
eviction-interval-timer-in-ms: 5000
@EnableEurekaServer
zuul网关
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8083
spring:
application:
name: ku-zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
instance:
lease-expiration-duration-in-seconds: 20
lease-renewal-interval-in-seconds: 2
prefer-ip-address: false
hostname: zuul
zuul:
routes:
ku-provider: /admin/**
ku-customer: /out/**
host:
connect-timeout-millis: 3000
socket-timeout-millis: 3000
@EnableEurekaClient
@EnableZuulProxy
?zuul拦截器
@Component
public class ZuulConfig extends ZuulFilter {
/**
* 可选值有 pre(路由之前)、
* routing(路由之时)、
* post(路由之后)、
* error(发生错误时调用)。
*/
@Override
public String filterType() {
return "pre";
}
/**
* 若有多个过滤器 数字越小优先制行
*/
@Override
public int filterOrder() {
return 0;
}
/**
* 是否过滤
*/
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
// do something
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
String token = request.getHeader("access-token");
if (StringUtils.isBlank(token)) {
context.setSendZuulResponse(false);
context.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
context.setResponseBody("{\"status\":\"401\", \"text\":\"request error!\"}");
}
context.set("token", token);
return null;
}
}
接口提供方
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8081
spring:
application:
name: ku-provider # 要想使用负载均衡 该name必须保持一致
eureka:
instance:
# ip注册
prefer-ip-address: true
# 心跳时间
lease-renewal-interval-in-seconds: 2
# 20s没有收到心跳 剔除注册中心
lease-expiration-duration-in-seconds: 20
client:
service-url:
defaultZone: http://localhost:8080/eureka
接口调用方
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
server:
port: 8082
spring:
application:
name: ku-customer
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 2
lease-expiration-duration-in-seconds: 20
client:
service-url:
defaultZone: http://localhost:8080/eureka
# 启用熔断
feign:
circuitbreaker:
enabled: true
feign调用接口与熔断
简化了resttemplate, ribbon请求方式
接口调用
@FeignClient(value = "ku-provider", fallbackFactory = TestFeignClientFallbackFactoryService.class)
@Service
public interface FeignClientService {
@GetMapping("test")
String test();
}
熔断
@Service
public class TestFeignClientFallbackFactoryService implements FallbackFactory<FeignClientService> {
@Override
public FeignClientService create(Throwable throwable) {
// 服务熔断执行的方法
return () -> "服务提供者挂了";
}
}
注意
????????开启多个服务提供方或使用方, 端口号不一样, eureka中的name保持一致, zuul就会进行负载均衡, 把请求分摊至每个服务, 所以配置中一定要定义连接eureka的心跳时间与剔除时间, 因为zuul进行负载均衡时遵循轮训机制, 如果不配置剔除时间, 一个服务挂了, zuul还会进行请求转发.
|