1.简介
微服务架构中使用 OpenFeign 进行服务调用, OpenFeign 提供了一种简洁的方式来定义和处理服 务间的调用。 OpenFeign 作为一个声明式的、模块化的 HTTP 客户端,通过 「接口」 的定义和 「注解」 的使用,简化了微服务之间的通信调用。
2.案例openfeign
创建alibaba-user-provider-8080
启动类添加注解 @EnableDiscoveryClient
编写配置文件application.yml
server:
port: 8080
spring:
application:
name: alibaba-user-provider-8080 #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:3333 #注册服务
datasource:
url: jdbc:mysql://127.0.0.1:3307/hz_security?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
password: root
username: root
mybatis-plus:
type-aliases-package: com.hz.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
编写dao接口(使用mybatisplus)
编写serviceAP
创建alibaba-user-consumer-8090
编写配置文件
server:
port: 8090
spring:
application:
name: alibaba-user-consumer-8090 #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:3333
datasource:
url: jdbc:mysql://127.0.0.1:3307/hz_security?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
password: root
username: root
mybatis-plus:
type-aliases-package: com.hz.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
创建
userservice
创建controller进行测试
公共jar包
<?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
https://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.17.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hz</groupId>
<artifactId>cloud-alibaba-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-alibaba-demo</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba-version>2.1.3.RELEASE</spring-cloud-alibabaversion>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacosdiscovery</artifactId>
<version>${spring-cloud-alibaba-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-alibaba-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!--开启Spring Cloud 应用程序启动时加载bootstrap配置文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.4</version>
</dependency>
添加前缀
使用@FeignClient下path属性
hystrix 熔断降级:使用 fallback 进行服务降级在配置文件中开启服务降级:feign:hystrix:enabled: true
不能用的的话换下面的
feign: #开启熔断处理 circuitbreaker: enabled: true
编写serviceimpl
@Component
public class SysUserServiceImpl implements SysUserService {
@Override
public ResultJSON findUserById(Integer id) {
return ResultJSON.error("系统异常。。。。");
}
}
修改接口中
@FeignClient
@FeignClient(name = "alibaba-user-provider",path = "user",fallback =SysUserServiceImpl.class)
如图
3.ribbon负载均衡Ribbon(负载均衡)注意:2021版本已经不支持 ribbon
SpringCloud
从
2020.0.1
版本开始
,
移除了
Ribbon
组件,使⽤
Spring Cloud LoadBalancer
组件来代替 Ribbon
实现
客户端负载均衡
在
consumer
中引入
jar
依赖
复制一个
provider
项目
修改
yml
配置文件
服务名必须一致
也可在consumer中配置策略
demo-provider: #provider服务名称
ribbon:
NFLoadBalancerRuleClassName:
com.netflix.loadbalancer.WeightedResponseTimeRule
默认为轮询策略
com.netflix.loadbalancer.RandomRule 随机分配策略
com.netflix.loadbalancer.WeightedResponseTimeRule 根据响应时间长短分配权重
4. loadbalancer负载均衡
在consumer中引入jar文件
<!--客户端负载均衡loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
注意:将两个
provider
服务名改为一致,端口号不一致,
consumer
中调用服务(一个
provider
两个实 例)
在config包中添加配置类RandomLoadBalancerConfig,实现随机策略
ppublic class RandomLoadBalancerConfig {
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(
Environment environment, LoadBalancerClientFactory
loadBalancerClientFactory) {
String name =
environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomLoadBalancer(loadBalancerClientFactory
.getLazyProvider(name, ServiceInstanceListSupplier.class),
name);
}
}
在
BillServive
中配置注解
@FeignClient(name = "bill-provider",path = "bill")
@LoadBalancerClient(name = "bill-provider",configuration =RandomLoadBalancerConfig.class)
public interface BillServive {
@GetMapping("/find")
public ResultAJAX findBillById(@RequestParam("id") Integer id);
}