前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)

Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)

作者头像
程裕强
发布2019-07-02 10:50:27
8130
发布2019-07-02 10:50:27
举报

版权声明:本文为博主原创文章,欢迎转载。 https://cloud.tencent.com/developer/article/1454277

1、Feign简介

Feign 整合了ribbon,具有负载均衡的能力;Feign 采用的是基于接口的注解

2、新建模块

新建一个服务消费者模块consumer-feign,代码结构如下图所示。

2.1 pom文件
<?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.cntaiping.tpa</groupId>
    <artifactId>consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>consumer-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>
2.2 配置文件application.yml
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8800/eureka/
server:
  port: 8500
spring:
  application:
    name: consumer-feign
2.3 启动类
package com.cntaiping.tpa.consumerfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ConsumerFeignApplication {

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

}
2.4 服务消费类

(1)Service层

定义一个feign接口,通过@ FeignClient(“服务名”),来指定消费哪个服务。

package com.cntaiping.tpa.consumerfeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "producer")
public interface SchedualService {
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    String hello(@RequestParam(value = "name") String name);
}

(2)controller层

package com.cntaiping.tpa.consumerfeign.controller;

import com.cntaiping.tpa.consumerfeign.service.SchedualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    SchedualService schedualService;

    @GetMapping(value = "/hello")
    public String hello(@RequestParam String name) {
        return schedualService.hello( name );
    }
}
2.5 运行效果
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Feign简介
  • 2、新建模块
    • 2.1 pom文件
      • 2.2 配置文件application.yml
        • 2.3 启动类
          • 2.4 服务消费类
            • 2.5 运行效果
            相关产品与服务
            负载均衡
            负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档