Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
83b04df5
Commit
83b04df5
authored
Jun 08, 2018
by
George.Z.Lin
Committed by
nobodyiam
Jul 01, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.meta 支持多个host配置(逗号分割)
parent
08720578
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
1 deletion
+82
-1
MetaDomainConsts.java
...ava/com/ctrip/framework/apollo/core/MetaDomainConsts.java
+4
-1
NetUtil.java
...n/java/com/ctrip/framework/apollo/core/utils/NetUtil.java
+56
-0
NetUtilTest.java
...va/com/ctrip/framework/apollo/core/utils/NetUtilTest.java
+22
-0
No files found.
apollo-core/src/main/java/com/ctrip/framework/apollo/core/MetaDomainConsts.java
View file @
83b04df5
package
com
.
ctrip
.
framework
.
apollo
.
core
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.utils.NetUtil
;
import
com.ctrip.framework.apollo.core.utils.ResourceUtils
;
import
java.util.HashMap
;
...
...
@@ -38,6 +39,8 @@ public class MetaDomainConsts {
}
public
static
String
getDomain
(
Env
env
)
{
return
String
.
valueOf
(
domains
.
get
(
env
));
String
metaAddress
=
String
.
valueOf
(
domains
.
get
(
env
));
String
validAddress
=
NetUtil
.
getValidAddress
(
metaAddress
);
return
validAddress
;
}
}
apollo-core/src/main/java/com/ctrip/framework/apollo/core/utils/NetUtil.java
0 → 100644
View file @
83b04df5
package
com
.
ctrip
.
framework
.
apollo
.
core
.
utils
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
/**
* Created by gl49 on 2018/6/8.
*/
public
class
NetUtil
{
public
static
int
getUrlStatus
(
String
address
){
int
statusCode
=
0
;
try
{
URL
urlObj
=
new
URL
(
address
);
HttpURLConnection
oc
=
(
HttpURLConnection
)
urlObj
.
openConnection
();
oc
.
setUseCaches
(
false
);
oc
.
setConnectTimeout
(
5000
);
statusCode
=
oc
.
getResponseCode
();
if
(
200
==
statusCode
)
{
return
statusCode
;
}
}
catch
(
Exception
ignore
)
{
}
return
statusCode
;
}
public
static
boolean
checkUrl
(
String
address
){
int
status
=
getUrlStatus
(
address
);
if
(
0
==
status
){
//异常状态重试一次
status
=
getUrlStatus
(
address
);
}
if
(
200
==
status
){
return
true
;
}
return
false
;
}
public
static
String
getValidAddress
(
String
metaAddress
)
{
String
validAddress
=
null
;
String
[]
addressArr
=
changeAddressArr
(
metaAddress
);
for
(
String
address
:
addressArr
){
if
(
NetUtil
.
checkUrl
(
address
)){
validAddress
=
address
;
break
;
}
}
if
(
null
==
validAddress
){
throw
new
RuntimeException
(
"invalid meta address, please check $env_meta config!"
);
}
return
validAddress
;
}
private
static
String
[]
changeAddressArr
(
String
address
)
{
String
[]
addressArr
=
address
.
split
(
","
);
return
addressArr
;
}
}
apollo-core/src/test/java/com/ctrip/framework/apollo/core/utils/NetUtilTest.java
0 → 100644
View file @
83b04df5
package
com
.
ctrip
.
framework
.
apollo
.
core
.
utils
;
import
org.junit.Assert
;
import
org.junit.Test
;
/**
* Created by gl49 on 2018/6/8.
*/
public
class
NetUtilTest
{
@Test
public
void
testCheckUrl
(){
String
apolloUrl
=
"http://www.ctrip.com"
;
Assert
.
assertEquals
(
NetUtil
.
checkUrl
(
apolloUrl
),
true
);
}
@Test
public
void
testGetValidAddress
(){
String
addressStr
=
"http://www.ctrip.com,http://www.ctrip.com2"
;
String
address
=
NetUtil
.
getValidAddress
(
addressStr
);
Assert
.
assertEquals
(
address
,
"http://www.ctrip.com"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment