美文网首页
Spring Cloud

Spring Cloud

作者: 美美的苹果核 | 来源:发表于2021-07-08 11:41 被阅读0次

公共抽象类 Commons

DiscoveryClient

服务发现,如EurekaDiscoveryClient

ServiceRegistry

服务注册,如EurekaServiceRegistry

RestTemplate

HTTP请求,可以通过setInterceptors添加拦截器对请求进行功能扩展,如负载均衡的实现

配置中心

Config

  • @EnableConfigServer

Nacos

服务注册与发现

Eureka 参考

  • @EnableEurekaServer 启用服务端
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
#服务注册中心端口号
server.port=1110

#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  • @EnableEurekaClient 或 @EnableDiscoveryClient 启用客户端
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
#服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://host:port/eureka/
  • 启用账号密码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
spring.security.user.name=aaaa
spring.security.user.password=123456
  • 工作步骤
# server端
服务注册(Register)-> 服务续约(Renew)-> 服务下线(Cancel)-> 服务剔除(Eviction)

# producer端
服务注册(Register)-> 服务续约(Renew)-> 服务下线(Cancel)

# consumer端
获取服务列表(Fetch)-> 更新服务列表(Update)

Nacos

负载均衡

Ribbon

  • @LoadBalanced
  • 均衡restRemplate实例请求的Rest API

Nginx

LoadBalancer

  • 替代Ribbon

容错保护

Hystrix

  • 服务降级
  • 资源隔离,防止线程长时间占用耗尽tomcat的线程
  • 服务熔断

Sentinel 官网

  • 流量控制

服务调用

RestTemplate

OpenFeign

  • @EnableFeignClients启用Feign
  • @FeignClient创建bean
  • @RequestMapping映射Rest API
  • 自带断路器hystrix
  • 自动负载均衡ribbon
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

网关路由

Zuul

  • @EnableZuulProxy
  • 配置路由
  • 服务过滤filter
zuul:
  ignoredServices: '*'
  host:
    connect-timeout-millis: 20000
    socket-timeout-millis: 20000
    
  routes:
    auth-service:
        path: /uaa/**
        url: http://auth-service:5000
        stripPrefix: false
        sensitiveHeaders:

    account-service:
        path: /accounts/**
        serviceId: account-service
        stripPrefix: false
        sensitiveHeaders:

Gateway

  • 替代zuul

健康检查

Actuator

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

通过HTTP暴露Actuator endpoints

# 对外暴露的endpoints,默认health,info
management.endpoints.web.exposure.include = *
# 显示详细信息
management.endpoint.health.show-details = always
# 关闭mongo健康检查
management.health.mongo.enabled = false

常用接口

# 查看actuator接口
http://xxx.com/config/actuator
# 健康检查
http://xxx.com/config/actuator/health
# 容器的Bean
http://xxx.com/config/actuator/beans

自定义

@Endpoint 构建 rest api 的唯一路径

@ReadOperation GET请求,响应状态为 200 如果没有返回值响应 404
@WriteOperation POST请求,响应状态为 200 如果没有返回值响应 204
@DeleteOperation DELETE请求,响应状态为 200 如果没有返回值响应 204

@Selector 获取路径上的参数

相关文章

网友评论

      本文标题:Spring Cloud

      本文链接:https://www.haomeiwen.com/subject/ztibyltx.html