Spring Cloud Alibaba版本选型
版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
由于Spring Boot 2.4+和以下版本之间变化较大,目前企业级客户老项目相关Spring Boot版本仍停留在Spring Boot 2.4以下,为了同时满足存量用户和新用户不同需求,社区以Spring Boot 2.4为分界线,同时维护2.2.x和2021.x两个分支迭代。
适配Spring Boot 2.4, Spring Cloud 2021.x版本及以上的Spring Cloud Alibaba版本如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba版本命名方式进行了调整, 未来将对应Spring Cloud版本, 前三位为Spring Cloud版本,最后一位为扩展版本,比如适配Spring Cloud 2021.0.1版本对应的Spring Cloud Alibaba第一个版本为:2021.0.1.0,第个二版本为:2021.0.1.1,依此类推)
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|
2021.0.4.0* | Spring Cloud 2021.0.4 | 2.6.11 | 2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 | 2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
父pom配置
这里使用Spring Cloud 2021.0.4、Spring Cloud Alibaba 2021.0.4.0。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/>
</parent>
<groupId>com.morris.nacos</groupId>
<artifactId>nacos-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>nacos-demo</name>
<description>nacos-demo</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
</properties>
<modules>
<module>user-service</module>
<module>order-service</module>
</modules>
<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>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务提供者order-service
服务提供者可以通过Nacos的服务注册发现功能将其服务注册到Nacos Server上。
引入依赖
order-service项目的pom.xml中引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
配置nacos注册中心
server:
port: 8020
spring:
application:
name: order-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
更多配置参考:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery
启动order-service,查看是否注册成功
启动order-service,可以前往nacos管理端界面查看是否成功注册。
管理后台地址:http://localhost:8848/nacos/index.html#/serviceManagement?dataId=&group=&appName=&namespace=
也可以通过Open API查询实例列表:http://localhost:8848/nacos/v1/ns/instance/list?serviceName=order-service
{
"hosts": [
{
"ip": "127.0.0.1",
"port": 8020,
"valid": true,
"healthy": true,
"marked": false,
"instanceId": "127.0.0.1#8020#DEFAULT#DEFAULT_GROUP@@order-service",
"metadata": {
"preserved.register.source": "SPRING_CLOUD"
},
"enabled": true,
"weight": 1,
"clusterName": "DEFAULT",
"serviceName": "order-service",
"ephemeral": true
}
],
"dom": "order-service",
"name": "DEFAULT_GROUP@@order-service",
"cacheMillis": 3000,
"lastRefTime": 1666168905613,
"checksum": "b943a7ec55bd85740b4d5055382261eb",
"useSpecifiedURL": false,
"clusters": "",
"env": "",
"metadata": {}
}
服务消费者user-service
服务消费者可以通过Nacos的服务注册发现功能从Nacos server上获取到它要调用的服务。
配置nacos注册中心
server:
port: 8030
spring:
application:
name: user-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
使用RestTemplate进行http调用
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
加入配置:
@Configuration
public class RestConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
这里只是简单的http调用,没有使用到注册中心:
@GetMapping("findOrderByUserId")
public List<Order> findOrderByUserId(Long userId) {
Order[] orders = restTemplate.getForObject("http://127.0.0.1:8020/order/findOrderByUserId?userId=", Order[].class, userId);
return Arrays.asList(orders);
}
使用RestTemplate进行服务调用
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
新版本SpringCloud推荐使用loadbalancer来实现负载均衡,已经不再包含ribbon依赖了。
加入配置:
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate2() {
return new RestTemplate();
}
}
调用:
@GetMapping("findOrderByUserId2")
public List<Order> findOrderByUserId2(Long userId) {
Order[] orders = restTemplate2.getForObject("http://order-service/order/findOrderByUserId?userId=", Order[].class, userId);
return Arrays.asList(orders);
}
使用WebClient进行HTTP调用
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
加入配置:
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
调用:
@GetMapping("findOrderByUserId3")
public Mono<List<Order>> findOrderByUserId3(Long userId) {
return webClientBuilder.build().get().uri("http://127.0.0.1:8020/order/findOrderByUserId?userId=" + userId)
.retrieve().bodyToMono(Order[].class).map(t -> Arrays.asList(t));
}
使用WebClient进行服务调用
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
加入配置:
@Bean
@LoadBalanced
WebClient.Builder webClientBuilder2() {
return WebClient.builder();
}
调用:
@GetMapping("findOrderByUserId4")
public Mono<List<Order>> findOrderByUserId4(Long userId) {
return webClientBuilder2.build().get().uri("http://order-service/order/findOrderByUserId?userId=" + userId)
.retrieve().bodyToMono(Order[].class).map(t -> Arrays.asList(t));
}
使用Feign进行服务调用
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
开启@EnableFeignClients。
@EnableFeignClients
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
加入Client配置:
package com.morris.user.client;
import com.morris.user.entity.Order;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(value = "order-service", path = "/order")
public interface OrderClient {
@GetMapping("findOrderByUserId")
List<Order> findOrderByUserId(@RequestParam("userId") Long userId);
}
调用:
@GetMapping("findOrderByUserId5")
public List<Order> findOrderByUserId5(Long userId) {
return orderClient.findOrderByUserId(userId);
}
|