springboot+Feign声明式调用
点关注不迷路,欢迎再来!
精简博客内容,尽量已专业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。
Feign的灵感来源于Retrofit、JAXRS-2.0和WebSocket,其的目的是简化java的http客户端的编写,旨在通过最少的代码和资源来实现和http APi的连接。本文旨在讲解怎么使用,教程如下:
一.先创建一个Eureka-Server服务注册中心
回顾上节知识:springboot集成Eureka注册中心
二.创建一个EurekaClient客户端
1.pom.xml配置
<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-server</artifactId>
</dependency>
<!-- 声明调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.配置application.yml文件
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8081/eureka/
server:
port: 8082 #服务端口号
spring:
application:
name: service-client #服务名称--调用的时候根据名称来调用该服务的方法
3.配置启动类引入@EnableFeignClients
package com.sun.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 客户端启动类
* @author ex_sunqi
*
*/
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
4.编写控制器ClientController .java
package com.sun.eureka.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.sun.eureka.constant.URLConstant;
import com.sun.eureka.service.IClientSerive;
import com.sun.eureka.service.IClientSeriveFeign;
/**
* @author ex_sunqi
*
*/
@RestController
@RequestMapping(URLConstant.INDEX)
public class ClientController {
@Autowired
private IClientSerive clientService;
@RequestMapping(value=URLConstant.HELLO , method = RequestMethod.GET)
public String indexHellp() {
return clientService.getTxt();
}
}
5.定义接口及实现类
package com.sun.eureka.service;
/**
* @author ex_sunqi
*
*/
public interface IClientSerive {
public String getTxt();
}
package com.sun.eureka.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.sun.eureka.constant.URLConstant;
/**
* @author ex_sunqi
*
*/
@FeignClient("SERVICE-CUSTOM")
public interface ICustomSeriveFeign {
@RequestMapping(value =URLConstant.CUSTOM+URLConstant.INDEX, method = RequestMethod.GET)
public String getString();
}
package com.sun.eureka.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sun.eureka.service.IClientSerive;
import com.sun.eureka.service.ICustomSeriveFeign;
@Service("clientService")
public class ClientServiceImpl implements IClientSerive{
@Autowired
private ICustomSeriveFeign customSeriveFeign;
public String getTxt() {
return customSeriveFeign.getString();
}
}
三.创建EurekaCustom消费者
1.pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sun</groupId>
<artifactId>EurekaCustom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EurekaCustom</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置application.yml文件
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8081/eureka/
server:
port: 8083 #服务端口号
spring:
application:
name: service-custom #服务名称--调用的时候根据名称来调用该服务的方法
3.配置启动类引入@EnableFeignClients
package com.sun.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 消费者启动类
* @author andy
*
*/
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaCustomApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaCustomApplication.class, args);
}
}
4.编写控制器CustomController .java
package com.sun.eureka.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.sun.eureka.constant.URLConstant;
import com.sun.eureka.service.ICustomSerive;
/**
* @author ex_sunqi
*
*/
@RestController
@RequestMapping(URLConstant.CUSTOM)
public class CustomController {
@Autowired
private ICustomSerive customService;
@RequestMapping(value=URLConstant.INDEX ,method =RequestMethod.GET)
public String toString() {
return customService.getString();
}
}
5.定义接口及实现类
package com.sun.eureka.service;
/**
* @author ex_sunqi
*/
public interface ICustomSerive {
public String getString();
}
package com.sun.eureka.impl;
import org.springframework.stereotype.Service;
import com.sun.eureka.service.ICustomSerive;
@Service("customService")
public class CustomServiceImpl implements ICustomSerive{
@Override
public String getString() {
return "Hello-World";
}
}
四.访问Eureka注册中心
发现EurekaClient和EurekaCustom都已注册成功
见证奇迹的时刻,访问http://127.0.0.1:8082/index/hello,看看会得到什么
通过访问EurekaClient,来调用EurekaCustom的接口,成功获取到了结果Hello-World。回顾下与httpInvoke调用有什么差异?