forked from LianjiaTech/retrofit-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yukdawn
committed
Apr 6, 2022
1 parent
6fe63fa
commit 7f8e34d
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/HttpApi4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.lianjiatech.retrofit.spring.boot.test.http; | ||
|
||
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; | ||
import com.github.lianjiatech.retrofit.spring.boot.degrade.Degrade; | ||
import com.github.lianjiatech.retrofit.spring.boot.degrade.FallbackFactory; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; | ||
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
import retrofit2.http.GET; | ||
import retrofit2.http.Query; | ||
|
||
/** | ||
* @author 陈添明 | ||
*/ | ||
@RetrofitClient(baseUrl = "${test.baseUrl}", fallbackFactory = HttpApi4.HttpDegradeFallbackFactory.class) | ||
@Degrade(count = 0.5) | ||
public interface HttpApi4 { | ||
|
||
/** | ||
* . | ||
* | ||
* @param id . | ||
* @return . | ||
*/ | ||
@GET("person") | ||
Result<Person> getPerson(@Query("id") Long id); | ||
|
||
@Service | ||
public class HttpDegradeFallbackFactory implements FallbackFactory<HttpApi4> { | ||
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. | ||
*/ | ||
@Override | ||
public HttpApi4 create(Throwable cause) { | ||
log.error("触发熔断了! ", cause.getMessage(), cause); | ||
return id -> { | ||
Result<Person> fallback = new Result<>(); | ||
fallback.setCode(100) | ||
.setMsg("fallback") | ||
.setData(new Person()); | ||
return fallback; | ||
}; | ||
} | ||
} | ||
|
||
} |