Spring Cloud Alibaba入门示例
- 使用spring cloud alibaba组件搭建的微服务入门级项目,
- 各组件使用的版本:spring boot 2.3.1.RELEASE、spring cloud Hoxton.SR8、nacos 2021.1、sentinel 2.2.5.RELEASE ;
- 整合组件包括:nacos 注册中心_配置中心、openfeign 服务调用、sentinel流控管理-熔断降级等。
- 下载代码
父工程
<?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>
<packaging>pom</packaging>
<modules>
<module>order-service</module>
<module>user-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<groupId>com.snail.learn</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.cloud-version>Hoxton.SR8</spring.cloud-version>
<spring.cloud.starter.alibaba.nacos-version>2021.1</spring.cloud.starter.alibaba.nacos-version>
<spring.cloud.starter.alibaba.sentinel-version>2.2.5.RELEASE</spring.cloud.starter.alibaba.sentinel-version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring.cloud.starter.alibaba.nacos-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring.cloud.starter.alibaba.nacos-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${spring.cloud.starter.alibaba.sentinel-version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
服务注册中心(nacos)
nacos = eureka + config,包括服务注册中心和配置中心的功能。
安装
- 下载nacos(2.0.1) : https://github.com/alibaba/nacos/releases
- 将文件加压到任意目录,进入其bin目录,修改startup.cmd(window环境下)的第26行,改为单服务器启动:set MODE=“standalone”(默认为集群启动),并双击startup.cmd运行
- 启动完成后访问:http://localhost:8848/nacos
服务配置中心(nacos)
安装
参见上面nacos的安装
使用代码
在服务提供者项目基础上修改
<!-- 1.增加依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring.cloud.starter.alibaba.nacos-version}</version>
</dependency>
#使用bootstrap.yml 替换 application.yml文件,配置如下
spring:
application:
name: user-service
cloud:
nacos:
config:
server-addr: localhost:8848 # 配置中心的地址
file-extension: yaml #文件类型
shared-configs: # 通用的配置文件(由于配置服务之间都通用的配置信息),可以配置多个
-
dataId: common.yaml
profiles:
active: dev #当前生效的profile
# 以上对应 nacos配置中的的 “Data Id” 为“user-service-dev.yaml”的配置文件,若没有在对应“user-service.yaml”的配置文件。
服务提供者
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动类
package com.snail.learn.springcloud.alibaba.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 启动服务发现
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class,args);
}
}
配置
spring:
application:
name: user-service
cloud:
nacos:
config:
server-addr: localhost:8848
使用代码
package com.snail.learn.springcloud.alibaba.user.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
@RequestMapping("/hasPermission/{permission}")
public String hasPermission(@PathVariable String permission){
log.error("hasPermission running ...");
return "has "+permission+" permission";
}
}
服务消费者
openfeign + sentinel 实现服务降级
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<!-- 排除 netflix-ribbon -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<!-- 父工程中引入了 sentinel相关的包, 不需要再重复引入-->
启动类
package com.snail.learn.springcloud.alibaba.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 开启Feign客户端支持
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class,args);
}
}
配置
spring:
application:
name: order-service
cloud:
nacos:
config:
server-addr: localhost:8848
server:
port: 8092
feign:
sentinel:
enabled: true # 启用sentinel
使用代码
package com.snail.learn.springcloud.alibaba.order.controller;
import com.snail.learn.springcloud.alibaba.order.service.UserServiceFeignImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
@Resource
private UserServiceFeignImpl userServiceFeignImpl;
@RequestMapping("/create")
public String createOrder(){
return userServiceFeignImpl.hasPermission("createOrder") + " create success";
}
}
package com.snail.learn.springcloud.alibaba.order.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name="user-service" ,path = "/user",fallback = UserServiceFeignFallbackImpl.class)
public interface UserServiceFeignImpl{
@RequestMapping("/hasPermission/{permission}")
public String hasPermission(@PathVariable("permission") String permission);
}
// UserServiceFeignFallbackImpl 类
package com.snail.learn.springcloud.alibaba.order.service.fallback;
import com.snail.learn.springcloud.alibaba.order.service.UserServiceFeignImpl;
import org.springframework.stereotype.Component;
@Component
public class UserServiceFeignFallbackImpl implements UserServiceFeignImpl {
@Override
public String hasPermission(String permission) {
return "fail: UserServiceFeignFallbackImpl ";
}
}
sentinel
Dashboard安装
下载并解压,再用下面的命令启动
java -Dserver.port=8980 -Dcsp.sentinel.dashboard.server=localhost:8980 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
使用代码
在服务提供者项目基础上修改
<!-- 1.增加 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${spring.cloud.starter.alibaba.sentinel-version}</version>
</dependency>
# 2.增加以下配置
spring:
cloud:
sentinel:
transport:
port: 8182 # 与sentinel交互使用的端口,任意未使用的端口即可
dashboard: localhost:8980 #sentinel dashboard地址.