这个测试工程是使用订单微服务和支付微服务的maven多模块的项目。
一、搭建父工程
这里我们使用IDEA创建简单的maven项目就可以,然后再pom文件中增加如下依赖:
<?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>org.example</groupId>
<artifactId>cloud-alibaba-nacos-test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<alibaba-cloud.version>2.2.5.RELEASE</alibaba-cloud.version>
<springcloud.version>Hoxton.SR8</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
具体springboot、springcloud和alibaba版本对应的关系可以查看这个网站:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
二、公共工程
这个模块中存放我们其他模块都会用到的类、实体等。
pom文件内容如下:
<?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">
<parent>
<artifactId>cloud-alibaba-nacos-test</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-nacos-common</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip> <!-- 防止编译出错 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
在这里面我们创建两个类,一个是支付的实体类和返回数据的实体类。
@Data
public class PayEntity {
private Long id;
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultEntity<T> {
private Integer code;
private String msg;
private T data;
}
三、支付工程
因为nacos
相当于注册中心,所以我们创建一个支付工程注册到nacos
。
首先创建一个新的模块:cloud-nacos-pay
<?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">
<parent>
<artifactId>cloud-alibaba-nacos-test</artifactId>
<groupId>org.example</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-nacos-pay</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- nacos依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 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>
<!-- 我们创建的公共模块 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-nacos-common</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
创建之后我们增加一个启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // nacos客户端注解
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class);
}
}
在配置文件中增加nacos
的地址和服务名称
server:
port: 9999
spring:
application:
name: pay-service
cloud:
nacos:
discovery:
server-addr: IP:8848
接着创建一个简单的接口用于测试(不使用数据库,使用静态数据替代数据库查询):
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@RequestMapping("/pay")
public class PayController {
public static HashMap<Integer, PayEntity> staticVar = new HashMap<>(5);
static {
staticVar.put(1, new PayEntity(1, "第一序列", 10.45));
staticVar.put(2, new PayEntity(2, "第二序列", 34.45));
staticVar.put(3, new PayEntity(3, "第三序列", 65.35));
staticVar.put(4, new PayEntity(4, "第四序列", 10.41));
staticVar.put(5, new PayEntity(5, "第五序列", 49.78));
}
@GetMapping("/get")
public ResultEntity<PayEntity> executePay(@RequestParam("id") Integer payId) {
return new ResultEntity<>(200, "成功", staticVar.get(payId));
}
}
这个是时候启动支付这个工程,查看一个是否注册到nacos中。
最后我们看一个nacos的控制台:
这说明服务注册成功了。
这里我们也可以测试一个服务的集群设置,我们只需要在配置文件上设置,然后在IDEA启动中设置一个启动参数:
server:
port: ${port:9001} # 启动的时候设置 -Dport=9002 的时候会用9002端口启动,否则默认时候9001端口
在点击服务的操作列,点击详情我们可以看到我们的多实例的服务。
四、订单工程
我们这里会用订单工程去调用支付工程的接口,我们现在看一下需要引入什么依赖。
<?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">
<parent>
<artifactId>cloud-alibaba-nacos-test</artifactId>
<groupId>org.example</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-nacos-order</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-nacos-common</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
服务之间的调用我们可以使用Ribbon
、openfeign
两种,但是在nacos-discovery
自带了Ribbon
。
接着还是和原来一样增加启动类和配置文件,基本上和支付工程一样:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
}
配置文件:
server:
port: ${port:9101}
spring:
application:
name: order-service
cloud:
nacos:
discovery:
server-addr: 192.168.182.131:8848
接着看一个我们如何通过Ribbon调用支付工程的接口。
先添加一个RestTemplate
配置类:
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
接着在创建一个控制器接口,用于调用支付服务接口:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/order")
public class OrderController {
// 服务名称
public static final String SERVER_NAME = "http://pay-service";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/get/{id}")
public ResultEntity<PayEntity> consumer(@PathVariable("id") Integer id) {
ResultEntity<PayEntity> resultEntity = restTemplate.getForObject(SERVER_NAME + "/pay/get?id=" + id, ResultEntity.class, id);
return resultEntity;
}
}
使用Ribbon
自动负载均衡。
五、使用openfeign调用
OpenFeign
是一个声明式的web服务客户端
,让编写web
服务客户端变的非常容易,只需要创建一个接口并在接口上添加注解即可,openFeign
的前身是Feign
,后者目前已经停更了,openFeign
是SpringCloud
在Feign
的基础上支持了Spring MVC
的注解,并通过动态代理的方式产生实现类来做负载均衡并进行调用其他服务。
先引入依赖,并在启动类上增加一个注解:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
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 // 增加注解
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
}
接着创建一个service
接口:
import org.example.entity.PayEntity;
import org.example.entity.ResultEntity;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "pay-service")
public interface PayService {
// 将支付工程中的控制器方法当作接口就可以了
@GetMapping("/pay/get")
ResultEntity<PayEntity> executePay(@RequestParam("id") Integer payId);
}
最后在控制器中调用:
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private PayService payService;
@GetMapping("/get/{id}")
public ResultEntity<PayEntity> consumer(@PathVariable("id") Integer id) {
return payService.executePay(id);
}
}