Written at the beginning, this part of the blog is to record the learning process of the self-study project, not a detailed technical point research.
Reference documents
关于Nacos
Nacos Config Example
Nacos Discovery Example
SpringCloud Alibaba - Nacos
Registry (service discovery, registration)
-
Introduce
nacos-discovery
dependencies<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
-
Download nacos-server and start it after decompression:
startup.cmd
-
Add
@EnableDiscoveryClient
annotations to the service startup class application@MapperScan(value = "com.bcxtm.gulimall.product.dao") @EnableDiscoveryClient @SpringBootApplication public class GulimallProductApplication { public static void main(String[] args) { SpringApplication.run(GulimallProductApplication.class, args); } }
-
Configuration file configuration service registration server and service name
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 application: name: gulimall-product
-
Start the service, visit the browser
127.0.0.1:8848
, log in to nacos, and view the service registration information. (User name and password default: nacos)
Configuration Center (Dynamic Configuration Management) -
Introduce
nacos-config
dependencies<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
-
Create a
/resource/bootstrap.properties
configuration file and configure Nacos Config metadataspring.application.name=gulimall-coupon spring.cloud.nacos.config.server-addr=127.0.0.1:8848
-
Add configuration through nacos configuration center
It should be noted that after the dependency is introduced, the default configuration rules will be printed on the console when the service starts
c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[gulimall-coupon.properties] & group[DEFAULT_GROUP]
You can find: the default rule is: gulimall-coupon.properties; the group is: DEFAULT_GROUP
In nacos configuration center -> configuration management -> configuration list, add configuration:
-
In
resource/application.properties
the AdduserName
anduserAge
data.userName=BCXTM userAge=24
-
After completing the above two steps, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of the Spring Environment. Here we use
@Value
annotations to inject the corresponding configuration of the ControlleruserName
anduserAge
field, and add@RefreshScope
open dynamic refresh function.@RestController @RefreshScope @RequestMapping("coupon/coupon") public class CouponController { @Value("${userName}") private String userName; @Value("${userAge}") private String userAge; @RequestMapping(value = "/test") public R test() { return new R().put("userName", userName).put("userAge", userAge); } }
-
Start the project, use the
postman
send request to view the returned result{ "msg": "success", "code": 0, "userName": "Bcxtm", "userAge": "24" }
-
nacos
Modify the configuration information in the configuration center and click confirm to publish
-
Request the interface again and view the result
{ "msg": "success", "code": 0, "userName": "bcxtm666", "userAge": "24666" }
-
As you can see on the service console , the two data in the configuration have been refreshed
o.s.c.e.event.RefreshEventListener : Refresh keys changed: [userName, userAge]