springcloud(服务消费者,Feign)

1)简介

Feign是一个伪Http客户端

特点:

1.1)基于接口的注解

1.2)整合了Ribbon,具有负载均衡的能力

1.3)整合了Hystrix,具有熔断的能力

 

2)依赖组件:注册中心与服务发现,服务提供者(eureka client注册到注册中心中)

 

3)搭建feign

3.1)添加pom.xml配置


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

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

	</dependencies>

 

3.2)配置YML

eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.3.30:8005/eureka/
  instance:
    instance-id: 192.168.3.30:${server.port}
    prefer-ip-address: true
    status-page-url-path: /index.html #配置在注册中心界面服务列表的Status列点击跳转到项目用
    
server:
  port: 8020

spring:
  application:
    name: service-feign

 

3.3)配置类

启动类:这里主要是添加了@EnableFeignClients,开启了feigin

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

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {

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

}

 

Web层controller暴露接口访问:

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.panda.feign.service.UserService;

@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/user/findAll")
	public Map findAll() throws Exception{
		return userService.findAll();
	}
	
}

 

服务调用类:定义feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。

import java.util.Map;

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

@FeignClient(value="PANDA-FRAME")
public interface UserService {

	@RequestMapping("/user/findAll")
	Map findAll();
	
}

 

访问接口结果:

 

github代码:

注册中心:https://github.com/April-D-phil/panda-eureka

服务提供者:https://github.com/April-D-phil/panda-frame

服务消费者:https://github.com/April-D-phil/panda-feign

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值