SpringCloud Alibaba完整使用

大家好,又见面了,我是你们的朋友全君。

今天我要分享一套精彩的springCloudAlibaba视频教程,以及如何使用docker搭建各种环境的资料。这里有一个必学的Docker容器技术和牛逼的SpringCloudAlibaba学习视频的下载链接:点击下载白嫖版(访问密码:2822)。

首先,我们需要在linux环境下搭建几个关键组件:

  1. Nacos注册中心 – 文档和jar包。
  2. sentinel – 用于流量控制和断路,提供文档和JAR下载地址。
  3. apache skywalking APM – 用于链路跟踪。
  4. rocketmq – 使用项目下载地址。

接下来,让我们详细介绍一下Nacos命名空间的使用:

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

创建带有分组的配置、通过配置设置使用的那个分组、然后访问测试显示dev

SpringCloud Alibaba完整使用

项目如下:

首先创建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></relativePath>     </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>         <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>             <scope>true</scope>         </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 + "生产者";     } }
package com.hxx.alibaba; <p>import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;</p><p>/**</p><ul><li>启动类</li><li>使用说明:生产者生产东西</li><li>@author huangxiangxiang</li><li>@version 2.0.0</li><li>@createTime 2019年09月10日 13:22:00 */ @SpringBootApplication @EnableDiscoveryClient public class ProvderApp { public static void main(String[] args) { SpringApplication.run(ProvderApp.class, args); } }

将本地的配置放到配置中心后,启动项目即可拉取配置,以yml的方式,默认是properties。

SpringCloud Alibaba完整使用

创建消费者工程 Alibaba-Cloud-Member package com.hxx.alibaba.controller;</li></ul><p>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;</p><p>/**</p><ul><li><p>使用说明:</p></li><li><p>@author huangxiangxiang</p></li><li><p>@version 2.0.0</p></li><li><p>@createTime 2019年09月10日 13:32:00 */ @RestController @RequestMapping("/aonsum") public class ConsumController { @Autowired private ConsumService consumService;</p><p>@RequestMapping("/getNmae") public String getNmae(@RequestParam("name") String name) { String nmaes = consumService.getNmae(name); System.out.println(".......消费者fegin调用.....配置中心................." + nmaes); return nmaes; } }
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;</p></li></ul><p>/**</p><ul><li>使用说明:远程调用fegin --- 加入fallback 熔断器  解决连锁反应 被调用服务挂的的时候 和服务崩溃的情况</li><li>@author huangxiangxiang</li><li>@version 2.0.0</li><li>@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;</li></ul><p>import com.hxx.alibaba.consum.ConsumService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;</p><p>/**</p><ul><li><p>使用说明:实现限流的异常处理</p></li><li><p>@author huangxiangxiang</p></li><li><p>@version 2.0.0</p></li><li><p>@createTime 2019年08月22日 18:07:00 */ @Component public class SentinelExceptionHandler implements ConsumService { final static Logger logger = LoggerFactory.getLogger(SentinelExceptionHandler.class);</p><p>@Override public String getNmae(String name) { logger.info("sentinel 熔断处理 {}", "SentinelExceptionHandler"); return "Sentinel {由于你的访问次数太多,已为你限流、您已进入保护模式,请稍后再试!}>>>熔断处理函数"; } }
bootstrap.properties 配置文件</p><h1>端口已经配置到 -NACOS 注册中心</h1><p>spring.application.name=alibaba-cloud-member</p><h1>配置文件的地址</h1><p>spring.cloud.nacos.config.server-addr=192.168.220.129:8848</p><h1>注册中心的地址</h1><h1>spring.cloud.nacos.discovery.server-addr=192.168.220.128:8848</h1><h1>限流监控中心</h1><h1>spring.cloud.sentinel.transport.dashboard=192.168.220.128:8080</h1><h1>spring.cloud.sentinel.eager=true</h1><p>spring.cloud.nacos.config.file-extension= yaml

配置中心:

SpringCloud Alibaba完整使用

启动后即可查看效果。

接下来,我们使用路由网管:

Alibaba-Gateway-Api 工程:

package alibaba.fielt;</p></li></ul><p>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;</p><p>/**</p><ul><li><p>使用说明: 鉴权过滤器</p></li><li><p>@author huangxiangxiang</p></li><li><p>@version 2.0.0</p></li><li><p>@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); }</p><p>//设置过滤器的执行顺序 @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } }</p></li></ul><p>package alibaba;</p><p>import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;</p><p>/**</p><ul><li>使用说明: 路由网管启动类</li><li>@author huangxiangxiang</li><li>@version 2.0.0</li><li>@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 注册中心<h1>--这个要和 Nacos 的 Data ID 前缀一致</h1><p>spring.application.name=alibaba-cloud-gateway</p><h1>2、配置文件的地址</h1><p>spring.cloud.nacos.config.server-addr=192.168.220.129:8848</p><h1>3、注册中心的地址</h1><p>spring.cloud.nacos.discovery.server-addr=192.168.220.129:8848</p><h1>4、限流监控中心</h1><p>spring.cloud.sentinel.transport.dashboard=192.168.220.129:8080 spring.cloud.sentinel.eager=true</p><h1>5、配置以yaml的形式----不配置就拉取不到</h1><p>spring.cloud.nacos.config.file-extension=yaml</p><h1>路由的网关  -id -uri 去掉-  在yml中不要在 路由中加 -

配置中心太长导致无法截图,所以复制如下:

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

启动后访问localhost/路由/路径即可。

搭建elasticsearch(ES),安装成功后访问IP + 9200出现JSON即为成功。

Apache SkyWalking APM比较复杂,先下载该组件:

修改application.yml配置文件:

SpringCloud Alibaba完整使用

创建一个文件夹,将下载的探针agent单独拷贝一份放到创建的文件夹下:

SpringCloud Alibaba完整使用

然后修改工程启动的VM参数:

SpringCloud Alibaba完整使用

注意:这里有几个容易出错的地方,第一行是刚才创建的agent文件夹位置要对应,第二行要与配置生产和消费的服务名称spring.application.name一致,不然出不来效果,第三行是Linux里的IP + 端口号。

-javaagent:D:hxxcloud02Alibaba-CloudAlibaba-cloud-external-skywalkingagentskywalking-agent.jar -Dskywalking.agent.service_name=alibaba-cloud-prod-provder -Dskywalking.collector.backend_service=192.168.220.129:8080

配置完成后,启动后出现日志即为成功,可以查看调用接口的效果。

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

SpringCloud Alibaba完整使用

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

发布者:全栈程序员栈长,转载请注明出处:https://www.php.cn/link/ec23187ecc4e0c6eb40cd187db4a865c 原文链接:https://www.php.cn/link/c8377ad2a50fb65de28b11cfc628d75c

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享