一、服务注册
在本地启动 navcos 服务器:
然后导入依赖:
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
然后是配置文件:
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置 Nacos 地址
management:
endpoints:
web:
exposure:
include: '*'
这里配置文件指定了访问的本地的 8848
端口。
主启动类,加上 @EnableDiscoveryClient
注解作为服务发现:
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class, args);
}
}
启动服务之后,访问 http://localhost:8848/nacos/#/serviceManagement
即可看到以下信息:
负载均衡
因为 nacos
集成了 ribbon
,所以支持负载均衡:
在消费者端,配置 ApplicationContextConfig
,在 Template 上加上 @LoadBalanced
注解开启负载均衡。
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
二、配置中心
pom 文件如下:
<dependencies>
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--web + actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般基础配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
bootstrap:
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
file-extension: yaml
# 根据公式得到地址为: nacos-config-client-dev.yaml
application:
spring:
profiles:
active: dev
业务类如下,记得加上 @RefreshScope
注解:
@RestController
@Slf4j
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
关于配置匹配,有一个公式:
${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
根据公式,我们这里的 Data Id 应该是:
nacos-config-client-dev.yaml
自带动态刷新,不需要我们通过消息总线发广播。
修改下 Nacos 中的 yaml 配置文件,再次调用查看配置的接口,就会发现配置已经刷新
Namespace + Group + Data ID 三者关系?
这三者的关系像是文件系统的分级目录一样,NameSpace
最大。
配置 GROUP
和 NameSpace
如下图所示:
config:
server-addr: localhost:8848
file-extension: yaml
namespace: asdasqe2e12312e1dasdad
group: TEST