题图来自APOD
你好,这里是codetrend专栏“SpringCloud2023实战”。
feign之前是Netflix的一个子项目,由于停止了维护,spring继续维护了一个openfeign作为替代。Spring Cloud OpenFeign 具有以下优点:
在 Spring Cloud 2023 中,远程调用的重要性主要体现在微服务架构中。微服务架构将应用程序划分为一组小型、松耦合的服务,每个服务都运行在自己的进程中,并通过轻量级的通信机制进行通信。远程调用在微服务架构中扮演着重要的角色,主要有以下几个方面的重要性:
无需特殊的集成和配置,需要注册到注册中心,这里以应用名为 client1
举例说明生产者。
spring.application.name: client1
spring:
cloud:
zookeeper:
connect-string: localhost:2181
server:
port: 10101
servlet:
context-path: /client1
package io.rainforest.banana.client1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package io.rainforest.banana.client1.web.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class HelloWorld {
@GetMapping("/")
public String hello(String hello){
return hello+" world";
}
@GetMapping("/hello2")
@ResponseBody
public List<String> hello2(String hello){
List<String> res = new ArrayList<>();
res.add("test1");
res.add("test2");
res.add(hello);
return res;
}
}
<artifactId>spring-cloud-starter-openfeign</artifactId>
。<?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">
<parent>
<groupId>io.rainforest</groupId>
<artifactId>banana</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>banana-client3</artifactId>
<description>spring cloud banana-client3</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--注册中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--引入openfeign主要引入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- LB 扩展 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 工具包依赖 -->
<dependency>
<groupId>io.rainforest</groupId>
<artifactId>banana-common-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
,配置中心配置主要是 spring.cloud.openfeign
下面的配置。spring:
application:
name: client3
cloud:
config:
profile: dev
name: ${spring.application.name}
# uri: http://localhost:10110
discovery:
enabled: true
service-id: config
zookeeper:
connect-string: 127.0.0.1:2181
openfeign:
httpclient:
enabled: false
okhttp:
enabled: true
通过引入 feign-okhttp
替换默认的httpurlconnection,以及修改配置 spring.cloud.openfeign.okhttp.enabled
实现。
package io.rainforest.banana.client3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
clent1
。package io.rainforest.banana.client3.web.demo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(name = "client1",value = "client1",contextId = "client1",path = "/client1")
public interface Client1 {
@RequestMapping(method = RequestMethod.GET,value = "/hello2")
List<String> hello2(@RequestParam("hello") String hello);
@GetMapping(value = "/")
String hello(@RequestParam("hello") String hello);
}
package io.rainforest.banana.client3.web.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloWorld {
@Autowired
private Client1 client1;
@GetMapping("/")
public String hello(String hello){
return hello+" world";
}
@GetMapping("/hello2")
public List<String> hello2(String hello){
return client1.hello2(hello);
}
@GetMapping("/hello")
public String hello1(String hello){
return client1.hello(hello);
}
}
完整源码信息查看 https://gitee.com/r0ad/spring-cloud-example
来自一线全栈程序员nine的探索与实践,持续迭代中。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。