SpringCloud - (三)服务消费者(feign)

Feign

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。

Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon

准备工作

启动项目

  • Ahut-Eureka-Server(注册中心)
  • Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
  • Ahut-Eureka-Client-Two(AHUT-EUREKA-CLIENT服务二)

浏览器访问Eureka
这里写图片描述

此时的项目架构

这里写图片描述

创建服务消费者

新创建一个项目Ahut-Service-Feign,和Ahut-Eureka-Client-One类似

右键,New>>Module>>Spring Init>>填写项目maven信息>>选择依赖>>填写项目名称

这里写图片描述

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>

    <groupId>com.ahut</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-feign</name>
    <description>service feign</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

    <dependencies>

        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- eureka disvovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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


</project>

项目启动类
加上@EnableFeignClients注解开启Feign的功能

package com.ahut;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/**
 * @author cheng
 * @className: ServiceFeignApplication
 * @description: service feign
 * @dateTime 2018/4/26 16:36
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

项目配置文件application.properties

# 配置服务器端口
server.port=8930
# spring项目名称
spring.application.name=AHUT-SERVICE-FEIGN
# 配置注册中心
eureka.client.service-url.defaultZone=http://localhost:8900/eureka/

声明一个接口HelloService
通过@ FeignClient(“服务名”),来指定调用哪个服务

package com.ahut.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author cheng
 * @interfaceName: HelloService
 * @description:
 * @dateTime 2018/4/26 16:39
 */
@FeignClient(value = "AHUT-EUREKA-CLIENT")
public interface HelloService {

    @RequestMapping(value = "/v1/showPort")
    String showPort();

    /**
     * @description: 演示带参数
     * @author cheng
     * @dateTime 2018/4/26 16:45
     */
    //@GetMapping(value = "/demo")
    //void demo(@RequestParam(value = "name") String name);

}

控制层HelloAction

package com.ahut.action;

import com.ahut.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cheng
 * @className: HelloAction
 * @description:
 * @dateTime 2018/4/26 14:40
 */
@RestController
@RequestMapping(value = "/v1")
public class HelloAction {

    @Autowired
    private HelloService helloService;

    /**
     * @description: 模拟调用
     * @author cheng
     * @dateTime 2018/4/26 14:42
     */
    @RequestMapping(value = "/hello")
    public String hello() {
        return helloService.showPort();
    }

}

启动项目

  • Ahut-Eureka-Server(注册中心)
  • Ahut-Eureka-Client-One(AHUT-EUREKA-CLIENT服务一)
  • Ahut-Eureka-Client-Two(AHUT-EUREKA-CLIENT服务二)
  • Ahut-Service-Feign(AHUT-SERVICE-FEIGN服务消费者)

访问eureka
这里写图片描述

此时的项目架构

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值