博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)
阅读量:2390 次
发布时间:2019-05-10

本文共 2670 字,大约阅读时间需要 8 分钟。

1、Feign简介

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

2、新建模块

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

在这里插入图片描述

2.1 pom文件

4.0.0
com.cntaiping.tpa
consumer-feign
0.0.1-SNAPSHOT
jar
consumer-feign
Demo project for Spring Boot
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign

2.2 配置文件application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8800/eureka/server:  port: 8500spring:  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@EnableFeignClientspublic 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;@RestControllerpublic class MessageController {    @Autowired    SchedualService schedualService;    @GetMapping(value = "/hello")    public String hello(@RequestParam String name) {        return schedualService.hello( name );    }}

2.5 运行效果

在这里插入图片描述

在这里插入图片描述

转载地址:http://rgvab.baihongyu.com/

你可能感兴趣的文章
collective intelligence framework
查看>>
2015年关注的技术书籍
查看>>
windows 2003 server 记录远程桌面的连接登录日志和修改3389连接端口方法
查看>>
samhain:比较变态的入侵检测系统
查看>>
Linux psacct文档
查看>>
使用setuptools自动安装python模块
查看>>
python IDE环境
查看>>
传说中的windows加固 -.... -
查看>>
windows目录监控软件
查看>>
Virus Bulletin malware分析杂志以及paper
查看>>
Security Considerations for AppLocker
查看>>
Oracle Forensics t00ls
查看>>
JetLeak Vulnerability: Remote Leakage Of Shared Buffers In Jetty Web Server [CVE-2015-2080]
查看>>
zZ-ModSecurity Framework支持Web应用安全核心规则集
查看>>
zz-LDAP详解
查看>>
zZ-google-perftools 加速MySQL – TCMalloc
查看>>
apache 防DDOS脚本
查看>>
使用syslog-ng 和stunnel 创建集中式安全日志服务器
查看>>
网友将电视剧潜伏当职场教科书 研究办公室政治
查看>>
graudit
查看>>