springCloud alibaba nacos openFeign 服务之间调用(二)

上一篇文章介绍了order-service  调用 user-service

 @Autowired

      private RestTemplate restTemplate;
      
      @Bean
      //负载均衡
      @LoadBalanced

      public RestTemplate getRestTemplate(){

          return new     RestTemplate();

      }
      
      @GetMapping("/order")

      public String test1() {
          //此处服务名称   http://user-service
          return restTemplate.getForObject("http://user-service/hello",String.class);

      }

 

RestTemplate还需要写上服务及IP这些信息,本章介绍openFeign  

openFeign是基于REST的服务调用上提供更高级别的抽象。Spring Cloud openFeign在声明性原则上工作。使用openFeign时,我们在客户端编写声明式REST服务接口,并使用这些接口来编写客户端程序。开发人员不用担心这个接口的实现。这将在运行时由Spring动态配置。通过这种声明性的方法,开发人员不需要深入了解由HTTP提供的HTTP级别API的细节的RestTemplate。

 

一,编写接口,user-service-api  提供一组基于rest风格的api,新建子模块user-service-api

此时项目结构为

1,加入依赖pom 

<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>com.liu.dianmall</groupId>
    <artifactId>mall-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>user-service-api</artifactId>
  
   <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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-openfeign</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2,添加配置文件application.yml

server:
  port: 9002

spring:
  application:
    name: user-service-api
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
3,添加接口userClient

package com.liu.nacos;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import com.liu.nacos.impl.UserClientHystrix;

//user-service  服务名 fallback  阻断后调用的服务

@FeignClient(name = "user-service",fallback = UserClientHystrix.class)
public interface UserClient {

     @GetMapping("/hello")
     String helloNacos();
}

添加实现类

package com.liu.nacos.impl;

import com.liu.nacos.UserClient;

public class UserClientHystrix implements UserClient{

    public String helloNacos() {
        return "请求超时!";
    }
}
 

添加启动类,测试

package com.liu.nacos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
public class UserServiceApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApiApplication.class, args);
    }

    @Autowired
    private UserClient userClient;

    @GetMapping("/api/user")
    public String test() {
        return userClient.helloNacos();
    }
}


四,修改 order-service项目,修改为feign调用

1,加入api依赖

      <dependency>
            <groupId>com.liu.dianmall</groupId>
            <artifactId>user-service-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

2,修改,

package com.liu.nacos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
    
    
      @Autowired UserClient userClient;
     
    
    
        /*
         * @Autowired private RestTemplate restTemplate;
         * 
         * @Bean
         * 
         * @LoadBalanced public RestTemplate getRestTemplate(){ return new
         * RestTemplate(); }
         * 
         * @GetMapping("/order") public String test1() {
         * 
         * return restTemplate.getForObject("http://user-service/hello",String.class); }
         */
     
    
    
      @GetMapping("/order") 
      public String test1() { 
          
          return userClient.helloNacos();
      
      }
     
}

 

 

基于springcloud+springboot+nacos+openFeign的分布式事务组件seata项目源码.zip 介绍 分布式事务组件seata的使用demo,AT模式、TCC模式,集成springboot、springcloudnacos注册中心、openFeign服务调用、Ribbon负载均衡器)、spring jpa,数据库采用mysql demo中使用的相关版本号,具体请看代码。如果搭建个人demo不成功,验证是否是由版本导致,版本稍有变化可能出现相关组件的版本不一致便会出现许多奇怪问题 seata服务端 1.3 Nacos服务端 1.1.4 spring-cloud-alibaba-dependencies 2.1.0.RELEASE springboot 2.1.3.RELEASE springcloud Greenwich.RELEASE 软件架构 软件架构说明 springcloud-common 公共模块 springcloud-order-AT 订单服务 springcloud-product-AT 商品库存服务 springcloud-consumer-AT 消费调用springcloud-business-Tcc 工商银行服务 springcloud-merchants-Tcc 招商银行服务 springcloud-Pay-Tcc 消费调用者 AT模式:springcloud-order-AT,springcloud-product-AT,springcloud-consumer-AT为AT模式Dome;模拟场景用户购买商品下单; 调用流程springcloud-consumer-AT调用订单服务创建订单(新增一条数据到订单表);在调用商品库存服务扣减商品库存数量(修改商品库存表商品数量);最后出现异常则统一回滚,负责统一提交; 第一阶段:准备阶段(prepare)协调者通知参与者准备提交订单,参与者开始投票。协调者完成准备工作向协调者回应Yes。 第阶段:提交(commit)/回滚(rollback)阶段协调者根据参与者的投票结果发起最终的提交指令。如果有参与者没有准备好则发起回滚指令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值