Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
d219a104
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d219a104
编写于
8月 09, 2020
作者:
A
Amit Garg
提交者:
Amit Garg
8月 09, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add logging in API Gateway
上级
1e90d0d6
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
57 addition
and
2 deletion
+57
-2
api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
...c/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
+22
-1
api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
...c/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
+23
-1
api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java
...java/com/iluwatar/image/microservice/ImageController.java
+6
-0
api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java
...java/com/iluwatar/price/microservice/PriceController.java
+6
-0
未找到文件。
api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
浏览文件 @
d219a104
...
...
@@ -23,11 +23,16 @@
package
com.iluwatar.api.gateway
;
import
static
org
.
slf4j
.
LoggerFactory
.
getLogger
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
org.slf4j.Logger
;
import
org.springframework.stereotype.Component
;
/**
...
...
@@ -35,6 +40,8 @@ import org.springframework.stereotype.Component;
*/
@Component
public
class
ImageClientImpl
implements
ImageClient
{
private
static
final
Logger
LOGGER
=
getLogger
(
ImageClientImpl
.
class
);
/**
* Makes a simple HTTP Get request to the Image microservice.
*
...
...
@@ -49,12 +56,26 @@ public class ImageClientImpl implements ImageClient {
.
build
();
try
{
LOGGER
.
info
(
"Sending request to fetch image path"
);
var
httpResponse
=
httpClient
.
send
(
httpGet
,
BodyHandlers
.
ofString
());
logResponse
(
httpResponse
);
return
httpResponse
.
body
();
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
(
);
LOGGER
.
error
(
"Failure occurred while getting image path"
,
e
);
}
return
null
;
}
private
void
logResponse
(
HttpResponse
<
String
>
httpResponse
)
{
if
(
isSuccessResponse
(
httpResponse
.
statusCode
()))
{
LOGGER
.
info
(
"Image path received successfully"
);
}
else
{
LOGGER
.
warn
(
"Image path request failed"
);
}
}
private
boolean
isSuccessResponse
(
int
responseCode
)
{
return
responseCode
>=
200
&&
responseCode
<=
299
;
}
}
api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
浏览文件 @
d219a104
...
...
@@ -23,18 +23,26 @@
package
com.iluwatar.api.gateway
;
import
static
org
.
slf4j
.
LoggerFactory
.
getLogger
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.http.HttpClient
;
import
java.net.http.HttpRequest
;
import
java.net.http.HttpResponse
;
import
java.net.http.HttpResponse.BodyHandlers
;
import
org.slf4j.Logger
;
import
org.springframework.stereotype.Component
;
/**
* An adapter to communicate with the Price microservice.
*/
@Component
public
class
PriceClientImpl
implements
PriceClient
{
private
static
final
Logger
LOGGER
=
getLogger
(
PriceClientImpl
.
class
);
/**
* Makes a simple HTTP Get request to the Price microservice.
*
...
...
@@ -49,12 +57,26 @@ public class PriceClientImpl implements PriceClient {
.
build
();
try
{
LOGGER
.
info
(
"Sending request to fetch price info"
);
var
httpResponse
=
httpClient
.
send
(
httpGet
,
BodyHandlers
.
ofString
());
logResponse
(
httpResponse
);
return
httpResponse
.
body
();
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
(
);
LOGGER
.
error
(
"Failure occurred while getting price info"
,
e
);
}
return
null
;
}
private
void
logResponse
(
HttpResponse
<
String
>
httpResponse
)
{
if
(
isSuccessResponse
(
httpResponse
.
statusCode
()))
{
LOGGER
.
info
(
"Price info received successfully"
);
}
else
{
LOGGER
.
warn
(
"Price info request failed"
);
}
}
private
boolean
isSuccessResponse
(
int
responseCode
)
{
return
responseCode
>=
200
&&
responseCode
<=
299
;
}
}
api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java
浏览文件 @
d219a104
...
...
@@ -23,15 +23,20 @@
package
com.iluwatar.image.microservice
;
import
static
org
.
slf4j
.
LoggerFactory
.
getLogger
;
import
org.slf4j.Logger
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* Exposes the Image microservice's endpoints.
*/
@RestController
public
class
ImageController
{
private
static
final
Logger
LOGGER
=
getLogger
(
ImageController
.
class
);
/**
* An endpoint for a user to retrieve an image path.
...
...
@@ -40,6 +45,7 @@ public class ImageController {
*/
@RequestMapping
(
value
=
"/image-path"
,
method
=
RequestMethod
.
GET
)
public
String
getImagePath
()
{
LOGGER
.
info
(
"Successfully found image path"
);
return
"/product-image.png"
;
}
}
api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java
浏览文件 @
d219a104
...
...
@@ -23,15 +23,20 @@
package
com.iluwatar.price.microservice
;
import
static
org
.
slf4j
.
LoggerFactory
.
getLogger
;
import
org.slf4j.Logger
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* Exposes the Price microservice's endpoints.
*/
@RestController
public
class
PriceController
{
private
static
final
Logger
LOGGER
=
getLogger
(
PriceController
.
class
);
/**
* An endpoint for a user to retrieve a product's price.
...
...
@@ -40,6 +45,7 @@ public class PriceController {
*/
@RequestMapping
(
value
=
"/price"
,
method
=
RequestMethod
.
GET
)
public
String
getPrice
()
{
LOGGER
.
info
(
"Successfully found price info"
);
return
"20"
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录