Skip to content

Commit

Permalink
fix fallback autowired bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Oct 7, 2022
1 parent 1d44a60 commit b2fc02f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
.addGenericArgumentValue(Objects.requireNonNull(definition.getBeanClassName()));
// beanClass全部设置为RetrofitFactoryBean
definition.setBeanClass(RetrofitFactoryBean.class);
definition.setPrimary(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -30,27 +27,5 @@ public interface DegradeSentinelApi {
@SentinelDegrade(enable = false)
Result<Person> getPerson2(@Query("id") Long id);

@Service
@Slf4j
class DegradeSentinelApiFallback implements DegradeSentinelApi {
@Override
public Result<Person> getPerson1(Long id) {
log.info("触发熔断了");
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person1")
.setData(new Person());
return fallback;
}

@Override
public Result<Person> getPerson2(Long id) {
log.info("触发熔断了");
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person2")
.setData(new Person());
return fallback;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Person> getPerson1(Long id) {
log.info("触发熔断了");
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person1")
.setData(new Person());
return fallback;
}

@Override
public Result<Person> getPerson2(Long id) {
log.info("触发熔断了");
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person2")
.setData(new Person());
return fallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -85,4 +87,8 @@ public void testDegrade() {

}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

0 comments on commit b2fc02f

Please sign in to comment.