Skip to content

Commit

Permalink
完善config-web
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lee committed Oct 9, 2015
1 parent 00a8778 commit ef25a61
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 31 deletions.
11 changes: 2 additions & 9 deletions config-web/src/main/java/com/github/autoconf/entity/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public Config() {

public Config(String name, String profile, String content) {
this.content = content;
setName(name);
setProfile(profile);
this.name = name;
this.profile = profile;
}

public Long getId() {
Expand All @@ -97,7 +97,6 @@ public String getName() {

public void setName(String name) {
this.name = name;
updatePath();
}

@JsonIgnore
Expand All @@ -115,12 +114,6 @@ public String getProfile() {

public void setProfile(String profile) {
this.profile = profile;
updatePath();
}

//TODO: XXXX
private void updatePath() {
this.path = "/cms/" + name + "/" + profile;
}

public int getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface ConfigHistoryMapper {
@Select("SELECT * FROM config_history")
List<ConfigHistory> findAll();

@Insert("INSERT config_history SET config_id=#{configId}, version=#{version}, editor=#{editor}, name=#{name}, path=#{path}, content=#{content}")
@Insert("INSERT config_history SET config_id=#{configId}, version=#{version}, editor=#{editor}, name=#{name}, profile=#{profile}, path=#{path}, content=#{content}")
void insert(ConfigHistory history);

@Select("SELECT * FROM config_history WHERE id=#{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ public interface ConfigMapper {
@Delete("DELETE FROM config WHERE id=#{id}")
void deleteById(@Param("id") Long id);

@Insert("INSERT config SET editor=#{editor}, name=#{name}, profile=#{profile}, path=#{path}, content=#{content}")
@Insert("INSERT config SET editor=#{editor}, name=#{name}, profile=#{profile}, content=#{content}")
@Options(useGeneratedKeys = true)
void insertAndGetId(Config config);

@Update("UPDATE config SET editor=#{editor}, version=#{version}, content=#{content} WHERE id=#{id}")
void update(Config config);

@Update("UPDATE config SET path=#{path} WHERE id=#{id}")
void updatePath(@Param("path") String path, @Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ public void delete(Config config) {
mapper.deleteById(config.getId());
}
}

public void updatePath(String path, Long id) {
mapper.updatePath(path, id);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import com.github.autoconf.helper.ConfigHelper;
import com.github.autoconf.helper.ZookeeperUtil;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
Expand Down Expand Up @@ -40,6 +42,8 @@ public class PublishService {
private String authType = "digest";
@Value("${zookeeper.authentication}")
private String auth = "root:Password4Zookeeper";
@Value("${zookeeper.basePath}")
private String zkPath = "/cms/config";
private CuratorFramework client;
private List<ACL> defaultAclList = Lists.newArrayList();

Expand All @@ -62,7 +66,7 @@ void init() {
/**
* 发布配置,需要:备份原配置,保存新内容,发布到zookeeper。
*/
public void cpZookeeper(Config config, boolean scp) {
public void cpZookeeper(Config config) {
clearCache();

// 首先保存config_history备份
Expand All @@ -75,10 +79,12 @@ public void cpZookeeper(Config config, boolean scp) {
log.error("cannot insertBackup({})", backup, e);
}

if (!scp) {
return;
String path = config.getPath();
if (Strings.isNullOrEmpty(path)) {
path = ZKPaths.makePath(zkPath, config.getName(), config.getProfile());
config.setPath(path);
configService.updatePath(config.getPath(), config.getId());
}

try {
// 发布到zookeeper中
byte[] payload;
Expand All @@ -87,15 +93,18 @@ public void cpZookeeper(Config config, boolean scp) {
} else {
payload = ZookeeperUtil.newBytes(config.getContent());
}
ZookeeperUtil.ensure(client, config.getPath());
ZookeeperUtil.setData(client, config.getPath(), payload);
if (ZookeeperUtil.exists(client, path) != null) {
ZookeeperUtil.setData(client, path, payload);
} else {
ZookeeperUtil.create(client, path, payload);
}
// 设定路径权限
if (!Iterables.isEmpty(defaultAclList)) {
ZookeeperUtil.setACL(client, config.getPath(), defaultAclList);
ZookeeperUtil.setACL(client, path, defaultAclList);
}
} catch (Exception e) {
log.error("cannot publish to zookeeper, path={}", config.getPath(), e);
throw new RuntimeException("cannot publish to zookeeper, path=" + config.getPath() + ", " + e.getMessage());
log.error("cannot publish to zookeeper, path={}", path, e);
throw new RuntimeException("cannot publish to zookeeper, path=" + path + ", " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ public String createConfigAction(@Valid Config c, BindingResult bind, RedirectAt
}
c.setModifyTime(new Date(System.currentTimeMillis()));
c.setEditor(getCurrentUserName());
CharMatcher matcher = CharMatcher.anyOf(",; |");
try {
CharMatcher matcher = CharMatcher.anyOf(",; |");
if (matcher.matchesAnyOf(c.getProfile())) {
// 批量创建
for (String i : Splitter.on(matcher).trimResults().omitEmptyStrings().splitToList(c.getProfile())) {
for (String i : Splitter.on(matcher).trimResults().omitEmptyStrings().split(c.getProfile())) {
Config one = new Config();
one.setEditor(getCurrentUserName());
one.setName(c.getName());
one.setProfile(i);
one.setContent(c.getContent());
config.save(one);
publish.cpZookeeper(one, true);
publish.cpZookeeper(one);
}
} else {
config.save(c);
publish.cpZookeeper(c, true);
publish.cpZookeeper(c);
}
return "redirect:/?search=" + encodeURL(c.getName());
} catch (Exception e) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public String editConfigAction(@ModelAttribute("config") Config c, RedirectAttri
try {
c.setEditor(getCurrentUserName());
config.save(c);
publish.cpZookeeper(c, true);
publish.cpZookeeper(c);
r.addFlashAttribute("message", "修改成功");
} catch (Exception e) {
log.error("/edit/config/: {}", c, e);
Expand All @@ -152,7 +152,7 @@ public String recoverHistory(@RequestParam long id, RedirectAttributes r) {
c.setEditor(getCurrentUserName());
c.setContent(h.getContent());
config.save(c);
publish.cpZookeeper(c, true);
publish.cpZookeeper(c);
r.addFlashAttribute("message", "回滚版本为" + h.getVersion() + "的配置\"" + c.getName() + "\"成功");
} catch (Exception e) {
log.error("/recover/history/: {}", h, e);
Expand Down Expand Up @@ -198,13 +198,14 @@ public String replaceConfigContent(@Valid ReplaceRequest req, BindingResult resu
Set<Long> ids = Sets.newHashSet(req.getConfigIds());
try {
for (Config c : config.findAll()) {
if (!ids.contains(c.getId()))
if (!ids.contains(c.getId())) {
continue;
}
if (c.getContent().contains(req.getSrc())) {
c.setEditor(getCurrentUserName());
c.setContent(StringUtils.replace(c.getContent(), req.getSrc(), req.getDst()));
config.save(c);
publish.cpZookeeper(c, true);
publish.cpZookeeper(c);
log.info("{}(profile={}) replace [{}] -> [{}]", c.getName(), c.getProfile(), req.getSrc(), req.getDst());
}
}
Expand Down
4 changes: 2 additions & 2 deletions config-web/src/main/resources/jetbrick-template.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jetx.syntax.safecall=true
jetx.template.loaders = $loader
$loader = jetbrick.template.loader.ServletResourceLoader
$loader.root = /WEB-INF/templates/
$loader.reloadable = true
$loader.reloadable = false

jetx.compile.strategy = precompile
jetx.compile.path = /WEB-INF/jetx_classes
jetx.compile.debug = true
jetx.compile.debug = false

jetx.trim.leading.whitespaces = true
jetx.trim.directive.whitespaces = true
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="col-md-3 col-lg-3">
<input type="text" class="form-control" name="profile" id="profile" placeholder="必填" value='${config.profile?!"test"}'/>

<p class="help-block bg-info">批量创建:逗号分隔的多个组名,如:test,online</p>
<p class="help-block bg-info">批量创建:逗号分隔的多个组名</p>
</div>
<p class="help-block col-md-offset-2">如:online(正式),preview(预发布),test(测试环境)</p>
</div>
Expand Down

0 comments on commit ef25a61

Please sign in to comment.