java feign请求pathvariable_8、服务发现&服务消费者Feign

spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign。上一篇和大家一起学习了Ribbon。

Ribbon :Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的,它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载。

Feign :spring cloud feign 是一个使用起来更加方便的 HTTP 客戶端。 在使用ribbon时,通常会使用RestTemplate实现对http请求的封装,形成了模板化的调用方法。spring cloud feign在此基础上做了进一步的封装,Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,完全感知不到这是远程方法,更感知不到这是个HTTP请求。

1、新建项目sc-eureka-client-consumer-feign,对应的pom.xml文件如下

"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">

4.0.0

spring-cloud

sc-eureka-client-consumer-feign

0.0.1-SNAPSHOT

jar

sc-eureka-client-consumer-feign

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

org.springframework.cloud

spring-cloud-dependencies

Finchley.RELEASE

pom

import

UTF-8

1.8

1.8

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-openfeign

备注:spring cloud 2.x后spring-cloud-starter-feign已经被标识为过期,推荐使用spring-cloud-starter-openfeign

61ec34cd7373b0264f1d2bad362e7ae2.png

2、新建spring boot启动类ConsumerFeignApplication.java

package sc.consumer;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication

@EnableEurekaClient

@EnableFeignClients

public class ConsumerFeignApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerFeignApplication.class, args);

}

}

3、 创建配置文件bootstrap.yml和application.yml,对应的内容如下

bootstrap.yml

server:

port: 5800

application.yml

spring:

application:

name: sc-eureka-client-consumer-feign

eureka:

client:

registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true

fetchRegistry: true #是否从Eureka中获取注册信息,默认为true

serviceUrl:

defaultZone: http://localhost:5001/eureka/

4、 编写feign客户端

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;

@FeignClient(value="sc-eureka-client-provider")

public interface UserService {

@GetMapping("/user/getUser/{id}")

Map getUser(@PathVariable(value ="id") Long id);

@RequestMapping("/user/listUser")

Map listUser();

@PostMapping("/user/addUser")

Map addUser(@RequestBody User user);

@PutMapping("/user/updateUser")

Map updateUser(@RequestBody User user);

@DeleteMapping("/user/deleteUser/{id}")

Map deleteUser(@PathVariable(value ="id") Long id);

}

5、 分别启动注册中心项目sc-eureka-server和服务提供者sc-eureka-client-provider

6、 启动项目sc-eureka-client-consumer-feign,并验证是否启动成功

方法一

1b913d15971cd9ddf143a3e29d1e48e6.png

方法二

605c2f6f32236fe8a66b7b3ff91feff6.png

7、 使用postman验证

查询:

http://127.0.0.1:5800/feign/user/getUser/4

4da06656f709fd58d0b59e708e1be682.png

列表:

http://127.0.0.1:5800/feign/user/listUser

af7da4e2a8884ffd6f13ee5e5bedafc5.png

添加:

http://127.0.0.1:5800/feign/user/addUser

8aa09116b4a3cacfbae6b5ea80bf9664.png

更新:

http://127.0.0.1:5800/feign/user/updateUser

6321b2155c3aba5e6fd8138092f6548f.png

删除:

http://127.0.0.1:5800/feign/user/deleteUser/6

27220b6554ef5546c080a5efece6d1e4.png

备注:

sc-eureka-client-provider项目的UserController.java 需要修正

acf8f3151b024fba2acece7a4e6fa518.png

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-feign

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值