Spring Cloud Feign消费服务

一 : Feign简介

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,这整个调用过程和Dubbo的RPC非常类似。开发起来非常的优雅。

二 : 新建spring starter project

项目结果如下:


三 : 新增pom依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
四 : 在Application新增注解开启Feign支持

package cn.sh.daniel;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient    //开启发现服务功能,从注册中心获取提供的服务列表
@EnableFeignClients     //开启Spring Cloud Feign的支持功能
public class CloudShopFeignApplication {


	public static void main(String[] args) {
		SpringApplication.run(CloudShopFeignApplication.class, args);
	}
}
五 : 配置application.yml文件

server:
  port: 7001
  
spring:
  application:
    name: cloud-shop-feign
    
eureka:
  client:
    service-url:
      defaultZone: http://daniel-cloud-shop-eureka:eureka123123@server1:9001/eureka/,http://daniel-cloud-shop-eureka:eureka123123@server2:9002/eureka/
  
  
六 : 新建UserServicejie
package cn.sh.daniel.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import cn.sh.daniel.entity.User;

@FeignClient(value = "cloud-shop-userservice")
public interface UserService {
	
	@GetMapping("/user/{id}")  //路径
	public User findUserById(@PathVariable("id")Long id);
}
@FeignClient(value = "cloud-shop-userservice") 指定服务提供者,与服务提供者配置文件配置的name一致,这边不区分大小写

七 : 新建UserController,并注入UserService

package cn.sh.daniel.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.sh.daniel.entity.User;
import cn.sh.daniel.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/{id}")
	public User findUserById(@PathVariable("id")Long id) {
		return userService.findUserById(id);
	}
}
八 : 启动Feign服务,成功注册到注册中心



九 : 调用Feign服务


成功调用到后端的user微服务!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值