关于java:Spring-Cloud-Alibaba-使用-feign-和-rebion-进行服务消费

8次阅读

共计 2322 个字符,预计需要花费 6 分钟才能阅读完成。

微服务的服务生产,个别是应用 feign 和 rebion 调用服务提供,进行服务的生产,本文将实战应用代码解说服务的生产。

微服务环境的搭建

创立一个 springboot 我的项目,springboot 是将服务进行拆分的一个最小服务单位。

增加 maven 依赖

  • 根本的 maven 依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • rebion 依赖

              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
              </dependency>
  • feign 依赖

             <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-openfeign</artifactId>
              </dependency>

feign 依赖蕴含 rebion 依赖,如果想同时应用 feign 和 rebion 依赖,只须要援用 feign 即可。

增加 application.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

增加 Appcation.java 启动类

@SpringBootApplication
// 将服务注册到注册核心
@EnableDiscoveryClient
// 扫描和注册 feign 客户端 bean 定义
@EnableFeignClients
public class NacosConsumeApplication {public static void main(String[] args) {SpringApplication.run(NacosConsumeApplication.class, args);
    }

}

其中 @EnableDiscoveryClient 将服务注册到注册核心,@EnableFeignClients 扫描和注册 feign 客户端 bean 定义。fegin bean 定义是 @FeignClient。

服务调用 : feign 和 ribbon

1. feign

  • 创立 feign 客户端
@FeignClient(value = "service-provider")
public interface ProductClient {@GetMapping("/hello")
    String product(@RequestParam("name") String name);
}

这里 @FeignClient 就是创立 bean 给 @EnableFeignClients 扫描的注册。@FeignClient 外面的配置 value 对应的是服务提供者的服务名称,@GetMapping 外面的 value 对应服务提供者的 @GetMapping 门路。

这样做的益处是这里间接配置好门路和服务名称,不须要调用方做任何的配置。

  • 创立申请调用 feign 客户端
@RestController
public class FeignController {


    // 这个谬误是编译器问题。因为这个 Bean 是在程序启动的时候注入的,编译器感知不到,所以报错。@Autowired
    private ProductClient productClient;

    @GetMapping("/feign")
    public String feign(String name){return productClient.product(name);
    }

}

2. rebion

  • 配置 RestTemplate bean

    @Configuration
    public class RibbonConfig {
    
      @LoadBalanced
      @Bean
      public RestTemplate restTemplate(){return new RestTemplate();
      }
    }
  • 调用服务

    @RestController
    public class RibbonController {
    
      @Autowired
      private RestTemplate restTemplate;
    
      @GetMapping("/ribbon")
      public String ribbon(String name){String result = restTemplate.getForObject("http://service-provider/hello?name="+name,String.class);
          return result;
      }
    }

feign 和 rebion 优缺点

  • feign 配置比拟简单明了,配置好了 FeignClient,间接调用即可,不过后续有多余的配置。
  • rebion 每次都须要配置申请门路,它的长处在于间接调用即可,不要独自配置客户端。
正文完
 0