Skip to content

Commit

Permalink
README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 8, 2022
1 parent 2c73a45 commit 4ed7462
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ public class TimeStampInterceptor extends BasePathMatchInterceptor {
```java
@RetrofitClient(baseUrl = "${test.baseUrl}")
@Intercept(handler = TimeStampInterceptor.class, include = {"/api/**"}, exclude = "/api/test/savePerson")
@Intercept(handler = TimeStamp2Interceptor.class) // 需要多个,直接添加即可
public interface HttpApi {
@GET("person")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
import com.github.lianjiatech.retrofit.spring.boot.degrade.resilience4j.Resilience4jDegrade;
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;

Expand All @@ -17,6 +18,7 @@
/**
* @author 陈添明
*/
@Retry(enable = false)
@RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = DegradeR4jApi.HttpDegradeFallbackFactory.class)
@Resilience4jDegrade(slidingWindowType = CircuitBreakerConfig.SlidingWindowType.TIME_BASED,
failureRateThreshold = 30, minimumNumberOfCalls = 10, permittedNumberOfCallsInHalfOpenState = 5)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.github.lianjiatech.retrofit.spring.boot.test.degrade;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.lianjiatech.retrofit.spring.boot.core.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory;
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}", fallbackFactory = DegradeSentinelApi.HttpDegradeFallbackFactory.class)
@RetrofitClient(baseUrl = "${test.baseUrl}", fallback = DegradeSentinelApi.DegradeSentinelApiFallback.class)
@Retry(enable = false)
public interface DegradeSentinelApi {

/**
Expand All @@ -31,38 +31,26 @@ public interface DegradeSentinelApi {
Result<Person> getPerson2(@Query("id") Long id);

@Service
class HttpDegradeFallbackFactory implements FallbackFactory<DegradeSentinelApi> {
Logger log = LoggerFactory.getLogger(HttpDegradeFallbackFactory.class);

/**
* Returns an instance of the fallback appropriate for the given cause
*
* @param cause fallback cause
* @return 实现了retrofit接口的实例。an instance that implements the retrofit interface.
*/
@Slf4j
class DegradeSentinelApiFallback implements DegradeSentinelApi {
@Override
public DegradeSentinelApi create(Throwable cause) {
log.error("触发熔断了! ", cause.getMessage(), cause);
return new DegradeSentinelApi() {
@Override
public Result<Person> getPerson1(Long id) {
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person1")
.setData(new Person());
return fallback;
}
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) {
Result<Person> fallback = new Result<>();
fallback.setCode(-1)
.setMsg("熔断Person2")
.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 @@ -21,6 +21,7 @@
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 okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

Expand All @@ -30,6 +31,7 @@
@ActiveProfiles("sentinel")
@SpringBootTest(classes = RetrofitTestApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class DegradeSentinelTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package com.github.lianjiatech.retrofit.spring.boot.test.interceptor;

import java.io.IOException;

import org.springframework.stereotype.Component;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;

import lombok.Setter;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
* 加签拦截器,取加签值,放到header中
*
* @author 陈添明
*/
@Component
@Setter
public class SignInterceptor extends BasePathMatchInterceptor {

private String accessKeyId;

private String accessKeySecret;

public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}

public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}

@Override
public Response doIntercept(Chain chain) throws IOException {
Request request = chain.request();
Expand Down

0 comments on commit 4ed7462

Please sign in to comment.