继上文:spring的整合分布式配置中心(ACM diamond nacos Apollo)
目录
注:本文篇幅有点长,所以建议各位下载源码学习。(如需要请收藏!转载请声明来源,谢谢!)
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
注:本文篇幅有点长,所以建议各位下载源码学习。(如需要请收藏!转载请声明来源,谢谢!)
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
nacos服务器配置
先下载nacos-servers:https://github.com/alibaba/nacos/releases
这里以windows为准!
解压进入\nacos\bin\运行命令如下:
startup.cmd -m standalone
启动如下:
默认用户名称密码 nacos/nacos
配置内容
java读取nacos
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
import java.util.concurrent.Executor;
public class ConfigExample {
/**
*
* 功能描述: 测试
*
* @param:
* @return:
* @auther: csh
* @date: 2020/11/24 18:04
*/
public static void main(String[] args) throws NacosException, InterruptedException {
String serverAddr = "localhost";
String dataId = "hong";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
configService.addListener(dataId, group, new Listener() {
public void receiveConfigInfo(String configInfo) {
System.out.println("recieve:" + configInfo);
}
public Executor getExecutor() {
return null;
}
});
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
System.out.println(isPublishOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
boolean isRemoveOk = configService.removeConfig(dataId, group);
System.out.println(isRemoveOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
Thread.sleep(300000);
}
}
结果
测试id123测试专用!
true
recieve:content
content
true
recieve:null
null
spring整合Nacos
基础配置
spring版本:3.2.18.RELEASE
jdk:1.8
service-api:3.x
代码实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring_config_center</artifactId>
<groupId>com.hong</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hong</groupId>
<artifactId>spring_nacos_config</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<logback.version>1.2.3</logback.version>
<over-slf4j.version>1.7.25</over-slf4j.version>
<spring.version>3.2.18.RELEASE</spring.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<slf4j.version>1.7.12</slf4j.version>
<servlet-api.version>3.0.1</servlet-api.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.3.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<artifactId>javax.annotation-api</artifactId>
<groupId>javax.annotation</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
nacos.xml
<?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:annotation-driven>
<!--指定nacos配置地址-->
<nacos:global-properties server-addr="localhost:8848"/>
<!--指定dataId,group-id, 是否是自动刷新-->
<nacos:property-source data-id="hong" group-id="DEFAULT_GROUP" auto-refreshed="true"/>
</beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<import resource="nacos.xml" />
<!-- Spring MVC Annotation-Driven -->
<mvc:annotation-driven/>
<!-- Spring Context Annotation-Driven -->
<context:annotation-config/>
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.hong"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 后缀 -->
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html"/>
</bean>
</beans>
其他配置请参考项目~
com.hong.controller.ConfigController
@Controller
@RequestMapping("config")
public class ConfigController {
//获取nacos上面的配置信息
@NacosValue(value = "${test}", autoRefreshed = true)
private String val;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public String get() throws NacosException {
return val;
}
}
关于配置参考:
tomcat配置
修改
结果
再改为:hong123456 再查询
再启动一个节点:8089
再改为:13456454771111
可以发现spring整合nacos非常方便,当然,nacos的功能也非常强大,具体可以阅读官方文档。
springboot 整合nacos
参考:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hong.springboot</groupId>
<artifactId>springboot_all</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>com.hong.springboot</groupId>
<artifactId>springboot_nacos_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_nacos_client</name>
<description>springboot??nacos</description>
<properties>
<java.version>1.8</java.version>
<nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#端口
server.port=8081
#dataId
dataId=hong
#nacos配置
nacos.config.server-addr=127.0.0.1:8848
com.hong.springboot.config.CommonConfig
package com.hong.springboot.config;
import lombok.Data;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author: csh
* @Date: 2020/11/26 11:40
* @Description:
*/
@Component
@PropertySource(value = "application.properties")
@Data
public class CommonConfig {
//dataId
public String dataId;
}
com.hong.springboot.controller
package com.hong.springboot.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Auther: csh
* @Date: 2020/8/18 17:36
* @Description:获取配置
*/
@Controller
@RequestMapping("/")
@Slf4j
public class IndexController {
@NacosValue(value = "${test}", autoRefreshed = true)
private String hong;
@RequestMapping
public String index() {
log.info("首页");
return "index";
}
@ResponseBody
@GetMapping("getConfig")
public String getConfig() {
return hong;
}
}
启动
beanName='application', bean='null', beanType='class com.hong.springboot.Application', annotatedElement='null', xmlResource='null', nacosProperties='{serverAddr=127.0.0.1:8848, encode=UTF-8}', nacosPropertiesAttributes='{accessKey=${nacos.access-key:}, clusterName=${nacos.cluster-name:}, contextPath=${nacos.context-path:}, encode=${nacos.encode:UTF-8}, endpoint=${nacos.endpoint:}, namespace=${nacos.namespace:}, secretKey=${nacos.secret-key:}, serverAddr=${nacos.server-addr:}}', source='org.springframework.core.type.StandardAnnotationMetadata@6e535154', timestamp='1606370966372'
请求
再将原来的springmvc 8089启动
将内容再次修改为:12345678910
发现真的是分布式同步了,而且都是1秒钟内完成的,给力!
最后
不管是diamond、apollo、nacos或者其它分布式配置框架,都很好的解决一块需要动态配置的问题,当然这里推荐用nacos不仅可以作为分布配置中心也可以作为分布式注册中心,并且功能简单实用,没有apollo学习成本那么高,api和相关文档也比diamond丰富得n倍,当然这也不一样,鞋子合不合适要由你的脚才知道,外人永远只推荐自己觉得最好的!再会~
如果疑问请在下方留言私聊本人!
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
参考文档:
https://nacos.io/zh-cn/docs/quick-start.html