博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud 快速入门总结
阅读量:4152 次
发布时间:2019-05-25

本文共 2532 字,大约阅读时间需要 8 分钟。

一.服务注册和发现 netflix Eureka

  有一种保护模式
  服务端:
  1.导坐标
        <dependency>          
        <groupId>org.springframework.cloud</groupId>           
        <artifactId>spring‐cloud‐starter‐netflix‐eurekaserver</artifactId>     
        </dependency>     
  2.配置文件
  3.启动类
    加上注解 @EnableEurekaServer 

  服务注册:

  1.添加坐标
  2.配置文件
  3.启动类增加注解@EnableEurekaClient
  
  
  
二.服务调用 netflix Feign
  负载均衡的调用服务
  在调用的项目中
  1.导坐标
  2.启动类中增加注解@EnableFeignClient
  3.写接口
    @FeignClient("tensquere-base")
    public interface LabelClient {
     @RequestMapping(value="/label/{id}", method = RequestMethod.GET)     
     public Result findById(@PathVariable("id") String id); 
    }

三.熔断器 netflix Hystrix

  Feign 本身支持Hystrix,不需要额外引入依赖
  1.增加配置文件
    feign:   hystrix:     enabled: true
  2.增加接口的实现类
    @component
    public class LabelClientImpl implements LabelClient {
      @Override    
      public Result findById(String id) {       
      return new Result(false, StatusCode.ERROR,"熔断器启动了");    
      } 
    }
  
  3.修改接口上的注解
   @FeignClient(value="tensquare‐base",fallback = LabelClientImpl.class)
  
  
  
  
  
四.微服务网关 netflix Zuul
  1.导坐标
     <dependency>             
     <groupId>org.springframework.cloud</groupId>            
     <artifactId>spring‐cloud‐starter‐netflix‐eurekaclient</artifactId>       
     </dependency>        
     <dependency>            
     <groupId>org.springframework.cloud</groupId>            
     <artifactId>spring‐cloud‐starter‐netflix‐zuul</artifactId>   
     </dependency> 

  2.增加配置文件

    zuul:  
      routes:     tensquare‐gathering: #活动      
      path: /gathering/** #配置请求URL的请求规则      
      serviceId: tensquare‐gathering #指定Eureka注册中心中的服务id 

  3.启动类

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

  4.过滤类

    @Component public class WebFilter extends ZuulFilter {
      @Override     
      public String filterType() {      
      return "pre";// 前置过滤器    pre   route   post  error
      }
      @Override    
      public int filterOrder() {       
      return 0;// 优先级为0,数字越大,优先级越低   
      }
      @Override    
      public boolean shouldFilter() {    
      return true;// 是否执行该过滤器,此处为true,说明需要过滤  
      }
      @Override   
      public Object run() throws ZuulException {    
      System.out.println("zuul过滤器...");      
      return null;     
      }
    }

五.分布式配置 springCloud config
  配置中心微服务
  1.导坐标
  <dependencies>   
  <dependency>         
  <groupId>org.springframework.cloud</groupId>     
  <artifactId>spring‐cloud‐config‐server</artifactId>     
  </dependency>   
  </dependencies>
  2.配置文件
  3.启动类
  @EnableConfigServer
  @SpringBootApplication 
  public class ConfigServerApplication {  
    public static void main(String[] args) {   
    SpringApplication.run(ConfigServerApplication.class, args); 
    } 
  }
  
  
  客户端
  1.导坐标
  2.添加bootstrap.yml ,删除application.yml 
  
  
  

六.消息通知总线 springCloud Bus

  服务端
  1.导坐标
  2.配置文件
  
  客户端
  1.导坐标
  2.马云上的配置文件增加配置
     rabbitmq:    
      host: 192.168.184.144
      
修改马云上的配置文件后  要执行 Url: http://127.0.0.1:12000/actuator/bus-refresh   Method: post 
如果是自定义配置文件  则要在类上增加注解@RefreshScope
  
  
 

转载地址:http://uolti.baihongyu.com/

你可能感兴趣的文章
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>
性能扩展问题要趁早
查看>>
MySQL-数据库、数据表结构操作(SQL)
查看>>
OpenLDAP for Windows 安装手册(2.4.26版)
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
JSP的内置对象及方法
查看>>