GithubHelp home page GithubHelp logo

springcloud-alibaba-demo's Introduction

springcloud-alibaba

官网地址:

springcloud-alibaba 和springcloud

功能 springcloud(netfilix) springcloud-alibaba dubbo
服务注册、发现 eureka(18年停更) nacos
负载均衡 ribbon、fegin ribbon
熔断、降级 hystrix Sentinel
路由 zuul getway
链路追踪 Sleuth
分布式事务 seata
分布式配置中心 SpringCloudConfig nacos

一、服务注册、发现 nacos

image-20201127135407557

image-20201127135531462

  • 下载完毕,解压到相应的目录

image-20201127135704188

启动nacos服务端

  • 进入到nacos的bin目录下

image-20201127135831629

  • 启动单机nacos服务startup.cmd -m standalone
  • 单机模式(standalone)和集群模式(cluster

image-20201127140000901

  • 启动完毕验证服务localhost:8848/nacos 默认账号密码都是nacos

image-20201127140113744

应用注册到nacos中

  1. 添加依赖
           <dependency>
               <groupId>com.alibaba.cloud</groupId>
               <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
           </dependency>
  1. 配置 在启动类上添加@EnableDiscoverClient
/**
 * @author liouwb
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  1. application.yml配置
   server:
     port: 8804 # 端口号
   
   spring:
     application:
       name: provider-server # 显示在nacos的服务名称
   
     cloud:
       nacos:
         discovery:
           server-addr: localhost:8848  # nacos地址
  1. 启动服务就会注册到nacos

image-20201127140548128

负载均衡 ribbon

  • 无需引用jar,nacos依赖的有ribbon

    1. 配置ribbon负载均衡的策略
    /**
     * @author liouwb
     */
    @Configuration
    public class RibbonRuleConfig {
    
        /**
         * 自定义负载均衡算法
         * ribbon负载均衡配置类IRule
         * RoundRobinRule 默认的 轮询
         * RandomRule随机
         * WeightedResponseTimeRule 权重
         */
        @Bean
        public IRule myRule() {
            return new RandomRule();
        }
    }
  1. 配置restTemplate
     /**
      * @author liouwb
      */
     @Configuration
     public class RestTemplateConfig {
     
         @Bean
         public RestTemplate restTemplate() {
     
             return new RestTemplate();
         }
     }
  1. webclient配置
   /**
    * @author liouwb
    */
   @Configuration
   public class WebClientConfig {
   
       @Bean
       @LoadBalanced
       public WebClient.Builder webClientBuilder() {
   
           return WebClient.builder();
       }
   }
  1. 通过client-server 调用provider-server
/**
 * 调用provider服务 restTemplate
 *
 * @return
 */
@GetMapping(value = "restProvider")
public String restProvider() {
    //Access through the combination of LoadBalanceClient and RestTemplate
    // 发现服务
    ServiceInstance serviceInstance = loadBalancerClient.choose("provider-server");
    // 格式化请求路径
    String path = String.format("http://%s:%s/provider/get", serviceInstance.getHost(), serviceInstance.getPort());

    System.out.println("请求路径:" + path);

    return restTemplate.getForObject(path, String.class);
}


/**
 * 调用provider服务 webClientProvider
 *
 * @return
 */
@GetMapping(value = "webClientProvider")
public String webClientProvider() {

    return clientBuilder
            .baseUrl("http://provider-server/provider/get")
            .build()
            .method(HttpMethod.GET)
            .retrieve()//请求结果的方法
            .bodyToMono(String.class)
            .block();
}

image-20201127141419400

熔断、降级 Sentinel

image-20201127141809262

image-20201127142424580

sentinel服务端安装和启动

image-20201127142931604

  • 下载完毕之后,到sentinel目录下启动sentinel`
java -Dserver.port=8805 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.0.jar
  • -Dserver.port=8805是sentinel服务端访问的端口

image-20201127143645520

  • 启动之后sentinel 登录界面localhost:8805 默认的用户名和密码都是sentinel

image-20201127143925854

  • 登录之后

image-20201127144028631

provider应用服务集成sentinel

  1. 添加依赖
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
  1. 配置application.yml
   cloud:
     nacos:
       discovery:
         server-addr: localhost:8848
     sentinel:
       transport:
         dashboard: localhost:8805
  1. 在接口上使用@SentinelResourc value 原方法 blockHandler 异常处理 fallback 降级处理
    @GetMapping(value = "get")
    @SentinelResource(value = "get", blockHandler = "exceptionHandler", fallback = "fallbackFun")// value 原方法 blockHandler  异常处理 fallback 降级处理
    public String get() {

        return "这是一个provider服务,服务端口:" + serverPort;
    }

    public String fallbackFun() {

        return "@sentinel:" + serverPort;
    }

    public String exceptionHandler(){

        return "@sentinel exceptionHandler:" + serverPort;
    }

image-20201127151649582

  • @SentinelResource` 注解用来标识资源是否被限流、降级

image-20201127145349021

  • 限流设置

image-20201127145649039

  • 添加

image-20201127145733846

  • 添加完限流规则就会出现在流控规则列表

image-20201127145828700

  • 然后在快速访问接口

image-20201127151304386

  • 实时监控流量

image-20201127151234150

nacos配置中心

  1. 添加依赖
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  1. 在nacos中心添加配置文件

image-20201127173828375

  1. 编写文件内容,并发布

image-20201127173931818

  1. 查看配置文件列表信息

image-20201127174021439

  1. 编写项目配置,从远程nacos获取配置文件 bootstrap.yml文件
server:
  port: 8802

spring:
  application:
    name: client-server
  profiles:
    active: dev

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        # nacos地址
        server-addr: localhost:8848
        # 配置文件后缀名为yaml
        # 指定远程nacos 配置文件名称 这里会自动加载provider-server-dev.yml
        name: provider-server
        file-extension: yml
  1. 启动程序,验证配置是否生效

image-20201127174307763

  • 自定义nacos配置中心命令空间

image-20201127175635367

  • 新增命令空间

image-20201127175748963

  • 新增配置

image-20201127175929684

  • 代码测试

image-20201127180326352

springcloud-alibaba-demo's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.