mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-04-27 01:40:50 +00:00
首页微服务集成Sentinel,实现小说服务调用的熔断降级
This commit is contained in:
parent
98b371d348
commit
deae12762e
@ -0,0 +1,57 @@
|
|||||||
|
package com.java2nb.novel.book.api.fallback;
|
||||||
|
|
||||||
|
import com.java2nb.novel.book.api.BookApi;
|
||||||
|
import com.java2nb.novel.book.entity.Book;
|
||||||
|
import com.java2nb.novel.book.entity.BookComment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说微服务API接口降级处理类(服务提供端,默认降级处理)
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/7
|
||||||
|
*/
|
||||||
|
public class BookApiFallback implements BookApi {
|
||||||
|
@Override
|
||||||
|
public List<Book> queryBookByMinUpdateTime(Date minDate, int limit) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> queryBookByIds(List<Long> ids) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> listRank(Byte type, Integer limit) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Book queryBookById(Long id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addBookComment(Long userId, BookComment comment) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<BookComment> listUserCommentByPage(Long userId, int page, int pageSize) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> queryNetworkPicBooks(String localPicPrefix, int limit) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBookPic(String picUrl, Long bookId) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,9 @@
|
|||||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
<artifactId>springfox-swagger2</artifactId>
|
<artifactId>springfox-swagger2</artifactId>
|
||||||
|
@ -21,7 +21,6 @@ spring:
|
|||||||
feign:
|
feign:
|
||||||
httpclient:
|
httpclient:
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
#关掉mq的健康检查,防止某些没有用到mq的服务启动报错,个别服务如需mq监控,单独开启
|
#关掉mq的健康检查,防止某些没有用到mq的服务启动报错,个别服务如需mq监控,单独开启
|
||||||
management:
|
management:
|
||||||
health:
|
health:
|
||||||
|
@ -40,6 +40,11 @@
|
|||||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -23,12 +23,12 @@
|
|||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-netflix-hystrix</artifactId>
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.java2nb.novel.home.feign;
|
package com.java2nb.novel.home.feign;
|
||||||
|
|
||||||
import com.java2nb.novel.book.api.BookApi;
|
import com.java2nb.novel.book.api.BookApi;
|
||||||
|
import com.java2nb.novel.home.feign.fallback.BookFeignFallback;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -9,7 +10,9 @@ import org.springframework.cloud.openfeign.FeignClient;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @since 2020/5/28
|
* @since 2020/5/28
|
||||||
*/
|
*/
|
||||||
@FeignClient("book-service")
|
|
||||||
|
@FeignClient(name = "book-service",fallback = BookFeignFallback.class)
|
||||||
public interface BookFeignClient extends BookApi {
|
public interface BookFeignClient extends BookApi {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.java2nb.novel.home.feign.fallback;
|
||||||
|
|
||||||
|
import com.java2nb.novel.book.api.fallback.BookApiFallback;
|
||||||
|
import com.java2nb.novel.home.feign.BookFeignClient;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说Feign客户端降级处理类(服务消费端,自定义降级处理)
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2020/6/7
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class BookFeignFallback extends BookApiFallback implements BookFeignClient {
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
spring:
|
spring:
|
||||||
profiles:
|
profiles:
|
||||||
include: [common]
|
include: [common]
|
||||||
|
feign:
|
||||||
|
sentinel:
|
||||||
|
enabled: true
|
||||||
|
20
pom.xml
20
pom.xml
@ -40,6 +40,7 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
|
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
|
||||||
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
|
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
|
||||||
|
<openfeign.version>2.2.0.RELEASE</openfeign.version>
|
||||||
<nacos.version>0.2.2.RELEASE</nacos.version>
|
<nacos.version>0.2.2.RELEASE</nacos.version>
|
||||||
<swagger.version>2.7.0</swagger.version>
|
<swagger.version>2.7.0</swagger.version>
|
||||||
<maven.test.skip>true</maven.test.skip>
|
<maven.test.skip>true</maven.test.skip>
|
||||||
@ -123,6 +124,23 @@
|
|||||||
<version>${novel-author-api.version}</version>
|
<version>${novel-author-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--在2.2.0.RELEASE里有一行注释描述,feign的SentinelContractHolder接口方法名parseAndValidatateMetadata拼写错误,
|
||||||
|
在2.2.2.RELEASE方法已修正了,即方法名发生了改变。
|
||||||
|
在spring-cloud-alibaba-sentinel中的SentinelContractHolder类,用到了该接口的这个方法
|
||||||
|
所以,要么降低SpringCloud版本,要么单独指定openfeign版本,否则启动报错
|
||||||
|
java.lang.AbstractMethodError:
|
||||||
|
com.alibaba.cloud.sentinel.feign.SentinelContractHolder.
|
||||||
|
parseAndValidateMetadata(Ljava/lang/Class;)Ljava/util/List
|
||||||
|
注意spring-cloud-openfeign-dependencies写在spring-cloud-dependencies前面,
|
||||||
|
因为dependency前面的优先生效。-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-openfeign-dependencies</artifactId>
|
||||||
|
<version>${openfeign.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- springCloud -->
|
<!-- springCloud -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -142,6 +160,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!--swagger-->
|
<!--swagger-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user