<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8001
spring:
application:
name: provider
cloud:
nacos:
discovery:
server-addr: 192.168.31.100:8848
#暴露监控
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Value("${server.port}")
private Integer port;
@GetMapping("/hello")
public String hello(){
return "Hello nacos , server port is "+port;
}
}
http://localhost:8001/provider/hello
nacos-provider-salve
模块跟nacos-provider
模块一样就端口不一样
http://localhost:8002/provider/hello
<dependencies>
<!--sentinel nacos-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.2</version>
</dependency>
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
server:
port: 8003
spring:
application:
name: customer
cloud:
nacos:
discovery:
server-addr: 192.168.31.100:8848
sentinel:
transport:
#配置sentinel地址,端口
dashboard: 192.168.31.100:8859 #这里是我linux地址
#客户端IP(sentinel dashboard进行实时监控的主机ip地址)
# 默认端口8719端口假如被占用会自动从8719开始依次+1扫描,直到找到未被占用的端口
port: 8719
client-ip: 172.100.20.220 #这里是我windows地址
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
@GetMapping(value = "/hello")
@SentinelResource(value = "hello")
public String hello(){
return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
}
}
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
测试接口
http://localhost:8003/customer/hello
查看nacos
查看sentinel
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
@GetMapping(value = "/hello/{id}")
@SentinelResource(value = "hello",fallback = "fallbackHandler")
public String hello(@PathVariable("id") String id){
if (id.equals("1")){
throw new RuntimeException("id不能为1");
}
return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
}
public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
return "CustomerController invoke fallbackHandler";
}
}
http://localhost:8003/customer/hello/1
http://localhost:8003/customer/hello/2
fallback:是进行降级处理
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
/** * blockHandler:仅负责sentinel违规 */
@GetMapping(value = "/hello/{id}")
@SentinelResource(value = "hello",blockHandler = "helloBlockHandler")
public String hello(@PathVariable("id") String id){
if (id.equals("1")){
throw new RuntimeException("id不能为1");
}
return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
}
public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
return "CustomerController invoke fallbackHandler";
}
public String helloBlockHandler(String id, BlockException e){
return "CustomerController invoke blockHandler";
}
}
#访问1时直接报错,java没有对应fallback处理
http://localhost:8003/customer/hello/1
#当频繁访问,出现限流响应
http://localhost:8003/customer/hello/2
blockHandler:仅负责sentinel违规
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
/** * blockHandler:仅负责sentinel违规 */
@GetMapping(value = "/hello/{id}")
@SentinelResource(value = "hello",blockHandler = "helloBlockHandler",fallback = "fallbackHandler")
public String hello(@PathVariable("id") String id){
System.out.println(id);
if (id.equals("1")){
throw new RuntimeException("id不能为1");
}
return restTemplate.getForObject(SERVER_URL+"/provider/hello", String.class);
}
public String fallbackHandler(@PathVariable("id") String id ,Throwable e){
return "CustomerController invoke fallbackHandler";
}
public String helloBlockHandler(String id,BlockException e){
return "CustomerController invoke blockHandler";
}
}
#java异常fallback->fallbackHandler
http://localhost:8003/customer/hello/1
#限流控制->helloBlockHandler
http://localhost:8003/customer/hello/2
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--sentinel nacos-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
server:
port: 9005
spring:
application:
name: customer-fegin
cloud:
nacos:
discovery:
server-addr: 192.168.31.100:8848
sentinel:
transport:
#配置sentinel地址,端口
dashboard: 192.168.31.100:8859
port: 8719
#客户端IP
client-ip: 172.100.20.220
#开启sentinel 对fegin的支持
feign:
sentinel:
enabled: true
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class OpenfeginApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeginApplication.class, args);
}
}
@Service
public class ProviderServiceFallback implements ProviderService {
@Override
public String hello() {
return "ProviderServiceFallback invoke hello";
}
}
@FeignClient(value = "provider",path = "/provider",fallback = ProviderServiceFallback.class)
@Service
public interface ProviderService {
@GetMapping("/hello")
String hello();
}
@RestController
@RequestMapping("/fegin")
public class FeginController {
@Autowired
private ProviderService providerService;
@GetMapping("/hello")
@SentinelResource(value = "hello")
public String hello(){
return providerService.hello();
}
}
#报错
com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata(Ljava/lang/Class;)
#解决办法,修改父pomspringcloud版本为Hoxton.SR1。- -,BUG。期待后续解决 - -!!!,修改后重启解决
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
http://localhost:9005/fegin/hello/
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/121034803
内容来源于网络,如有侵权,请联系作者删除!