sentinel与feign集成
建接口定义模块
hhd-hplat-provider-feign-api
定义POM 如下
引入spring-cloud-starter-openfeign 依赖
<?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>hhd-hplat</artifactId>
<groupId>com.hhd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hhd-hplat-provider-feign-api</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-openfeign</artifactId>
</dependency>
</dependencies>
</project>
定义feign接口
package com.hplat.provider;
import com.hplat.provider.fallback.UserServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/*
* 在Feign的声明式接口上添加fallback属性或者 fallbackFactory属 性
这里直接通过 fallbackFactory 声明,因为通过 fallback 无法捕获异常
* */
@FeignClient(value = "nacos-provider",fallbackFactory = UserServiceFallbackFactory.class )
public interface UserService {
@GetMapping(value = "/test1/{message}")
public String test1(@PathVariable("message") String message);
}
建异常处理工程类
package com.hplat.provider.fallback;
import com.hplat.provider.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
static private Logger logger = LoggerFactory.getLogger(UserServiceFallbackFactory.class);
@Override
public UserService create(Throwable cause) {
return new UserService(){
@Override
public String test1(String message) {
logger.error("原因:{}",cause);
return cause.getMessage();
}
};
}
}
建服务提供者模块
hhd-hplat-provider-feign
定义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>hhd-hplat</artifactId>
<groupId>com.hhd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hhd-hplat-provider-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.hhd</groupId>
<artifactId>hhd-hplat-provider-feign-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
不使用nacos动态配置功能的话spring-cloud-starter-alibaba-nacos-config 依赖可以去掉。
定义application.yml配置文件
server:
port: 8081
spring:
application:
name: nacos-provider
cloud: #nacos注册中心地址的配置法
nacos:
discovery:
server-addr: localhost:8848 # nacos 注册中心的地址
enabled: true # 关闭/启动nacos的注册中心功能
#ip: provider-xxx # 配置nacos的ip 地址
management:
endpoints:
web:
exposure:
include: "*"
girl:
name: happy
定义启动类
package com.hplat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // 开启服务的发现和注册
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class ,args) ;
}
}
定义接口类
package com.hplat.provider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController implements UserService{
@GetMapping(value = "/test1/{message}")
@Override
public String test1(@PathVariable("message") String message){
return "provider :"+message;
}
}
- 备注 : *
- 此处定义接口和常规的restful接口没有什么区别。
- controllerl类可以不从feign定义接口继承,此处集成是为了更好的校验接口参数的一致性。
建消费者模块
hhd-hplat-consumer-feign
定义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>hhd-hplat</artifactId>
<groupId>com.hhd</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hhd-hplat-consumer-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.hhd</groupId>
<artifactId>hhd-hplat-provider-feign-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
定义application.yml
server:
port: 8082
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
port: 8719
#对Feign的支持
feign:
sentinel:
enabled: true
定义消费者启动类
package com.hplat;
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 ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class ,args) ;
}
}
定义全局异常处理类
package com.hplat.consumer.handler;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
public class GlobalExceptionHandler {
/**
* 限流后处理方法
*/
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
try {
return new SentinelClientHttpResponse("被限制流量啦");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 熔断后处理的方法
*/
public static SentinelClientHttpResponse fallback(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
try {
return new SentinelClientHttpResponse("被降级啦");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
定义消费接口类
package com.hplat.consumer.controller;
import com.hplat.provider.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Autowired
private UserService userService;
// @Autowired
// private RestTemplate restTemplate;
@GetMapping("/test/{message}")
public String test(@PathVariable("message") String message) {
// ResponseEntity<String> responseEntity = restTemplate.getForEntity(
// "http://nacos-provider/test1/"+message,
// String.class,
// message
// );
// if(responseEntity.getStatusCode()== HttpStatus.OK){
// return ResponseEntity.ok(String.format("远程调用成功,结果为%s",responseEntity.getBody())) ;
// }
// return ResponseEntity.badRequest().body("远程调用失败") ;
return userService.test1(message);
}
}