发布于2021-05-29 20:56 阅读(586) 评论(0) 点赞(5) 收藏(0)
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。Feign有可插拔的注解,包括Feign注解和JAX-RS注解。Feign也支持编码器和解码器。
OpenFeign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等
集成了Ribbon,可以实现客户端的负载均衡
使用Eureka作为注册中心 (当然nacos、consul、zookeeper也可以);至于为什么要使用Eureka,你懂的
不是Eureka的代码量少,只是因为我有现成的代码
首先创建一个父工程,然后创建四个子模块,两个server作为注册中心,provider模块作为服务提供模块,consumer只作为模拟消费模块,不注册进Eureka的注册中心
1、父pom
<?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>
<groupId>com.hao</groupId>
<artifactId>cloud-eureka-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>cloud-eureka-server</module>
<module>cloud-eureka-server02</module>
<module>service-provider</module>
<module>service-consumer-feign</module>
</modules>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.3</version>
</parent>
<properties>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<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>
</project>
2、两个server服务模块pom(相同)
<?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>
<artifactId>cloud-eureka-demo</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server02</artifactId>
<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.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
3、provider模块
<?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>
<artifactId>cloud-eureka-demo</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4、consumer模块
<?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>
<artifactId>cloud-eureka-demo</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.feign</groupId>
<artifactId>service-consumer-feign</artifactId>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
1、两个server模块除了配置文件稍微不一样外,其他都一样
server:
port: 8080
spring:
application:
name: cloud-eureka-server
security:
user:
name: root
password: root
eureka:
instance:
hostname: eureka01
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://root:root@127.0.0.1:8081/eureka/
server:
port: 8081
spring:
application:
name: cloud-eureka-server02
security:
user:
name: root
password: root
eureka:
instance:
hostname: eureka02
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://root:root@127.0.0.1:8080/eureka/
2、product模块代码编写,框架如下
product实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private Integer id;
private String productName;
private Integer productNum;
private Double productPrice;
}
service
@Service
public class ProductServiceImpl implements ProductService {
@Override
public List<Product> selectProductList() {
return Arrays.asList(
new Product(1, "HuaWei nova3", 100, 2999.0),
new Product(2, "xiaomi", 99, 1999.0),
new Product(3, "vivo", 102, 2999.0)
);
}
}
application.yml
server:
port: 7070
spring:
application:
name: service-provider
eureka:
instance:
hostname: provider
prefer-ip-address: true
instance-id: http://${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/
1、导入OpenFeign依赖
2、创建接口,添加@FeignClient注解声明调用的服务
3、激活Feign组件,在启动类上添加@EnableFeignClients即可
@FeignClient(value = "service-provider")
public interface ProductService {
@GetMapping(value = "/product/list")
List<Product> selectProductList();
}
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ProductService productService;
@Override
public Order selectOrderById(Integer id) {
return new Order(id, "one", "china", 199D, productService.selectProductList());
}
}
@RestController
public class OrderController {
@Resource
private OrderService orderService;
@GetMapping(value ="/order/{id}")
public Order getOrderById(@PathVariable("id") Integer id) {
return orderService.selectOrderById(id);
}
}
4、其他代码
application.yml
server:
port: 9091
spring:
application:
name: service-consumer-feign
eureka:
client:
register-with-eureka: false #是否是将自己注册到注册中心
registry-fetch-interval-seconds: 20 #Client多久去服务器拉去注册信息 默认30s
service-url:
defaultZone: http://root:root@127.0.0.1:8080/eureka/,http://root:root@127.0.0.1:8081/eureka/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private Integer id;
private String orderNo;
private String orderAddress;
private Double totalPrice;
private List<Product> productList;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private Integer id;
private String productName;
private Integer productNum;
private Double productPrice;
}
成功!
使用OpenFeign进行远程调用还是非常符合我们程序员的习惯的,完全感知不到像在远程调用,帮我们省掉了很多重复的代码。
原文链接:https://blog.csdn.net/Kevinnsm/article/details/117253048
作者:想要飞翔的天使
链接:http://www.javaheidong.com/blog/article/207473/3539c7a9eecef8d79f17/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!