1.单机部署
1.1 官网
https://nacos.io/zh-cn/index.html
https://github.com/alibaba/Nacos
1.2.版本说明
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
1.3.下载地址
https://github.com/alibaba/nacos/releases/tag/2.2.0
1.4.安装
解压安装包,直接运行bin目录下,默认账号密码都是nacos,命令运行成功后直接访问http://localhost:8848/nacos,结果页面
startup.cmd -m standalone
2.注册中心
2.1 支付项目
1) 父POM
2.2.6.RELEASE以上版本集群报错,大概意思是找不到grpc的端口
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.6.RELEASE</version><type>pom</type><scope>import</scope>
</dependency>
2) POM
<!--nacos-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3) YML
server:port: 9001spring:application:name: nacos-payment-providercloud:nacos:discovery:server-addr: localhost:8848 #配置Nacos地址
4) 主程序
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {public static void main(String[] args) {SpringApplication.run(PaymentMain9001.class,args);}
}
2.2 订单项目
1) POM
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2) YML
spring:application:name: order-nacos-servicecloud:nacos:discovery:server-addr: localhost:8848
3) 主程序
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain {public static void main(String[] args) {SpringApplication.run(OrderNacosMain.class, args);}
}
4) 业务类
ApplicationContextBean
@Configuration
public class ApplicationContextBean {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();}
}
OrderNacosController
@RestController
public class OrderController {@Resourceprivate RestTemplate restTemplate;private String serverURL = "http://nacos-payment-service";@GetMapping(value = "/payment/get/{id}")public String paymentInfo(@PathVariable("id") Long id){return restTemplate.getForObject(serverURL+"/payment/get/"+id,String.class);}}
3.配置中心
3.1 POM
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
3.2 YOM
1) bootstrap.yml
此文件不写,yaml格式无法加载
spring:profiles:active: devcloud:nacos:discovery:server-addr: localhost:8848config:server-addr: localhost:8848file-extension: yaml
2) application.yml
server:port: 8001
spring:application:name: nacos-payment-service
3.3 业务类
@RestController
@RefreshScope
public class ConfigClientController
{@Value("${config.info}")private String configInfo;@GetMapping("/config/info")public String getConfigInfo() {return configInfo;}
}
通过 Spring Cloud 原生注解@RefreshScope
实现配置自动更新
3.4 Nacos中添加配置信息
1) 配置规格
${prefix}-${spring.profiles.active}.${file-extension}
2) 说明
prefix
默认为spring.application.name
的值,也可以通过配置项spring.cloud.nacos.config.prefix
来配置spring.profiles.active
即为当前环境对应的 profilefile-exetension
为配置内容的数据格式,可以通过配置项spring.cloud.nacos.config.file-extension
来配置,官网上说目前只支持properties
和yaml
类型,但我的版本只有properties
成功过
3) 配置新增
4.配置mysql
修改配置文件application.properties
spring.datasource.platform=mysql### Count of DB:
db.num=1### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.2.18:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
启动
startup.cmd
5.集群配置
修改配置配置文件cluster.conf
1) 单机配置:
192.168.2.93:8840
192.168.2.93:8850
192.168.2.93:8860
2) 多服务器
6.nginx
6.1 配置
upstream nacosserver{server 192.168.2.18:8840;server 192.168.2.18:8850;server 192.168.2.18:8860;}server {listen 8023;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm; proxy_pass http://nacosserver;}
}