Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
添加基于Nacos的动态配置相关代码
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinDai committed Aug 17, 2020
1 parent 5540011 commit add8a49
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- 基于Zookeeper实现的分布式锁
- 分布式ID(基于Leaf的Segment模式)
- 分布式事务(使用RocketMQ实现最终一致性)
- 基于Nacos的动态配置

## 工具类

Expand Down
21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@
<mybatis.version>3.4.6</mybatis.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
<mysql.version>8.0.16</mysql.version>
<nacos.version>0.3.6</nacos.version>
<perf4j.version>0.9.16</perf4j.version>
<poi.version>3.17</poi.version>
<rocketmq.version>4.3.0</rocketmq.version>
<servlet.version>3.1.0</servlet.version>
<sharding-sphere.version>4.0.0-RC2</sharding-sphere.version>
<slf4j.version>1.7.21</slf4j.version>
<spring.version>5.2.3.RELEASE</spring.version>
<spring.version>5.1.6.RELEASE</spring.version>
<spring.redis.version>2.0.12.RELEASE</spring.redis.version>
<xmemcached.version>2.4.3</xmemcached.version>
<zk.version>3.4.14</zk.version>
Expand Down Expand Up @@ -438,6 +439,24 @@
<version>${perf4j.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>${nacos.version}</version>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
5 changes: 5 additions & 0 deletions springmvc-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
<artifactId>perf4j</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.doodl6.springmvc.service.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Service;

/**
* nacos配置服务
*/
@Service
public class NacosConfigService {

@NacosValue(value = "${maxPageSize:20}", autoRefreshed = true)
private int maxPageSize;

public int getMaxPageSize() {
return maxPageSize;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.doodl6.springmvc.web.controller;

import com.doodl6.springmvc.service.config.NacosConfigService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* 配置控制类
*/
@RestController
@RequestMapping("/config")
public class ConfigController extends BaseController {

@Resource
private NacosConfigService nacosConfigService;

@GetMapping(value = "/getMaxPageSize")
public int getMaxPageSize() {
return nacosConfigService.getMaxPageSize();
}
}
2 changes: 1 addition & 1 deletion springmvc-web/src/main/resources/spring/spring-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.doodl6.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
<context:exclude-filter type="annotation" expression="javax.websocket.server.ServerEndpoint"/>
</context:component-scan>

Expand Down
1 change: 1 addition & 0 deletions springmvc-web/src/main/resources/spring/spring-mvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<context:component-scan base-package="com.doodl6.springmvc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
<context:include-filter type="annotation" expression="javax.websocket.server.ServerEndpoint"/>
</context:component-scan>

Expand Down
14 changes: 14 additions & 0 deletions springmvc-web/src/main/resources/spring/spring-nacos.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:nacos="http://nacos.io/schema/nacos"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nacos.io/schema/nacos http://nacos.io/schema/nacos.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--nacos配置,这里是使用配置文件的方式,这只是其中的一种方式-->
<!--开启注解-->
<nacos:annotation-driven/>
<!--指定nacos配置地址-->
<nacos:global-properties server-addr="127.0.0.1:8848"/>
<!--指定dataId,group-id, 是否是自动刷新-->
<nacos:property-source data-id="doodl6" group-id="spring-mvc" auto-refreshed="true"/>
</beans>
7 changes: 6 additions & 1 deletion springmvc-web/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
<!-- Spring Config Location -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
<param-value>
classpath:spring/spring-base.xml,
classpath:spring/spring-datasource.xml,
classpath:spring/spring-dubbo.xml,
classpath:spring/spring-nacos.xml
</param-value>
</context-param>

<context-param>
Expand Down

0 comments on commit add8a49

Please sign in to comment.