达成目标
本文意在基于SpringCloudAlibaba完成一个基础框架的搭建。主要实现以下功能
- 基于jdk17/Spring Cloud Alibaba(2022.0.0.0)完成框架搭建
- 通过open feign进行微服务间的调用
- nacos注册中心、配置中心
- gateway+nacos配置中心实现动态路由
案例场景
通过spring cloud alibaba完成两个服务(订单服务、用户服务)的服务间调用,并通过网关服务(gateway)进行接口服务的对外暴露。
搭建前知识储备
SpringCloudAlibaba依赖关系
SpringCloudAliabab-->SpringCloud-->SpringBoot
为了保证框架的兼容性,需要关注版本号的匹配。根据上面的依赖关系可以去确认版本号组合是否合理,以尽量避免因为版本号不兼容而引发的问题。
如何确定版本是否匹配?
访问GitHub的SpringCloudAlibaba中的README.md文件可知SpringCloudAlibaba适配的版本号,如下图所示
最终我选定的版本号组合如下:
<spring.cloud.alibaba.version>2022.0.0.0</spring.cloud.alibaba.version>
<spring.cloud.version>2022.0.4</spring.cloud.version>
<spring.boot.version>3.0.10</spring.boot.version>
Maven项目结构说明
|-- demo-framework
|-- demo-gateway
|-- demo-user-service
|-- demo-order-service
|-- feign-api
demo-framework:父级pom,负责管理dependencyMangment以及整体项目的properties(如项目依赖jar的版本号)
demo-gateway:网关服务
demo-user-service:用户服务
demo-order-service:订单服务
feign-api:feign api公共依赖,负责统一管理feign接口
模块:demo-framework
pom源码核心内容
<artifactId>demo-framework</artifactId>
<packaging>pom</packaging>
<modules>
<module>feign-api</module>
<module>demo-gateway</module>
<module>demo-user-service</module>
<module>demo-order-service</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 目前最新版本 jdk要求17-->
<spring.cloud.alibaba.version>2022.0.0.0</spring.cloud.alibaba.version>
<spring.cloud.version>2022.0.4</spring.cloud.version>
<spring.boot.version>3.0.10</spring.boot.version>
<alibaba.fastjson2.version>2.0.39</alibaba.fastjson2.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${alibaba.fastjson2.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
重点:
- 父级pom不要定义build,因为子模块内存在SpringBoot项目和普通jar项目,打包方式不同
模块:feign-api
负责管理各个服务内Feign接口定义
pom.xml
<parent>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>demo-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>feign-api</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
定义一个Feign接口
UserServiceFeign.java
@FeignClient(name = "user-service")
public interface UserServiceFeign {
@GetMapping("/user/{userId}")
String user(@PathVariable("userId") String userId);
}
模块:demo-user-service
特点:
feign服务接口提供方
引入注册中心
pom.xml
<parent>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>demo-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-user-service</artifactId>
<dependencies>
<!-- 项目内feign公共接口-->
<dependency>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>feign-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- nacos服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- spring boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服务状态监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- openfeign远程接口调用依赖的负载均衡 ,openfeign必须添加此依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- openfeign远程接口 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- spring boot打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
UserController.java
@RestController
@RequestMapping
public class UserController implements UserServiceFeign {
@Value("${server.port}")
private String port;
@GetMapping("/{userId}")
@Override
public String user(@PathVariable String userId){
return "张三:"+port;
}
}
application-dev.yml
spring:
cloud:
discovery:
enabled: true
nacos:
discovery:
enabled: true
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
management:
endpoints:
enabled-by-default: false
web:
exposure:
include: ["health","info"]
endpoint:
health:
enabled: true
info:
enabled: true
application.yml
server:
port: 8082
servlet:
context-path: /user
spring:
profiles:
include: dev
application:
name: user-service
模块:demo-order-service
特点:
服务消费方
引入注册中心、配置中心
pom.xml
<parent>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>demo-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-order-service</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- 项目内feign公共接口-->
<dependency>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>feign-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- nacos服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- spring boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服务状态监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- openfeign远程接口调用依赖的负载均衡 ,openfeign必须添加此依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- openfeign远程接口 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- nacos注册中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- nacos注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
OrderController.java
@RestController
@RequestMapping("")
public class OrderController {
@Autowired
private UserServiceFeign userServiceFeign;
@GetMapping("/{orderId}")
public JSONObject user(@PathVariable String orderId){
JSONObject json = new JSONObject();
json.put("user",userServiceFeign.user("001"));
return json;
}
}
bootstrap.yml
spring:
application:
name: order-service
cloud:
nacos:
config:
enabled: true
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
file-extension: yaml
application-dev.yml
spring:
cloud:
loadbalancer:
# nacos:
# enabled: true # 启动nacos负载均衡策略
retry:
enabled: false
openfeign:
client:
refresh-enabled: true
# config:
# user-service:
## 配合配置中心,可以实现动态修改服务地址
# url: http://localhost:8083
discovery:
enabled: true
nacos:
discovery:
enabled: true
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
management:
endpoints:
enabled-by-default: false
web:
exposure:
include: ["health","info"]
endpoint:
health:
enabled: true
info:
enabled: true
application.yml
server:
port: 8081
servlet:
context-path: /order
spring:
profiles:
active: dev
模块:demo-gateway
特点:
负责暴露服务供外部调用
pom.xml
<parent>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>demo-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-gateway</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${alibaba.fastjson2.version}</version>
</dependency>
<!-- 项目内feign公共接口-->
<dependency>
<groupId>spring-cloud-alibaba-demo</groupId>
<artifactId>feign-api</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 服务状态监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- nacos服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos注册中心依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- nacos注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- openfeign远程接口调用依赖的负载均衡 ,openfeign必须添加此依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- openfeign远程接口 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
bootstrap.yml
需要配置配置中心地址
spring:
application:
name: gateway
cloud:
nacos:
config:
enabled: true
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
file-extension: yaml
application-dev.yml
如果启用了配置中心,该内容应配置在nacos配置中心。
spring:
cloud:
discovery:
enabled: true
nacos:
discovery:
enabled: true
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
enabled: false
default-filters:
- AddResponseHeader=X-Demo-Gateway, demo-gateway
routes:
- id: user-route
predicates:
- Path=/user-service/**
filters:
- StripPrefix=1
- PrefixPath=/user
uri: lb://user-service
- id: order-route
predicates:
- Path=/order-service/**
filters:
- StripPrefix=1
- PrefixPath=/order
uri: lb://order-service
openfeign:
client:
config:
user-service:
loggerLevel: full
management:
endpoints:
enabled-by-default: false
web:
exposure:
include: ["health","info","gateway"]
endpoint:
gateway:
enabled: true
health:
enabled: true
info:
enabled: true
logging:
level:
root: info
application.yml
server:
port: 8080
spring:
profiles:
active: dev
验证结果
浏览器访问http://localhost:8080/user-service/1得到正常响应即为搭建成功!
本文暂时没有评论,来添加一个吧(●'◡'●)