SpringCloud Alibaba 实战,搭建第一个SpringCloud Alibaba项目

分享一套牛逼SpringCloudAlibaba视频


源代码下载地址

介绍 NACOS 命名空间的使用
在这里插入图片描述
| 在这里插入图片描述

在这里插入图片描述

创建带有分组的配置、通过配置设置使用的那个分组、然后访问测试显示dev
在这里插入图片描述

项目如下

在这里插入图片描述

首先创建pom工程 Alibaba-Cloud

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.hxx.alibaba</groupId>
<artifactId>Alibaba-Cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>Alibaba-Cloud-Member</module>
    <module>Alibaba-Cloud-Provder</module>
    <module>Alibaba-Gateway-Api</module>
</modules>


<packaging>pom</packaging>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>0.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>


    <!--引入配置中心阿里巴巴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

    <!--引入注册中心阿里巴巴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!--sentinel-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.2</version>
        <optional>true</optional>
    </dependency>
</dependencies> </project>

工程名称 ​Alibaba-Cloud-Provder

package com.hxx.alibaba.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用说明: 使用了 yaml配置中心后我的请求一直访问不到资源是404  出现这个情况的话 在Controller类在加一个@RequestMapping("/alibaba")
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 13:25:00
 */

@RestController
@RequestMapping("/alibaba")
public class ProvderController {


    @RequestMapping("/getNmae")
    public String getNmaeList(String name) {
        System.out.println("生产者");
        return name + "生产者";
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
    启动类
 * 使用说明:生产者生产东西
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 13:22:00
 */

@SpringBootApplication
@EnableDiscoveryClient
public class ProvderApp {

    public static void main(String[] args) {
        SpringApplication.run(ProvderApp.class, args);
    }
}

bootstrap.properties 配置文件

#1、端口已经配置到 -NACOS 注册中心
#–这个要和 Nacos 的 Data ID 前缀一致 spring.application.name=alibaba-cloud-provder
#2、配置文件的地址
spring.cloud.nacos.config.server-addr=192.168.220.129:8848

#3、注册中心的地址
#spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

#4、限流监控中心
#spring.cloud.sentinel.transport.dashboard=192.168.220.128:8080
#spring.cloud.sentinel.eager=true

#5、配置以yaml的形式----不配置就拉取不到
spring.cloud.nacos.config.file-extension=yaml

这是把本地的配置 放到了配置中心 、启动项目就可以拉取到配置、以yml的方式、默认是 properties
在这里插入图片描述

## 创建消费者工程 Alibaba-Cloud-Member
package com.hxx.alibaba.controller;

import com.hxx.alibaba.consum.ConsumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用说明:
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 13:32:00
 */

@RestController
@RequestMapping("/aonsum")
public class ConsumController {

    @Autowired
    private ConsumService consumService;

    @RequestMapping("/getNmae")
    public String getNmae(@RequestParam("name") String name) {
        String nmaes = consumService.getNmae(name);
        System.out.println(".......消费者fegin调用.....配置中心................." + nmaes);
        return nmaes;
    }
}

这里加入了远程调用(Fegin客户端 测试)

import com.hxx.alibaba.exceptionhandler.SentinelExceptionHandler;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 使用说明:远程调用fegin --- 加入fallback 熔断器  解决连锁反应 被调用服务挂的的时候 和服务崩溃的情况
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 13:30:00
 */

@FeignClient(value = "alibaba-cloud-prod-provder",fallback = SentinelExceptionHandler.class)
public interface ConsumService {

    @RequestMapping("/alibaba/getNmae")
    public String getNmae(@RequestParam("name")  String name);

}

package com.hxx.alibaba.exceptionhandler;

import com.hxx.alibaba.consum.ConsumService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 使用说明:实现限流的异常处理
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年08月22日 18:07:00
 */

@Component
public class SentinelExceptionHandler implements ConsumService {
    final static Logger logger = LoggerFactory.getLogger(SentinelExceptionHandler.class);
    
    @Override
    public String getNmae(String name) {
        logger.info("sentinel 熔断处理 {}", "SentinelExceptionHandler");
        return "Sentinel {由于你的访问次数太多,已为你限流、您已进入保护模式,请稍后再试!}>>>熔断处理函数";
    }
}

bootstrap.properties 配置文件

#端口已经配置到 -NACOS 注册中心
spring.application.name=alibaba-cloud-member

#配置文件的地址
spring.cloud.nacos.config.server-addr=192.168.220.129:8848
#注册中心的地址
#spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

# 限流监控中心
#spring.cloud.sentinel.transport.dashboard=192.168.220.128:8080
#spring.cloud.sentinel.eager=true
spring.cloud.nacos.config.file-extension= yaml

配置中心

在这里插入图片描述
启动看效果即可

接着使用路由网管

Alibaba-Gateway-Api 工程

package alibaba.fielt;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * 使用说明: 鉴权过滤器
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 15:55:00
 */

@Component
public class AuthFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if (token == null || token.isEmpty()) {
            ServerHttpResponse response = exchange.getResponse();
            Map<Object, Object> map = Maps.newHashMap();
            map.put("code", 401);
            map.put("message", "非法请求!");
            map.put("cause", "Token not is null");

            ObjectMapper mapper = new ObjectMapper();
            try {
                byte[] bytes = mapper.writeValueAsBytes(map);
                // 输出错误信息到页面
                DataBuffer buffer = response.bufferFactory().wrap(bytes);
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
                return response.writeWith(Mono.just(buffer));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }


        }

        return chain.filter(exchange);
    }


    //设置过滤器的执行顺序
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}





package alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 使用说明: 路由网管启动类
 *
 * @author huangxiangxiang
 * @version 2.0.0
 * @createTime 2019年09月10日 14:27:00
 */

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

配置文件

#1、端口已经配置到 -NACOS 注册中心
#--这个要和 Nacos 的 Data ID 前缀一致
spring.application.name=alibaba-cloud-gateway
#2、配置文件的地址
spring.cloud.nacos.config.server-addr=192.168.220.129:8848

#3、注册中心的地址
spring.cloud.nacos.discovery.server-addr=192.168.220.129:8848

#4、限流监控中心
spring.cloud.sentinel.transport.dashboard=192.168.220.129:8080
spring.cloud.sentinel.eager=true

#5、配置以yaml的形式----不配置就拉取不到
spring.cloud.nacos.config.file-extension=yaml

#路由的网关  -id -uri 去掉-  在yml中不要在 路由中加 -

配置中心 太长导致无法截图了 所以复制了一下

server:
    port: 9000 spring:
    cloud:
        gateway:
            discovery:
                locator:
                    enabled: true
            routes:
                id: ALIBABA-CLOUD-MEMBER
                uri: lb://alibaba-cloud-member
                predicates:
                  Method=GET,POST
                id: alibaba-cloud-provder
                uri: lb://alibaba-cloud-provder
                predicates:
                  Method=GET,POST logging:   level:
    org.springframework.cloud.gateway: debug

启动访问就可以了
localhost/路由/路径

搭建一个
要安装es
安装成功 访问 ip + 9200出现json 即便成功

apache-skywalking-apm-bin 这个比较复杂 先下载这个

修改application.yml 配置文件
在这里插入图片描述

上面配置好 --兄弟你完成了百分之八十了
开始链路跟踪配置、请仔细看

创建一个文件夹 把
在这里插入图片描述

把刚才下载的探针 agent 单独考出来一份放到创建的文件夹下
在这里插入图片描述

然后修改 这个工程启动的 vm参数

在这里插入图片描述

注意:这边比较坑 坑了我半天、
第一行 是刚才创建的 agent的文件夹位置要对应
第二行 要和你配置生产和消费的服务名称spring.applition.name = 一样 不然出不来效果
第三行 是Linux里面的ip +post(端口号)

-javaagent:D:\hxxcloud02\Alibaba-Cloud\Alibaba-cloud-external-skywalking\agent\skywalking-agent.jar
-Dskywalking.agent.service_name=alibaba-cloud-prod-provder
-Dskywalking.collector.backend_service=192.168.220.129:8080

配置完成 启动 出现日志就是成功了、可以看调用接口的效果了
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

还有一个MQ还没写 有空就更新下

源代码下载地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值