目前的消费者是Ribbon+restTemplate形式进行远程调用的
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>e-commerce-center</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>member-service-nacos-consumer-81</artifactId>
<name>Archetype - member-service-nacos-consumer-81</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 引入openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--引入nacos的服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- springboot web starter 用来监听端口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 如果在子工程/模块指定了 version,则以指定为准 -->
</dependency>
<!--
1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息
等
2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 公共模块的jar包 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>e_commerce_center-common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
server:
port: 81
spring:
application:
name: member-service-nacos-consumer-81
# 配置nacos的服务注册
cloud:
nacos:
discovery:
server-addr: localhost:8848 # 配置要注册到的nacos地址,根据实际情况填写
package com.sun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author 孙显圣
* @version 1.0
*/
@SpringBootApplication // springboot启动类
@EnableDiscoveryClient // 开启nacos服务发现
@EnableFeignClients // 开启feign远程调用
public class MemberNacosCostomerApplication81 {
public static void main(String[] args) {
SpringApplication.run(MemberNacosCostomerApplication81.class, args);
}
}
package com.sun.springcloud.service;
import com.sun.springcloud.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* Description:
*
* @Author sun
* @Create 2024/3/30 10:39
* @Version 1.0
*/
public interface MemberOpenfeignService {
/**
* 根据id来获取某个会员的信息
*
* @param id 使用路径参数的形式传入参数
* @return 返回json格式的数据
*/
@GetMapping("/memberget/{id}") // 这里使用的路径参数
public Result getMemberById(@PathVariable("id") Long id);
}
package com.sun.springcloud.service;
import com.sun.springcloud.util.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* Description:
*
* @Author sun
* @Create 2024/3/30 10:39
* @Version 1.0
*/
@FeignClient("member-service-nacos-provider") // 会进行服务的发现,发现服务的ip+端口+上下文路径
public interface MemberOpenfeignService {
/**
* 根据id来获取某个会员的信息
*
* @param id 使用路径参数的形式传入参数
* @return 返回json格式的数据
*/
@GetMapping("/member/get/{id}") // 这里使用的路径参数
public Result getMemberById(@PathVariable("id") Long id);
}
package com.sun.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
/**
* Description:
*
* @Author sun
* @Create 2024/3/30 10:53
* @Version 1.0
*/
// 这两个注解是要远程调用的controller的注解
@RestController
@Slf4j
public class MemberOpenFeignController {
}
package com.sun.springcloud.controller;
import com.sun.springcloud.service.MemberOpenfeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* Description:
*
* @Author sun
* @Create 2024/3/30 10:53
* @Version 1.0
*/
// 这两个注解是要远程调用的controller的注解
@RestController
@Slf4j
public class MemberOpenFeignController {
@Resource
private MemberOpenfeignService memberOpenFeignService; // 注入远程调用的接口
}
package com.sun.springcloud.controller;
import com.sun.springcloud.service.MemberOpenfeignService;
import com.sun.springcloud.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* Description:
*
* @Author sun
* @Create 2024/3/30 10:53
* @Version 1.0
*/
// 这两个注解是要远程调用的controller的注解
@RestController
@Slf4j
public class MemberOpenFeignController {
@Resource
private MemberOpenfeignService memberOpenFeignService; // 注入远程调用的接口
/**
* 根据id来获取某个会员的信息
*
* @param id 使用路径参数的形式传入参数
* @return 返回json格式的数据
*/
@GetMapping("/member/openfeign/get/{id}") // 1.这里的url是可以改的
public Result getMemberById(@PathVariable("id") Long id) { // 2.这里的参数和方法名是可以改的
// 远程调用member-service-nacos-provider的getMemberById方法
return memberOpenFeignService.getMemberById(id); // 这里的参数和返回值是要远程调用的接口的参数和返回值
}
}
package com.sun.springcloud.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** 配置类,用于配置Ribbon的负载均衡策略
* @author 孙显圣
* @version 1.0
*/
@Configuration
public class RibbonRule {
@Bean
public IRule ribbonRule() {
// 随机策略
return new RandomRule();
}
}
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 10000
feign:
client:
config:
客户端的application-name:
connectTimeout: 5000
readTimeout: 10000
<!-- 引入Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
sentinel:
transport:
dashboard: localhost:8080 # 配置sentinel服务端的地址,根据实际情况填写
port: 8719 # 配置sentinel客户端的端口,根据实际情况填写,如果冲突,会自动+1,直到找到可用的端口
package com.sun.springcloud.service;
import com.sun.springcloud.util.Result;
import org.springframework.stereotype.Component;
/**
* Description: 会员服务的降级处理, 当服务不可用时,立即返回一个默认的结果
*
* @Author sun
* @Create 2024/3/30 14:32
* @Version 1.0
*/
@Component // 注入到spring容器中
public class MemberFeignFallbackService implements MemberOpenfeignService{
@Override
public Result getMemberById(Long id) {
return Result.error("503", "服务降级返回, 服务不可用");
}
}
# 配置openfeign和sentinel的整合
feign:
sentinel:
enabled: true
<!-- 引入nacos和sentinel持久化的依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
datasource:
ds1: # 从Nacos中读取sentinel的规则配置
nacos:
server-addr: localhost:8848 # 配置要注册到的nacos地址,根据实际情况填写
dataId: member-service-nacos-consumer-81 # 配置要读取的配置的dataId
groupId: DEFAULT_GROUP # 配置要读取的配置的groupId
data-type: json # 配置要读取的配置的类型
rule-type: flow # 配置要读取的配置的规则类型为流控规则
更多【云原生-【微服务】OpenFeign+Sentinel集中处理远程调用异常】相关视频教程:www.yxfzedu.com