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

本文详细介绍了在微服务架构中,如何从使用RestTemplate进行服务间调用过渡到采用Feign的声明式REST客户端。通过实例展示了如何配置Feign,包括依赖添加、接口定义、服务调用等步骤。

上一篇文章介绍了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();
      
      }
     
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值