Spring cloud:服务调用-声明式客户端访问

2021-02-10 16:19

阅读:464

标签:order   pos   not   tool   datetime   mys   搭建   负载   test   

环境

  1. spring cloud Edgware.SR6
  2. jdk 7
  3. sts 4.6.0
  4. 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

4.0.0org.springframework.cloud
        spring-cloud-starter-parent
        Edgware.SR6jiangbo.springcloud
    12spring-cloud-order
    1.0.0jar1.7org.springframework.cloud
            spring-cloud-starter-openfeign
        org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        org.springframework.boot
            spring-boot-starter-jdbc
        mysql
            mysql-connector-java
            runtimeorg.springframework.boot
            spring-boot-starter-test
            testorg.springframework.boot
            spring-boot-devtools
            providedorg.springframework.boot
                spring-boot-maven-plugin
            

验证

使用 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}   

默认也是具有负载均衡的。

Spring cloud:服务调用-声明式客户端访问

标签:order   pos   not   tool   datetime   mys   搭建   负载   test   

原文地址:https://www.cnblogs.com/jiangbo44/p/12734052.html


评论


亲,登录后才可以留言!