1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

基于F版的一些适配性调整

This commit is contained in:
theonefx
2020-04-03 10:38:16 +08:00
parent c579109d2a
commit 0db3b26fd6
69 changed files with 412 additions and 2640 deletions

View File

@@ -74,7 +74,7 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// detail
if (!sentinelProperties.isEnabled()) {
detailMap.put("enabled", false);
builder.up().withDetails(detailMap);
withDetails(builder.up(), detailMap);
return;
}
@@ -139,11 +139,16 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// If Dashboard and DataSource are both OK, the health status is UP
if (dashboardUp && dataSourceUp) {
builder.up().withDetails(detailMap);
withDetails(builder.up(), detailMap);
}
else {
builder.down().withDetails(detailMap);
withDetails(builder.down(), detailMap);
}
}
private void withDetails(Health.Builder builder, Map<String, Object> detailMap) {
for (String key : detailMap.keySet()) {
builder.withDetail(key, detailMap.get(key));
}
}
}

View File

@@ -45,8 +45,8 @@ public class SentinelContractHolder implements Contract {
}
@Override
public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
List<MethodMetadata> metadatas = delegate.parseAndValidateMetadata(targetType);
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType);
metadatas.forEach(metadata -> METADATA_MAP
.put(targetType.getName() + metadata.configKey(), metadata));
return metadatas;

View File

@@ -33,7 +33,6 @@ import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* {@link Feign.Builder} like {@link HystrixFeign.Builder}.
@@ -87,11 +86,7 @@ public final class SentinelFeign {
"fallback");
Class fallbackFactory = (Class) getFieldValue(feignClientFactoryBean,
"fallbackFactory");
String beanName = (String) getFieldValue(feignClientFactoryBean,
"contextId");
if (!StringUtils.hasText(beanName)) {
beanName = (String) getFieldValue(feignClientFactoryBean, "name");
}
String beanName = (String) getFieldValue(feignClientFactoryBean, "name");
Object fallbackInstance;
FallbackFactory fallbackFactoryInstance;

View File

@@ -99,7 +99,7 @@ public class SentinelInvocationHandler implements InvocationHandler {
}
else {
String resourceName = methodMetadata.template().method().toUpperCase()
+ ":" + hardCodedTarget.url() + methodMetadata.template().path();
+ ":" + hardCodedTarget.url() + methodMetadata.template().url();
Entry entry = null;
try {
ContextUtil.enter(resourceName);

View File

@@ -1,136 +1,136 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.sentinel;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Add this unit test to verify https://github.com/alibaba/spring-cloud-alibaba/pull/838.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class },
properties = { "feign.sentinel.enabled=true" })
public class ContextIdSentinelFeignTests {
@Autowired
private EchoService echoService;
@Autowired
private FooService fooService;
@Test
public void testFeignClient() {
assertThat(echoService.echo("test")).isEqualTo("echo fallback");
assertThat(fooService.echo("test")).isEqualTo("foo fallback");
assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
@EnableFeignClients
public static class TestConfig {
}
@FeignClient(contextId = "echoService", name = "service-provider",
fallback = EchoServiceFallback.class,
configuration = FeignConfiguration.class)
public interface EchoService {
@GetMapping("/echo/{str}")
String echo(@PathVariable("str") String str);
}
@FeignClient(contextId = "fooService", value = "foo-service",
fallbackFactory = CustomFallbackFactory.class,
configuration = FeignConfiguration.class)
public interface FooService {
@RequestMapping(path = "echo/{str}")
String echo(@RequestParam("str") String param);
}
public static class FeignConfiguration {
@Bean
public EchoServiceFallback echoServiceFallback() {
return new EchoServiceFallback();
}
@Bean
public CustomFallbackFactory customFallbackFactory() {
return new CustomFallbackFactory();
}
}
public static class EchoServiceFallback implements EchoService {
@Override
public String echo(@RequestParam("str") String param) {
return "echo fallback";
}
}
public static class FooServiceFallback implements FooService {
@Override
public String echo(@RequestParam("str") String param) {
return "foo fallback";
}
}
public static class CustomFallbackFactory
implements feign.hystrix.FallbackFactory<FooService> {
private FooService fooService = new FooServiceFallback();
@Override
public FooService create(Throwable throwable) {
return fooService;
}
}
}
///*
// * Copyright 2013-2018 the original author or authors.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * https://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
//
//package com.alibaba.cloud.sentinel;
//
//import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.cloud.openfeign.EnableFeignClients;
//import org.springframework.cloud.openfeign.FeignClient;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//
//import static org.assertj.core.api.Assertions.assertThat;
//
///**
// * Add this unit test to verify https://github.com/alibaba/spring-cloud-alibaba/pull/838.
// *
// * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
// */
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class },
// properties = { "feign.sentinel.enabled=true" })
//public class ContextIdSentinelFeignTests {
//
// @Autowired
// private EchoService echoService;
//
// @Autowired
// private FooService fooService;
//
// @Test
// public void testFeignClient() {
// assertThat(echoService.echo("test")).isEqualTo("echo fallback");
// assertThat(fooService.echo("test")).isEqualTo("foo fallback");
// assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
// assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
// assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
// }
//
// @Configuration
// @EnableAutoConfiguration
// @ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
// @EnableFeignClients
// public static class TestConfig {
//
// }
//
// @FeignClient(name = "service-provider",
// fallback = EchoServiceFallback.class,
// configuration = FeignConfiguration.class)
// public interface EchoService {
//
// @GetMapping("/echo/{str}")
// String echo(@PathVariable("str") String str);
//
// }
//
// @FeignClient(value = "foo-service",
// fallbackFactory = CustomFallbackFactory.class,
// configuration = FeignConfiguration.class)
// public interface FooService {
//
// @RequestMapping(path = "echo/{str}")
// String echo(@RequestParam("str") String param);
//
// }
//
// public static class FeignConfiguration {
//
// @Bean
// public EchoServiceFallback echoServiceFallback() {
// return new EchoServiceFallback();
// }
//
// @Bean
// public CustomFallbackFactory customFallbackFactory() {
// return new CustomFallbackFactory();
// }
//
// }
//
// public static class EchoServiceFallback implements EchoService {
//
// @Override
// public String echo(@RequestParam("str") String param) {
// return "echo fallback";
// }
//
// }
//
// public static class FooServiceFallback implements FooService {
//
// @Override
// public String echo(@RequestParam("str") String param) {
// return "foo fallback";
// }
//
// }
//
// public static class CustomFallbackFactory
// implements feign.hystrix.FallbackFactory<FooService> {
//
// private FooService fooService = new FooServiceFallback();
//
// @Override
// public FooService create(Throwable throwable) {
// return fooService;
// }
//
// }
//
//}