Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringCloud-study
提交
73476c30
S
SpringCloud-study
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringCloud-study
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringCloud-study
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
73476c30
编写于
1月 14, 2023
作者:
Q
qinyingjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:添加备注
上级
1a5e52a0
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
181 addition
and
34 deletion
+181
-34
pom.xml
pom.xml
+1
-0
springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java
.../src/main/java/com/kwan/springcloud/FeignApplication.java
+10
-0
springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java
...n/java/com/kwan/springcloud/api/ComputeClientHystrix.java
+0
-1
springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java
.../main/java/com/kwan/springcloud/service/FeignService.java
+4
-24
springcloud-service-feign/src/main/resources/application.yaml
...ngcloud-service-feign/src/main/resources/application.yaml
+5
-1
springcloud-service-hystrix-dashboard/pom.xml
springcloud-service-hystrix-dashboard/pom.xml
+49
-0
springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java
...ava/com/kwan/springcloud/HystrixDashboardApplication.java
+22
-0
springcloud-service-hystrix-dashboard/src/main/resources/application.yaml
...ice-hystrix-dashboard/src/main/resources/application.yaml
+7
-0
springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java
...om/kwan/springcloud/HystrixDashboardApplicationTests.java
+13
-0
springcloud-service-portal/pom.xml
springcloud-service-portal/pom.xml
+11
-0
springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java
...src/main/java/com/kwan/springcloud/PortalApplication.java
+11
-2
springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java
...ava/com/kwan/springcloud/controller/PortalController.java
+13
-0
springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java
...main/java/com/kwan/springcloud/service/PortalService.java
+22
-5
springcloud-service-portal/src/main/resources/application.yaml
...gcloud-service-portal/src/main/resources/application.yaml
+13
-1
未找到文件。
pom.xml
浏览文件 @
73476c30
...
@@ -35,6 +35,7 @@
...
@@ -35,6 +35,7 @@
<module>
springcloud-service-feign
</module>
<module>
springcloud-service-feign
</module>
<module>
springcloud-service-zuul
</module>
<module>
springcloud-service-zuul
</module>
<module>
springcloud-service-consul
</module>
<module>
springcloud-service-consul
</module>
<module>
springcloud-service-hystrix-dashboard
</module>
<module>
springcloud-service-eureka
</module>
<module>
springcloud-service-eureka
</module>
</modules>
</modules>
...
...
springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java
浏览文件 @
73476c30
...
@@ -6,6 +6,16 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
...
@@ -6,6 +6,16 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
/**
*
*
* http://localhost:8081/feign/1
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 16:13
*/
@EnableCircuitBreaker
@EnableCircuitBreaker
@EnableFeignClients
@EnableFeignClients
@EnableEurekaClient
@EnableEurekaClient
...
...
springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java
浏览文件 @
73476c30
...
@@ -7,7 +7,6 @@ import java.io.Serializable;
...
@@ -7,7 +7,6 @@ import java.io.Serializable;
@Component
@Component
public
class
ComputeClientHystrix
implements
ComputeClient
{
public
class
ComputeClientHystrix
implements
ComputeClient
{
@Override
@Override
public
Result
selectOne
(
Serializable
id
)
{
public
Result
selectOne
(
Serializable
id
)
{
return
Result
.
error
(
"feign方式请求 服务不可用 请求失败"
);
return
Result
.
error
(
"feign方式请求 服务不可用 请求失败"
);
...
...
springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java
浏览文件 @
73476c30
package
com.kwan.springcloud.service
;
package
com.kwan.springcloud.service
;
import
com.kwan.springcloud.api.ComputeClient
;
import
com.kwan.springcloud.response.Result
;
import
com.kwan.springcloud.response.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
@Slf4j
@Slf4j
@Service
@Service
public
class
FeignService
{
public
class
FeignService
{
/**
* http直接访问
*/
private
static
final
String
GOODS_URL
=
"http://localhost:9100/kwanGoodsInfo/"
;
/**
* eureka访问
*/
private
static
final
String
GOODS_URL_2
=
"http://goods-service/kwanGoodsInfo/"
;
@Autowired
@Autowired
RestTemplate
restTemplate
;
private
ComputeClient
computeClient
;
// @HystrixCommand(fallbackMethod = "addServiceFallback")
public
Result
addService
(
String
id
)
{
//调用远程的controller
public
Result
addService
(
String
id
)
{
return
restTemplate
.
getForEntity
(
GOODS_URL_2
+
id
,
Result
.
class
).
getBody
();
return
computeClient
.
selectOne
(
id
);
}
public
Result
addServiceFallback
(
String
id
)
{
log
.
info
(
"失败id={}"
,
id
);
return
Result
.
error
(
"ribbon方式请求 服务不可用 请求失败"
);
}
}
}
}
springcloud-service-feign/src/main/resources/application.yaml
浏览文件 @
73476c30
...
@@ -11,3 +11,7 @@ eureka:
...
@@ -11,3 +11,7 @@ eureka:
client
:
client
:
service-url
:
service-url
:
defaultZone
:
http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
defaultZone
:
http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
feign
:
hystrix
:
enabled
:
true
\ No newline at end of file
springcloud-service-hystrix-dashboard/pom.xml
0 → 100644
浏览文件 @
73476c30
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
com.kwan.springcloud
</groupId>
<artifactId>
springcloud-service-parent
</artifactId>
<version>
1.0.0
</version>
</parent>
<artifactId>
springcloud-service-hystrix-dashboard
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<name>
springcloud-service-hystrix-dashboard
</name>
<properties>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<!--spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-hystrix-dashboard
</artifactId>
</dependency>
<!--springboot开发自动热部署-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java
0 → 100644
浏览文件 @
73476c30
package
com.kwan.springcloud
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard
;
/**
*
* http://localhost:9909/hystrix
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 15:33
*/
@EnableHystrixDashboard
@SpringBootApplication
public
class
HystrixDashboardApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
HystrixDashboardApplication
.
class
,
args
);
}
}
springcloud-service-hystrix-dashboard/src/main/resources/application.yaml
0 → 100644
浏览文件 @
73476c30
#端口号
server
:
port
:
9909
spring
:
application
:
name
:
hystrix-dashboard-service
#服务名称
\ No newline at end of file
springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java
0 → 100644
浏览文件 @
73476c30
package
com.kwan.springcloud
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
@SpringBootTest
class
HystrixDashboardApplicationTests
{
@Test
void
contextLoads
()
{
}
}
springcloud-service-portal/pom.xml
浏览文件 @
73476c30
...
@@ -29,6 +29,17 @@
...
@@ -29,6 +29,17 @@
<groupId>
org.springframework.cloud
</groupId>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-openfeign-core
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-hystrix
</artifactId>
<version>
1.4.7.RELEASE
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java
浏览文件 @
73476c30
...
@@ -3,8 +3,17 @@ package com.kwan.springcloud;
...
@@ -3,8 +3,17 @@ package com.kwan.springcloud;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.netflix.hystrix.EnableHystrix
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
/**
*
* http://localhost:8080/portal/hystrix/1
*@version : 2.2.0
*@author : qinyingjie
*@date : 2023/1/14 16:07
*/
@EnableFeignClients
@EnableHystrix
@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
@SpringBootApplication
public
class
PortalApplication
{
public
class
PortalApplication
{
...
...
springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java
浏览文件 @
73476c30
...
@@ -24,6 +24,17 @@ public class PortalController {
...
@@ -24,6 +24,17 @@ public class PortalController {
@Autowired
@Autowired
private
PortalService
portalService
;
private
PortalService
portalService
;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping
(
value
=
"/hystrix/{id}"
,
produces
=
MediaType
.
APPLICATION_PROBLEM_JSON_VALUE
)
public
Result
hystrix
(
@PathVariable
String
id
)
{
return
portalService
.
hystrix
(
id
);
}
/**
/**
* 通过主键查询单条数据
* 通过主键查询单条数据
*
*
...
@@ -40,4 +51,6 @@ public class PortalController {
...
@@ -40,4 +51,6 @@ public class PortalController {
public
Result
test
()
{
public
Result
test
()
{
return
Result
.
ok
(
"portal 访问成功"
);
return
Result
.
ok
(
"portal 访问成功"
);
}
}
}
}
\ No newline at end of file
springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java
浏览文件 @
73476c30
package
com.kwan.springcloud.service
;
package
com.kwan.springcloud.service
;
import
com.kwan.springcloud.response.Result
;
import
com.kwan.springcloud.response.Result
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
...
@@ -9,26 +10,42 @@ import org.springframework.web.client.RestTemplate;
...
@@ -9,26 +10,42 @@ import org.springframework.web.client.RestTemplate;
@Slf4j
@Slf4j
@Service
@Service
public
class
PortalService
{
public
class
PortalService
{
/**
/**
* http直接访问
* http直接访问
*/
*/
private
static
final
String
GOODS_URL
=
"http://localhost:9100/kwanGoodsInfo/"
;
private
static
final
String
GOODS_URL
=
"http://localhost:9100/kwanGoodsInfo/"
;
/**
/**
* eureka访问
* eureka访问
*/
*/
private
static
final
String
GOODS_URL_2
=
"http://goods-service/kwanGoodsInfo/"
;
private
static
final
String
GOODS_URL_2
=
"http://goods-service/kwanGoodsInfo/"
;
@Autowired
@Autowired
RestTemplate
restTemplate
;
RestTemplate
restTemplate
;
public
Result
addService
(
String
id
)
{
public
Result
addService
(
String
id
)
{
//调用远程的controller
//调用远程的controller
return
restTemplate
.
getForEntity
(
GOODS_URL_2
+
id
,
Result
.
class
).
getBody
();
return
restTemplate
.
getForEntity
(
GOODS_URL_2
+
id
,
Result
.
class
).
getBody
();
}
}
@HystrixCommand
(
fallbackMethod
=
"addServiceFallback"
)
public
Result
hystrix
(
String
id
)
{
//1·服务超时,会降级
try
{
Thread
.
sleep
(
2000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
//2.服务异常,会降级
String
str
=
null
;
if
(
str
==
null
)
{
throw
new
RuntimeException
(
"服务异常了."
);
}
return
restTemplate
.
getForEntity
(
GOODS_URL_2
+
id
,
Result
.
class
).
getBody
();
}
public
Result
addServiceFallback
(
String
id
)
{
log
.
info
(
"失败id={}"
,
id
);
// return Result.error("请求超时 服务不可用 请求失败");
return
Result
.
error
(
"请求异常 服务不可用 请求失败"
);
}
}
}
springcloud-service-portal/src/main/resources/application.yaml
浏览文件 @
73476c30
...
@@ -11,3 +11,15 @@ eureka:
...
@@ -11,3 +11,15 @@ eureka:
client
:
client
:
service-url
:
service-url
:
defaultZone
:
http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
defaultZone
:
http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/
feign.hystrix.enabled=true
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMi
1liseconds=5000
如果hystrix.command.default.execution.timeout.enabled为true,则会有两
个执行方法超时的配置,一个就是ribbon的ReadTimeout,一个就是熔断器
I
hystrix的timeoutInMilliseconds,此时谁的值小谁生效;
如果hystrix.command.default.execution.timeout.enabled为false,则熔断
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录