微服务解决方案 -- Spring Cloud Alibaba (三)服务消费者(Feign)

不了解此套教程的可以移步之前章节
1.微服务解决方案 – Spring Cloud Alibaba (一)服务的注册与发现
2.微服务解决方案 – Spring Cloud Alibaba (二)服务提供者

服务消费者


Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,Nacos 也很好的兼容了 Feign,默认实现了负载均衡的效果

因为其默认就实现了负载均衡,所以我们平时就应该直接使用Feign,而不是用LoadBalancerClient

创建spring-cloud-alibaba-nacos-consumer项目,其pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.laoshiren</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>spring-cloud-alibaba-nacos-consumer</artifactId>
    <packaging>jar</packaging>

    <name>spring-cloud-alibaba-nacos-consumer</name>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.laoshiren.alibaba.nacos.consumer.NacosConsumerFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application类也只多了一个注解@EnableFeignClients,表示我们这个项目是feign

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerFeignApplication {

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

}

application.yml

spring:
  application:
    name: nacos-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

server:
  port: 8600

management:
  endpoints:
    web:
      exposure:
        include: "*"

访问服务提供者


创建service接口

package com.laoshiren.alibaba.nacos.consumer.feign.service;

import com.laoshiren.alibaba.nacos.consumer.feign.service.fallback.NacosServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * ProjectName:     spring-cloud-alibaba
 * Package:         com.laoshiren.alibaba.nacos.consumer.feign.service
 * ClassName:       NacosProviderService
 * Author:          laoshiren
 * Date:            2019/12/23 13:22
 * Version:         1.0.0
 * Description:     调用提供者服务
 */
@FeignClient(value = "nacos-provider")
public interface NacosProviderService {
    @GetMapping(value = "/hello/{message}" )
    String hello(@PathVariable(value = "message") String message);
}

方法和服务提供者一致,@FeignClietvalue 是我们服务提供者的application name,我们通过nacosnaming server去调用接口。也像之前一样随便写个controller来测试是否能调用。

@RestController
public class NacosProviderController {

    @Autowired
    private NacosProviderService nacosProviderService;

    @GetMapping("/hello")
    public String hello(){
        return nacosProviderService.hello("hello feign");
    }

}

打印出服务提供者的内容信息即正确

负载均衡


provider项目启动两次。
在这里插入图片描述
点击Edit Configurations...
在这里插入图片描述
选中Provider项目勾选 Allow parallel run ,点击Apply
在这里插入图片描述
首先启动一次项目,然后修改配置文件将端口号修改为8501,再次启动。右下角会弹框
在这里插入图片描述
点击Show run confiigurations in ServicesIDEA2018Dashboard)。
在这里插入图片描述
这样我们就启动两个服务提供者,我们通过服务消费者就可以调用2个服务提供者了。在nacos上也可以看见实例数为2
在这里插入图片描述

发布了27 篇原创文章 · 获赞 4 · 访问量 911

猜你喜欢

转载自blog.csdn.net/weixin_42126468/article/details/103687115