Nacos的学习-创新互联
Nacos的学习
1、下载地址
名称栏目:Nacos的学习-创新互联
分享地址:http://ybzwz.com/article/dceieh.html
https://github.com/alibaba/nacos/releases/tag/2.1.1
成都创新互联服务项目包括泰和网站建设、泰和网站制作、泰和网页制作以及泰和网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,泰和网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到泰和省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
2、Spring与Nacos (1)新增一个配置(2)导包在bin目录中输入命令 startup.cmd -m standalone
输入localhost:8848/nacos
账号:nacos,密码:nacos
org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.ycz Nacos-study 0.0.1-SNAPSHOT Nacos-study Nacos-study 8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba.nacos nacos-spring-context 1.1.1
(3)配置package com.ycz.nacosstudy.config;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.ycz.nacosstudy")
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@NacosPropertySource(dataId = "test.properties" , autoRefreshed = true)
public class AppConfig {}
(4)servicepackage com.ycz.nacosstudy.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class UserService {@Value("${oracle.username}")
private String username;
@Value("${oracle.password}")
private String password;
public void test(){System.out.println(username);
System.out.println(password);
}
}
(5)测试package com.ycz.nacosstudy;
import com.ycz.nacosstudy.config.AppConfig;
import com.ycz.nacosstudy.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {public static void main(String[] args) {AnnotationConfigApplicationContext annotationConfigApplicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService =
annotationConfigApplicationContext.getBean(UserService.class);
userService.test();
}
}
结果:3、Springboot+Nacos
(1)导包org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.ycz Nacos-study 0.0.1-SNAPSHOT Nacos-study Nacos-study 8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba.boot nacos-config-spring-boot-starter 0.2.12
(2)配置nacos.config.context-path=127.0.0.1:8848
nacos.config.data-id=test.properties
nacos.config.auto-refresh=true
nacos.config.bootstrap.enable=true
(3)controllerpackage com.ycz.nacosstudy.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {@Value("${oracle.username}")
private String username;
@Value("${oracle.password}")
private String password;
@GetMapping("test")
public String test(){return username + password;
}
}
(4)测试 http://localhost:8080/test(5)将@Value改成@NacosValue现在如果nacos中配置修改的话,再刷新页面还是旧的
所以现在要改一下注解
package com.ycz.nacosstudy.Controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {@NacosValue(value = "${oracle.username}" , autoRefreshed = true)
private String username;
@NacosValue(value = "${oracle.password}" , autoRefreshed = true)
private String password;
@GetMapping("test")
public String test(){return username + password;
}
}
4、springcloud+nacos (1)导包再测试,是可以的
org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.ycz Nacos-study 0.0.1-SNAPSHOT Nacos-study Nacos-study 8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 2.2.8.RELEASE
(2)配置spring.application.name=test.properties
spring.cloud.nacos.config.context-path=127.0.0.1:8848
spring.cloud.nacos.config.refresh-enabled=true
(3)controllerpackage com.ycz.nacosstudy.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //自动刷新注解
public class UserController {@Value(value = "${oracle.username}")
private String username;
@Value(value = "${oracle.password}")
private String password;
@GetMapping("test")
public String test(){return username + password;
}
}
(4)测试(5)我们可以配置一个公共的配置类,这样就不用在每个类中都加@RefreshScopepackage com.ycz.nacosstudy.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@RefreshScope
@Component
public class CommonConfig {@Value(value = "${oracle.username}")
private String username;
@Value(value = "${oracle.password}")
private String password;
public String getUsername() {return username;
}
public void setUsername(String username) {this.username = username;
}
public String getPassword() {return password;
}
public void setPassword(String password) {this.password = password;
}
}
package com.ycz.nacosstudy.Controller;
import com.ycz.nacosstudy.config.CommonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {@Autowired
private CommonConfig config;
@GetMapping("test")
public String test(){return config.getUsername() + config.getPassword();
}
}
测试:
一般我们需要配置多套环境加上spring.profiles.active=prod就可以了
测试
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
名称栏目:Nacos的学习-创新互联
分享地址:http://ybzwz.com/article/dceieh.html