环境
- spring cloud Edgware.SR6
- jdk 7
- sts 4.6.0
- mysql 5.7
背景
通过声明式客户端 openfeign 进行支付微服务的访问。
搭建步骤
只需要修改支付服务调用层,增加依赖就可以了。
支付服务调用层
只需要定义接口,不需要进行实现。
package jiangbo.springcloud.dao;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import jiangbo.springcloud.dao.dto.PaymentRequest;
import jiangbo.springcloud.dao.dto.PaymentResponse;
@FeignClient("payment")
public interface PaymentDao {
@PostMapping("/payment")
PaymentResponse payment(PaymentRequest paymentRequest);
}
启动类
package jiangbo.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class JiangBoApplication {
public static void main(String[] args) {
SpringApplication.run(JiangBoApplication.class, args);
}
}
pom.xml
<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>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Edgware.SR6</version>
</parent>
<groupId>jiangbo.springcloud</groupId>
<artifactId>12spring-cloud-order</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
验证
使用 curl 命令访问订单微服务,查看返回,如果返回的订单已支付,则表示通过声明式客户端访问成功。
curl -H "Content-Type: application/json" -X POST --data '{"name":"jiangbo11","amount":"8.99"}', http://localhost:4410/order/payment
{"id":12,"name":"jiangbo11","amount":"8.99","status":"PAID","createTime":1587304461000,"updateTime":1587304462000}
默认也是具有负载均衡的。