Springcloud from zero to build a micro-service (seven) ---- instead of using the Sentinel Hystrix

First, instead of using the Sentinel Hystrix

1. Modify the POM file
(1) plus the spring-cloud-openfeign-dependencies rely on cloud-dependencies module
Note: Add here the purpose is to solve the spring-cloud-starter-openfeign2.2.0.RELEASE in the interface method name misspelled problem.
Specifically refer to the article https://www.cnblogs.com/cdfive2018/p/12537412.html
<dependency>
<the groupId> org.springframework.cloud </ the groupId>
<the artifactId> Spring-Cloud-openfeign-Dependencies </ the artifactId>
<Version> $ {Spring-Cloud-alibaba.version} </ Version>
<type> POM </ type>
<scope> Import </ scope>
</ dependency>
(2) Consumer Cloud-module plus spring-cloud- starter-alibaba-sentinel dependent
<dependency>
<the groupId> com.alibaba.cloud </ the groupId>
<the artifactId> Spring-Cloud-starter-alibaba-sentinel </ the artifactId>


Use feign.sentinel.enabled = true Alternatively feign.hystrix.enabled = true
after the modifications
# feign.hystrix.enabled = true
feign.sentinel.enabled = true
3. Modify cloud-consumer module code
modifications CloudConsumerApplication class
in public RestTemplate restTemplate ( ) {
return new new RestTemplate ();
}
increase @SentinelRestTemplate the method
modified effect becomes
@LoadBalanced
@Bean
@SentinelRestTemplate
public RestTemplate RestTemplate () {
return new new RestTemplate ();
}

Second, using the Sentinel
1. Review module code cloud-consumer
(1) new class SentinelGlobalDefaultUtil, and treated as a unified block Fallback, the return value of the interface is to be noted here must be unified
package com.plkd.consumer.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public class SentinelGlobalDefaultUtil {
public static final int BLOCK_FLAG = 88888;
public static final String FALLBACK_DEFAULT_RESULT = "fallback";

public static int globalBlockHandler(BlockException ex) {
System.out.println("blocked by: " + ex.getClass().getSimpleName());
return BLOCK_FLAG;
}

public static String globalDefaultFallback(Throwable t) {
System.out.println("Fallback caught: " + t.getClass().getSimpleName());
return FALLBACK_DEFAULT_RESULT;
}
}

(2)修改OrderController类增加使用SentinelResource注解
修改后结果:
@RequestMapping(value = "/getUserByOrderId/{id}",method = RequestMethod.GET)
@SentinelResource(blockHandler = "globalBlockHandler", blockHandlerClass = SentinelGlobalDefaultUtil.class,
defaultFallback = "globalDefaultFallback",fallbackClass = {SentinelGlobalDefaultUtil.class})
public String getUserByOrderId(@PathVariable("id") int id){
return userRemoteClient.findUserByUserName("michael").toString();
}

2. Modify the user-open-api module code
(1) new class UserFeignClientFallbackFactory
package com.plkd.usercenter.client.sentinel;

import com.plkd.usercenter.client.UserRemoteClient;
import com.plkd.usercenter.dto.UseDto;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/ *
Recommended to use this embodiment, fallbackFactory fallback attributes based on the abnormality information can get
* /
@Component
public class UserFeignClientFallbackFactory the implements FallbackFactory <UserRemoteClient> {
@Override
public UserRemoteClient Create (the Throwable Throwable) {
return new new UserRemoteClient () {
@Override
usedto findUserByUserName public (the userName String) {
System.out.println ( "remote calls to be limiting or downgraded, Throwable:" Throwable +);
return null;
}
};
}
}
(2) modify the class UserRemoteClient
the modified annotation FeignClient @FeignClient (name = "cloud-provider ", fallbackFactory = UserFeignClientFallbackFactory.class)

Third, the deployment Dashboard Sentinel
1. Download the Sentinel Console: https: //github.com/alibaba/Sentinel/releases
execute the command to start Dashboard:
the Java -Dserver.port = 8081 -jar Sentinel-Dashboard-1.7.1.jar
such default 8081 is the port, enter in your browser: http: // localhost: 8081. The default account password: sentinel: sentinel, see the console interface for the deployment of success

2.cloud-consumer module configuration incorporated:
# Sentinel Dashboard
spring.cloud.sentinel.transport.dashboard = localhost: 8081

3. Restart cloud-consumer module
to access your browser to http: 8080 / order / getUserByOrderId / 1 : // localhost: 8080 / echo / 2018, as well as http: // localhost
application cloud- this time will appear in the Sentinel Console consumer

 

 

 4. Set the flow control rules

 

 

 The continuous access http: // localhost: 8080 / order / getUserByOrderId / 1 results are limiting

 

 

Guess you like

Origin www.cnblogs.com/michael-qi/p/12621350.html