spring4 系列四 cloud

一、概述 spring提供了一套分布式环境的基础框架,可以用于实现基于服务的架构。使用这些服务基本上都需要添加pom依赖、添加配置、代码中添加注解。

二、具体服务

1、服务注册和发现

<groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka-server</artifactId>

 

    @EnableEurekaServer

 

    eureka:

  instance:

    hostname: peer1

 client:

    serviceUrl:

      defaultZone: http://peer2:8762/eureka/ # 互相备份

 

    2、具体服务的注册

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka</artifactId>

 

    @EnableDiscoveryClient

    @Autowired

  private DiscoveryClient discoveryClient;

 

    eureka:

 client:

   serviceUrl:

     defaultZone: http://10.88.102.203:8761/eureka/

 instance:

   preferIpAddress: true

3、消费服务方式一 ribbon

ribbon可以实现寻找同一个Zone且负载较少的eureka Server,通过uereka server获取服务列表,客户端负载均衡、超时重试等逻辑。

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-ribbon</artifactId>

 

@LoadBalanced

public RestTemplate restTemplate() {

return new RestTemplate();

}

// 通过url找服务

restTemplate.getForObject("http://xxx/" + id, User.class); 

 

   eureka:

 client:

   serviceUrl:

     defaultZone: http://10.88.102.203:8761/eureka/

 instance:

   preferIpAddress: true

 

4、消费服务 方式二 Feign

整合ribbon,简化代码,适合于一次性调用很多个服务的时候

<groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-feign</artifactId>

 

@EnableFeignClients

@EnableDiscoveryClient

 

@FeignClient(name = "microservice-provider-user")

public interface UserFeignClient {

 @RequestMapping("/{id}")

 // 通过函数名来找服务

 public User findByIdFeign(@RequestParam("id") Long id); 

}

 

ribbon:

 eureka:

   enabled: true   

 

5、Hystrix 熔断器 隔离故障的范围

<groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-hystrix</artifactId>

 

    @EnableCircuitBreaker

    1》ribbon:

    // 注意fallback的函数签名要和调用函数本身一致

    @HystrixCommand(fallbackMethod = "fallback")

    2》forein: 直接传递第二个参数(函数的重载)即可

    @FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)

 

    6、对熔断器的监控 Hystrix Dashboard

    <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

 

      @EnableHystrixDashboard

 

    7、同时收集多个服务的监控数据 Turbine

    <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-turbine</artifactId>

    </dependency>

    <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-netflix-turbine</artifactId>

    </dependency>

 

    @EnableTurbine

 

    security.basic.enabled: false

turbine:

 aggregator:

   clusterConfig: default

 appConfig: serviceid1, serviceid2 

 clusterNameExpression: new String("default")

 

8、统一配置

<groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-config-server</artifactId>

 

    @EnableConfigServer

 

    cloud:

    config:

      server:

        git:

          uri: https://github.com/xxx

          search-paths: dir

          username:        

          password:

 

 

    使用方:

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-config</artifactId>

 

    @RefreshScope

@Value("${profile}")

private String profile;

 

bootstrap.yml(数据加载顺序:bootstrap config application,如果在application中,实现不了占位符的替换)

  cloud:

   config:

     uri: http://config-server:8040/

     profile: dev                      

     label: master       

 

 

使用方刷新:curl  -X POST http://localhost:8041/refresh

9、网关 Zuul提供了动态路由、监控、回退、安全等功能。

<groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-zuul</artifactId>

 

    @EnableZuulProxy

 

    zuul:

 ignored-services: xxxx

 routes:

   movie:                                                            

     path: /movie/**

     service-id: yyyy

 

三、常用命令

java -jar target/**.jar --spring.profiles.active=peer1 > peer1.log 2>&1 &

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值