diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java index c5a835b..4c7be0a 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java @@ -72,6 +72,7 @@ private void processBeanDefinitions(Set beanDefinitions) { .addGenericArgumentValue(Objects.requireNonNull(definition.getBeanClassName())); // beanClass全部设置为RetrofitFactoryBean definition.setBeanClass(RetrofitFactoryBean.class); + definition.setPrimary(true); } } } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java index 265e18a..ae12fa5 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApi.java @@ -1,21 +1,18 @@ package com.github.lianjiatech.retrofit.spring.boot.test.degrade; -import org.springframework.stereotype.Service; - import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient; import com.github.lianjiatech.retrofit.spring.boot.degrade.sentinel.SentinelDegrade; import com.github.lianjiatech.retrofit.spring.boot.retry.Retry; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; -import lombok.extern.slf4j.Slf4j; import retrofit2.http.GET; import retrofit2.http.Query; /** * @author 陈添明 */ -@RetrofitClient(baseUrl = "${test.baseUrl}", fallback = DegradeSentinelApi.DegradeSentinelApiFallback.class) +@RetrofitClient(baseUrl = "${test.baseUrl}", fallback = DegradeSentinelApiFallback.class) @Retry(enable = false) public interface DegradeSentinelApi { @@ -30,27 +27,5 @@ public interface DegradeSentinelApi { @SentinelDegrade(enable = false) Result getPerson2(@Query("id") Long id); - @Service - @Slf4j - class DegradeSentinelApiFallback implements DegradeSentinelApi { - @Override - public Result getPerson1(Long id) { - log.info("触发熔断了"); - Result fallback = new Result<>(); - fallback.setCode(-1) - .setMsg("熔断Person1") - .setData(new Person()); - return fallback; - } - @Override - public Result getPerson2(Long id) { - log.info("触发熔断了"); - Result fallback = new Result<>(); - fallback.setCode(-1) - .setMsg("熔断Person2") - .setData(new Person()); - return fallback; - } - } } \ No newline at end of file diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApiFallback.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApiFallback.java new file mode 100644 index 0000000..99da4c9 --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelApiFallback.java @@ -0,0 +1,33 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.degrade; + +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +@Order(value = Ordered.HIGHEST_PRECEDENCE) +public class DegradeSentinelApiFallback implements DegradeSentinelApi { + @Override + public Result getPerson1(Long id) { + log.info("触发熔断了"); + Result fallback = new Result<>(); + fallback.setCode(-1) + .setMsg("熔断Person1") + .setData(new Person()); + return fallback; + } + + @Override + public Result getPerson2(Long id) { + log.info("触发熔断了"); + Result fallback = new Result<>(); + fallback.setCode(-1) + .setMsg("熔断Person2") + .setData(new Person()); + return fallback; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java index 66cf5da..633fae6 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/degrade/DegradeSentinelTest.java @@ -9,8 +9,11 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @@ -32,10 +35,9 @@ @SpringBootTest(classes = RetrofitTestApplication.class) @RunWith(SpringRunner.class) @Slf4j -public class DegradeSentinelTest { +public class DegradeSentinelTest implements ApplicationContextAware { - @Autowired - private DegradeSentinelApi degradeSentinelApi; + private ApplicationContext applicationContext; private static final ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) @@ -75,7 +77,7 @@ public void testDegrade() { .setBody(objectMapper.writeValueAsString(mockResult)) .setBodyDelay(5, TimeUnit.SECONDS); server.enqueue(response); - return degradeSentinelApi.getPerson1(2L).getCode(); + return applicationContext.getBean(DegradeSentinelApi.class).getPerson1(2L).getCode(); } catch (Exception e) { return 100; } @@ -85,4 +87,8 @@ public void testDegrade() { } + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } } \ No newline at end of file