SpringCloud Alibaba actual combat, build the first SpringCloud Alibaba project

Project download address
sentinel JAR download address

If you like, welcome to join the exchange

Introduce the use of NACOS namespace
Insert picture description here
|Insert picture description here

Insert picture description here

Create a configuration with a group, the group used by the configuration settings, and then access the test to display dev
Insert picture description here

The items are as follows
在这里插入代码片Insert picture description here

     首先创建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>|

Project name​Alibaba-Cloud-Provder

​​package com.hxx.alibaba.controller;

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

/**

  • Instructions for use: After using the yaml configuration center, my request has not been able to access the resource is 404. If this happens, add a @RequestMapping("/alibaba") to the Controller class
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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;

/**
Startup class

  • Instructions for use: Producers produce things
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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 configuration file

#1, the port has been configured to -NACOS registration center
#--this must be the same as the Nacos Data ID prefix spring.application.name=alibaba-cloud-provder
#2, the address of the configuration file
spring.cloud.nacos.config.server- addr=192.168.220.129:8848

#3, the address of the registration center
#spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

#4, current limit monitoring center
#spring.cloud.sentinel.transport.dashboard=192.168.220.128:8080
#spring.cloud.sentinel.eager=true

#5. The configuration is in the form of yaml
-spring.cloud.nacos.config.file-extension=yaml cannot be pulled without configuration

This is to put the local configuration in the configuration center, and start the project to pull the configuration, in the way of yml, the default is properties
Insert picture description here

创建消费者工程 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;

/**

  • Instructions for use:
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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

Here added remote call (Fegin client test)
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;

/**

  • Instructions for use: remote call fegin-add fallback fuse to solve the chain reaction when the called service hangs and the service crashes
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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;

/**

  • Instructions for use: to achieve current limiting exception handling
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime August 22, 2019 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 配置文件

#The port has been configured to the -NACOS registration center
spring.application.name=alibaba-cloud-member


#Configuration file address spring.cloud.nacos.config.server-addr=192.168.220.129:8848 #Registration
center address
#spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848

Current Limiting Monitoring Center

#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

Configuration Center
Insert picture description here

Start to see the effect

Then use routing network management

Alibaba-Gateway-Api project

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;

/**

  • Instructions for use: Authentication filter
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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;

/**

  • Instructions for use: Route network management startup class
  • @author huangxiangxiang
  • @version 2.0.0
  • @createTime September 10, 2019 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, the address of the registration center
spring.cloud.nacos.discovery.server-addr=192.168.220.129:8848

#4, current limit monitoring center
spring.cloud.sentinel.transport.dashboard=192.168.220.129:8080
spring.cloud.sentinel.eager=true

#5. The configuration is in the form of yaml
-spring.cloud.nacos.config.file-extension=yaml cannot be pulled without configuration

#Route's gateway-id -uri remove-do not add-in yml-

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

The configuration center is too long to take a screenshot, so I copied it

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

Just start access
localhost/route/path

Set up one
to install es,
install successfully, access ip + 9200, and json appears even if it is successful

apache-skywalking-apm-bin This is more complicated, first download this

Modify the application.yml configuration file
Insert picture description here

The above configuration is good-brother, you are 80% complete.
Start link tracking configuration, please look carefully

Create a folder
Insert picture description here

Take a separate test of the probe agent just downloaded and put it in the created folder
Insert picture description here

Then modify the vm parameters of this project startup

Insert picture description here

Note: I’ve
been trapped here for a long time. The first line is the folder location of the agent you just created. The
second line should be the same as the service name spring.applition.name = you configured for production and consumption, otherwise the effect will not come out.
The third line is ip +post (port number) in Linux

-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

The configuration is complete and the log appears after the startup is successful. You can see the effect of calling the interface.
Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

There is also an MQ update before it is free

Guess you like

Origin blog.csdn.net/penggerhe/article/details/108745479