博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三篇: 服务消费者(Feign)(Greenwich版本)
阅读量:4180 次
发布时间:2019-05-26

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

一、在前面的工程基础上,增加一个service-feign的子模块。

 

修改父工程的pom.xml 把service-feign添加到它的子模块里面去

eureka-server
eureka-client
service-ribbon
service-feign

service-feign的pom.xml内容如下:

4.0.0
service-feign
0.0.1-SNAPSHOT
jar
service-feign
Demo project for Spring Boot
com.example
chapter2
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

application.properties配置如下:

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/spring.application.name=service-feignserver.port=8005
ServiceFeignApplication配置如下:
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;/** * @author xiaobu @EnableFeignClients 开启feign功能 */@SpringBootApplication@EnableDiscoveryClient@EnableEurekaClient@EnableFeignClientspublic class ServiceFeignApplication {    public static void main(String[] args) {        SpringApplication.run(ServiceFeignApplication.class, args);    }}

feign目录结构如下:

定义一个feign接口

package com.example.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 14:24 * @description V1.0 定义个feign接口 @FeignClient("服务名") 来确定调哪个服务 */@FeignClient("eureka-client")public interface FeignService {    /***     * @author xiaobu     * @date 2018/11/6 14:34     * @param name 名字     * @return java.lang.String     * @descprition value为test则是调用 eureka-client的test的方法     * RequestMapping(value="/test",method = RequestMethod.GET)与GetMapping(value="/test")等价     * RequestParam.value() was empty on parameter 0 第一个参数不能为空     * @version 1.0     */    //@RequestMapping(value="/test",method = RequestMethod.GET)    @GetMapping(value="/test")    String testFromClient(@RequestParam(value = "name") String name);}

 

定义一个controller

package com.example.controller;import com.example.service.FeignService;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;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/6 14:22 * @description V1.0 调用feignService的test方法 */@RestControllerpublic class FeginController {    @Autowired    FeignService feignService;    @GetMapping("/test")    public String test(@RequestParam String name){        return feignService.testFromClient(name);    }}

输入结果如下:

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

你可能感兴趣的文章
MCU_如何通过硬件VID 查找生产厂家
查看>>
NCNN部署例程 mxnet-gluoncv之simple_pose
查看>>
Ubuntu18.04查看显卡信息并安装NVDIA显卡驱动driver + Cuda + Cudnn
查看>>
电子元件二极管封装SMA,SMB,SMC的区别
查看>>
利用FFmpeg玩转Android视频录制与压缩(二)
查看>>
eclipse下生成Java类图和时序图,生成UML图
查看>>
M文件程序设计(matlab)
查看>>
matlab基础知识
查看>>
程序员的职业素养
查看>>
一道面试题深入了解java底层
查看>>
java下载附件
查看>>
cron表达式每个月最后一天
查看>>
Oracle中Like与Instr模糊查询性能大比拼
查看>>
Spring Boot入门===Hello World
查看>>
spring boot应用启动原理分析
查看>>
使用spring的好处
查看>>
微服务:分解应用以实现可部署性和可扩展性
查看>>
log4j2 使用详解
查看>>
spring security
查看>>
java线程池管理多线程的应用
查看>>