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

项目下载地址
sentinel JAR下载地址

如果喜欢欢迎加入交流

介绍 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>

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

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

&lt;!--sentinel--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-alibaba-sentinel&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;version&gt;1.18.2&lt;/version&gt;
    &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;

</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 + "生产者";
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

package com.hxx.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日 13:22:00
    */

@SpringBootApplication
@EnableDiscoveryClient
public class ProvderApp {

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

这里加入了远程调用(Fegin客户端 测试)
package com.hxx.alibaba.consum;

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);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
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 {由于你的访问次数太多,已为你限流、您已进入保护模式,请稍后再试!}&gt;&gt;&gt;熔断处理函数";
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

配置中心
在这里插入图片描述

启动看效果即可

接着使用路由网管

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&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    String token = exchange.getRequest().getQueryParams().getFirst("token");
    if (token == null || token.isEmpty()) {
        ServerHttpResponse response = exchange.getResponse();
        Map&lt;Object, Object&gt; 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
#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中不要在 路由中加 -

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

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

 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

启动访问就可以了
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

  
   
   
  • 1
  • 2
  • 3

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

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

猜你喜欢

转载自blog.csdn.net/penggerhe/article/details/108745479