nacos-provider pom文件1.8 UTF-8 UTF-8 2.3.7.RELEASE Hoxton.SR9 2.1.0.RELEASE org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test
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);
}
}
controller
@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-salvenacos-provider-salve模块跟nacos-provider模块一样就端口不一样
http://localhost:8002/provider/hello
nacos-consumer pom文件application.ymlcom.alibaba.csp sentinel-datasource-nacos 1.8.2 com.alibaba.cloud spring-cloud-starter-alibaba-sentinel 2.2.1.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.1.0.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test
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地址
controller
@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);
}
}
RestTemplateConfig
@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
二、fallback 1.修改CustomerController查看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";
}
}
2.测试
http://localhost:8003/customer/hello/1
http://localhost:8003/customer/hello/2
3.注意fallback:是进行降级处理
三、blockHandler 1.修改CustomerController@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
@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";
}
}
2.sentinel配置
3.测试
#访问1时直接报错,java没有对应fallback处理 http://localhost:8003/customer/hello/1 #当频繁访问,出现限流响应 http://localhost:8003/customer/hello/24.注意
blockHandler:仅负责sentinel违规
四、fallback、blockHandler同时配置 1.修改controller@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
private final String SERVER_URL="http://provider";
@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";
}
}
2.测试
#java异常fallback->fallbackHandler http://localhost:8003/customer/hello/1 #限流控制->helloBlockHandler http://localhost:8003/customer/hello/2五、OpenFegin 1.pom文件
2.application.ymlorg.springframework.cloud spring-cloud-starter-openfeign com.alibaba.csp sentinel-datasource-nacos com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test
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
3.启动类
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class OpenfeginApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeginApplication.class, args);
}
}
4.降级类
@Service
public class ProviderServiceFallback implements ProviderService {
@Override
public String hello() {
return "ProviderServiceFallback invoke hello";
}
}
5.Fegin服务类
@FeignClient(value = "provider",path = "/provider",fallback = ProviderServiceFallback.class)
@Service
public interface ProviderService {
@GetMapping("/hello")
String hello();
}
6.controller
@RestController
@RequestMapping("/fegin")
public class FeginController {
@Autowired
private ProviderService providerService;
@GetMapping("/hello")
@SentinelResource(value = "hello")
public String hello(){
return providerService.hello();
}
}
7.启动主程序
#报错 com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidatemetadata(Ljava/lang/Class;) #解决办法,修改父pomspringcloud版本为Hoxton.SR1。- -,BUG。期待后续解决 - -!!!,修改后重启解决8.测试Hoxton.SR1
http://localhost:9005/fegin/hello/



