Compare commits
No commits in common. "c5fd71b542e1c0e96543ea1f4538ecda8ca35df1" and "e973fea5f3ce804e84641722c12209cbf502cbcb" have entirely different histories.
c5fd71b542
...
e973fea5f3
3
.env
3
.env
@ -7,8 +7,7 @@ NACOS_HOST=
|
||||
NACOS_PORT=8848
|
||||
# 命名空间,用默认的public的话这儿就不写
|
||||
NACOS_NAMESPACE=
|
||||
# 除本文件以外的其他托管到Nacos的配置文件,如果有多个则以半角分号`;`隔开
|
||||
# Ps. 后面可以把NACOS_*和系统启动相关的留着,其他的全部托管到Nacos
|
||||
# 除本文件以外的其他托管到Nacos的配置文件,如果有多个则以半角分号`;`隔开 Ps. 后面可以把NACOS_*和系统启动相关的留着,其他的全部托管到Nacos
|
||||
NACOS_CONFIG_NAME=gateway.yml
|
||||
# 系统启动配置
|
||||
# Jaeger链路追踪
|
||||
|
@ -6,9 +6,8 @@ import (
|
||||
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
|
||||
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
|
||||
"github.com/nacos-group/nacos-sdk-go/vo"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -60,17 +59,33 @@ func (n nacosClient) Remote(ctx *gin.Context) {
|
||||
apiHost = fmt.Sprintf("%s:%v", instance.Ip, instance.Port)
|
||||
}
|
||||
|
||||
u := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: apiHost,
|
||||
// 组装内部接口
|
||||
apiUrl := fmt.Sprintf("http://%v%v", apiHost, uri)
|
||||
// 创建Request对象
|
||||
req, err := http.NewRequest(ctx.Request.Method, apiUrl, ctx.Request.Body)
|
||||
if err != nil {
|
||||
Log.Error("请求异常: %v\n", err)
|
||||
ctx.String(http.StatusBadGateway, "请求异常: %v", err.Error())
|
||||
return
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
ctx.Request.Header.Add("From", "internal")
|
||||
ctx.Request.Header.Add("uber-trace-id", ctx.GetString("UberTraceId"))
|
||||
// 重写错误回调
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
Log.Error("代理转发错误: %v", err.Error())
|
||||
ctx.String(http.StatusInternalServerError, "系统内部错误")
|
||||
// 填充Form参数(大概率用不上)
|
||||
req.Form = ctx.Request.Form
|
||||
// 填充链路追踪标记
|
||||
req.Header.Add("uber-trace-id", ctx.GetString("UberTraceId"))
|
||||
// 请求ID
|
||||
req.Header.Add("X-Request-Id", ctx.GetString("X-Request-Id"))
|
||||
// 设置来源
|
||||
req.Header.Add("From", "internal")
|
||||
// 发起请求
|
||||
client := &http.Client{}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
Log.Error("请求异常: %v\n", err)
|
||||
ctx.String(http.StatusBadGateway, "请求异常:%v", err.Error())
|
||||
return
|
||||
}
|
||||
proxy.ServeHTTP(ctx.Writer, ctx.Request)
|
||||
// 返回结果到前端
|
||||
sss, _ := ioutil.ReadAll(response.Body)
|
||||
Log.Debug(string(sss))
|
||||
ctx.String(response.StatusCode, string(sss))
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func initNacos() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ip := config.AppInfo.AppName
|
||||
ip := "gateway"
|
||||
if ips := utils.GetIps(); ips != nil {
|
||||
ip = ips[0]
|
||||
}
|
||||
@ -45,7 +45,7 @@ func initNacos() {
|
||||
Weight: 10,
|
||||
Enable: true,
|
||||
Healthy: true,
|
||||
ServiceName: config.AppInfo.AppName,
|
||||
ServiceName: "gateway",
|
||||
Ephemeral: true,
|
||||
})
|
||||
core.Log.Debug("Nacos注册结果: %v", success)
|
||||
@ -80,7 +80,7 @@ func initNacos() {
|
||||
|
||||
// 读取初始配置
|
||||
core.NacosClient = core.NewNacosClient(client, configClient)
|
||||
configStr, err := configClient.GetConfig(vo.ConfigParam{DataId: config.NacosConfig.CenterConfigName, Group: "DEFAULT_GROUP"})
|
||||
configStr, err := configClient.GetConfig(vo.ConfigParam{DataId: "gateway.yml", Group: "DEFAULT_GROUP"})
|
||||
if err != nil {
|
||||
core.Log.Panic("读取配置文件错误: %v", err.Error())
|
||||
}
|
||||
|
18
main.go
18
main.go
@ -19,5 +19,23 @@ func main() {
|
||||
|
||||
app.Any("/*action", core.NacosClient.Remote)
|
||||
|
||||
//app.GET("/hello", func(context *gin.Context) {
|
||||
// dd := context.GetString("UberTraceId")
|
||||
// //log.Println("数据是否存在: ", boo)
|
||||
// //if boo {
|
||||
// // log.Println(dd)
|
||||
// //}
|
||||
// context.String(http.StatusOK, get("get", context.Request.RequestURI, context.GetString("X-Request-Id"), dd))
|
||||
//})
|
||||
//
|
||||
//app.POST("/hello", func(context *gin.Context) {
|
||||
// context.String(http.StatusOK, post(context.Request.RequestURI))
|
||||
//})
|
||||
//
|
||||
//app.GET("/ip", func(context *gin.Context) {
|
||||
// dd := context.GetString("UberTraceId")
|
||||
// context.String(http.StatusOK, get("get", context.Request.RequestURI, context.GetString("X-Request-Id"), dd))
|
||||
//})
|
||||
|
||||
_ = app.Run(fmt.Sprintf(":%v", config.AppInfo.Port))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user