首页 > 编程语言 > 详细

Spring Cloud

时间:2020-11-30 10:57:38      阅读:24      评论:0      收藏:0      [点我收藏+]

集群:多个服务都执行同一个功能(平时卖票和春运卖票)(倾向于运维层面)

分布式:一个复杂的任务拆成多个简单的任务(倾向于开发层面)

 

 

 

负载:对用户量很大的功能进行负载(比如:秒杀系统;而登录退出几乎不用)

 

 

 

Ribbon、Feign:负载均衡

Zuul:网关,统一进入接口(实现反向代理功能,内部实现动态路由,身份认证,IP过滤,数据监控等)

Hystrix:服务容错

Actuator:服务监控

Zipkin:服务跟踪

 

 

 

父工程pom.xml

 <!-- 引入Spring Boot的依赖 -->
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.7.RELEASE</version>
     </parent>
 ?
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
         <!-- 解决 JDK 9 以上没有 JAXB API 的问题,EurekaServer启动失败-->
         <dependency>
             <groupId>javax.xml.bind</groupId>
             <artifactId>jaxb-api</artifactId>
             <version>2.3.0</version>
         </dependency>
         <dependency>
             <groupId>com.sun.xml.bind</groupId>
             <artifactId>jaxb-impl</artifactId>
             <version>2.3.0</version>
         </dependency>
         <dependency>
             <groupId>com.sun.xml.bind</groupId>
             <artifactId>jaxb-core</artifactId>
             <version>2.3.0</version>
         </dependency>
         <dependency>
             <groupId>javax.activation</groupId>
             <artifactId>activation</artifactId>
             <version>1.1.1</version>
         </dependency>
     </dependencies>
 ?
     <!-- 引入Spring Cloud的依赖,管理Spring Cloud生态各个组件 -->
     <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>

 

 

 

EurekaServer

EurekaServer 注册中心 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

注册中心 配置文件 application.yml

 server:
  port: 8761
 eureka:
  client:
    service-url:
      defaultZero: http://localhost:8761/eureka/ #访问注册中心地址 http://localhost:8761
    register-with-eureka: false #是否要注册自己
    fetch-registry: false #是否要同步其他注册中心

注册中心 启动类 EurekaServerApplication.java

 package com.southwind;
 ?
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 ?
 @SpringBootApplication
 @EnableEurekaServer //注册中心
 public class EurekaServerApplication {
     public static void main(String[] args) {
         SpringApplication.run(EurekaServerApplication.class,args);
    }
 }
 ?

 

 

 

EurekaClient

EurekaClient 服务提供者 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

服务提供者 配置文件 application.yml

 server:
  port: 8010
 spring:
  application:
    name: provider #在注册中心名字
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #当前服务的ip注册到注册中心

 

 

 

@PathVariable("id"):从url获取数据

@RequestBody:获取前端传过来的实体类

@RequestParam:设置默认值,当前端没有传该参数时,调用默认值

 

 

 

UP:当前服务可用

DOWN:当前服务不可用

 

 

RestTemplate

RestTemplate使用:放在需要使用RestTemplate的启动类中

 @Bean
 public RestTemplate restTemplate(){
  return new RestTemplate();
 }
 @Autowired
 private RestTemplate restTemplate;
 ?
 restTemplate.getForEntity("url",结果集类型.class,参数或不填).getBody(); //返回封装类型,getBody返回需要类型
 restTemplate.getForObject("url",结果集类型.class,参数或不填); //返回泛型,不用getBody方法
 restTemplate.postForEntity("url",参数,null).getBody();
 restTemplate.postForObject("url",参数,null);
 restTemplate.put("url",参数);
 restTemplate.delete("url",参数);

 

 

 

Zuul

Zuul网关 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
         
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

Zuul网关 application.yml

 server:
  port: 8030
 spring:
  application:
    name: gateway #在注册中心名字
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
 zuul:
  routes:
    provider: /p/** #为服务提供者provider设置映射

Zuul网关 启动类 ZuulApplication.java

 @EnableZuulProxy            //网关启动类,包含@EnableZuulServer
 @EnableAutoConfiguration
 public class ZuulApplication {
     public static void main(String[] args) {
         SpringApplication.run(ZuulApplication.class,args);
    }
 }

Zuul自带负载均衡功能

 

 

 

Ribbon

Ribbon负载均衡(就是一个客户端)

负载均衡算法:随机、加权轮询、加权随机、轮询(默认是轮询)

前提:在注册中心注册后,也可以自己定义负载均衡算法

 

Ribbon 负载均衡 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

Ribbon 负载均衡 application.yml

 server:
  port: 8040
 spring:
  application:
    name: ribbon #在注册中心名字
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #当前服务的ip注册到注册中心

Ribbon使用:放在需要使用Ribbon的启动类中

 @Bean
 @LoadBalanced //基于Ribbon的负载均衡
 public RestTemplate restTemplate(){
  return new RestTemplate();
 }

 

 

 

Feign

Feign 负载均衡 (就是一个客户端)

整合了Ribbon和Hystrix,基于Ribbon实现的

Feign 负载均衡 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-openfeign</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

Feign 负载均衡 application.yml

 server:
  port: 8050
 spring:
  application:
    name: feign #在注册中心名字
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #当前服务的ip注册到注册中心

Feign 负载均衡 FeignApplication.java

 @SpringBootApplication
 @EnableFeignClients
 public class FeignApplication {
     public static void main(String[] args) {
         SpringApplication.run(FeignApplication.class,args);
    }
 }

FeignProviderClient 接口,之后在Controller层创建声明式接口(private FeignProviderClient feignProviderClient,@Autowired引入)

 @FeignClient(value = "provider")
 public interface FeignProviderClient{
     @GetMapping("/student/index")
  public String index();
 }

加入熔断机制

server:
  port: 8050
spring:
  application:
    name: feign				#在注册中心名字
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true		#当前服务的ip注册到注册中心
feign:
  hystrix:
    enabled: true				#加入熔断机制

创建FeignProviderClient 接口实现类FeignError

 @Component      //注入到Ioc容器中
 public class FeignError implement FeignProviderClient{
  @Override
     public String index(){
         return "服务器维护中。。。";
    }
 }
 ?

FeignProviderClient 接口,设置@FeignClient的fallback属性设置映射

 @FeignClient(value = "provider",fallback = FeignError.class)
 public interface FeignProviderClient{
     @GetMapping("/student/index")
  public String index();
 }

 

 

 

Hystrix

Hystrix 容错机制

实时监控和报警功能,实时配置修改功能

Hystrix数据监控功能需要结合SpringBoot Actuator来使用

Actuator:对服务提供健康监控、数据统计,可以通过hystrix.stream节点获取监控请求数据,提供可视化的监控界面

Hystrix 容错机制 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-openfeign</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
             <version>2.0.7.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

Hystrix 容错机制 application.yml

(localhost:8060/actuator/hystrix.stream可以监控请求数据)

(localhost:8060/hystrix可视化监控界面,输入要监控的地址节点,即上面地址,可看到该节点的可视化数据监控)

 server:
  port: 8060
 spring:
  application:
    name: hystrix #在注册中心名字
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #当前服务的ip注册到注册中心
 feign:
  hystrix:
    enabled: true #加入熔断机制
 management:
  endpoints:
    web:
      exposure:
        include: ‘hystrix.stream‘

Hystrix 容错机制 HystrixApplication.java

 @SpringBootApplication
 @EnableFeignClients
 @EnableCircuitBreaker //声明启用数据监控
 @EnableHystrixDashboard //声明启用可视化数据监控
 public class HystrixApplication {
     public static void main(String[] args) {
         SpringApplication.run(HystrixApplication.class,args);
    }
 }

 

 

 

Config

本地配置

Config Server

ConfigServer 配置中心 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-config-server</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

ConfigServer 配置中心 application.yml(resources文件下新建shared文件夹,并在新文件夹下新建configclient-dev.yml文件)

 server:
  port: 8762
 spring:
  application:
    name: configserver
  profiles:
    active: native #配置文件读取方式
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared #本地配置文件存放路径

ConfigServer 配置中心 启动类 ConfigApplication.java

 package com.southwind;
 ?
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.config.server.EnableConfigServer;
 ?
 @SpringBootApplication
 @EnableConfigServer //配置中心
 public class ConfigServerApplication {
     public static void main(String[] args) {
         SpringApplication.run(ConfigServerApplication.class,args);
    }
 }

 

Config Client

ConfigClient 配置客户端 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-config</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

ConfigClient 配置客户端 bootstrap.yml

 spring:
  application:
    name: configclient  
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762 #本地Config Server 的访问路径
      fail-fast: true #设置客户端优先判断Config Server获取是否正常

 

远程配置

Config Server

ConfigServer 配置中心 application.yml

 server:
  port: 8888
 spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: http://github.com/...
          searchPaths: config
          username: root
          password: root
      label: master
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

 

Config Client

ConfigClient 配置中心 bootstrap.yml

 spring:
  cloud:
    config:
      name: configclient #与远程仓库的配置文件名对应
      label: master #Git Repository的分支
      discovery:
        enabled: true #是否开启Config服务发现支持
        service-id: configserver #配置中心在Eureka Server上注册的名称
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

 

 

 

Zipkin

监控各个微服务所耗费的时间

Zipkin Server:采集数据

Zipkin Client:展示数据

Zipkin Server

Zipkin Server 服务端 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>io.zipkin.java</groupId>
             <artifactId>zipkin-server</artifactId>
             <version>2.9.4</version>
         </dependency>
         <dependency>
             <groupId>io.zipkin.java</groupId>
             <artifactId>zipkin-autoconfigure-ui</artifactId>
             <version>2.9.4</version>
         </dependency>
     </dependencies>

Zipkin 服务跟踪 application.yml(访问地址:localhost:9090/zipkin/)

 server:
  port: 9090

Zipkin 服务跟踪 启动类 ZipkinApplication.java

 package com.southwind;
 ?
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import zipkin.server.internal.EnableZipkinServer;
 ?
 @SpringBootApplication
 @EnableZipkinServer //声明Zipkin Server
 public class ZipkinApplication {
     public static void main(String[] args) {
         SpringApplication.run(ZipkinApplication.class,args);
    }
 }
 ?

 

Zipkin Client

Zipkin Client 客户端 pom.xml

     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-zipkin</artifactId>
             <version>2.0.2.RELEASE</version>
         </dependency>
     </dependencies>

Zipkin Client 客户端 application.yml

 server:
  port: 8090
 spring:
  application:
    name: zipkinclient
  sleuth:
    web:
      client:
        enabled: true #设置开启请求跟踪
    sampler:
      probability: 1.0 #设置采用比例,默认1.0
  zipkin:
    base-url: http://localhost:9090/ #Zipkin Server地址
 eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

 

Spring Cloud

原文:https://www.cnblogs.com/wzcwzc/p/14059647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!