点击这里,下载1.2.1版本
下载后解压,进入bin目录,打开cmd
http://localhost:8848/nacos/
用户名和密码都是nacos
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.hry.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置nacos地址
management:
endpoints:
web:
exposure:
include: "*"
package com.hry.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class, args);
}
}
package com.hry.springcloud.alibaba.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id){
return "nacos serverPort: "+serverPort+"\t id = "+id;
}
}
步骤与9001相同,省略(CV大法好)
启动9002,刷新8848
实例数由1变成2
分别测测能不能跑通
server:
port: 83
spring:
application:
name: nacos-payment-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置nacos地址
新建config包,新建配置类(可直接复制之前80模块的)
package com.hry.springcloud.alibaba.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 该配置类相当于SSM框架中 applicationContext.xml中
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
新建controller
package com.hry.springcloud.alibaba.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderController {
public static final String SERVER_URL="http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Long id){
return restTemplate.getForObject(SERVER_URL+"/payment/nacos/"+id,String.class);
}
}
nacos因为整合了ribbon因此自带负载均衡
刷新8848
访问83,验证负载均衡
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.hry.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
两个yml:application和bootstrap
application
spring:
profiles:
active: dev #开发环境
bootstrap
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
#nacos服务注册中心地址
server-addr: localhost:8848
config:
#nacos配置中心地址
server-addr: localhost:8848
file-extension: yml
package com.hry.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3377.class, args);
}
}
package com.hry.springcloud.alibaba.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 //支持Nacos动态刷新
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping(value = "/config/info")
public String getConfigInfo(){
return configInfo;
}
}
配置中的Data ID命名规则:
打开8848,新建配置
填写配置信息
完成后点击发布
返回
在配置管理中点编辑,编辑刚才的nacos-config-client-dev.yml。
version改成2然后发布
发布后刷新3377
目前Group相同,命名空间namespace是:public
修改application.yml
spring:
profiles:
active: test #测试环境
#active: dev #开发环境
切换namespace到dev,新建配置
这样找到的就是Namespace为dev的分组是DEV的dev.yml文件
官方参考文档
无论是关闭网页重新进入还是关机重启,Nacos的配置都不会丢失,那么肯定不是存储在内存了,查看官网介绍可知,Nacos内置了derby做持久化。
MySQL版本为5.6.5+
打开Nacos下的conf目录的nacos-mysql.sql
如果没有建库语句,首先自己建立数据库,然后运行下面的建表语句。
新建数据库nacos_config
然后运行刚才的nacos-mysql.sql
然后打开config下的application.properties
在配置文件末尾添加以下配置
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=username
db.password=password
保存后重启Nacos服务
刷新8848发现配置没了,证明我们已经成功切换了数据库为我们自己的MySQL之后添加配置就可以在我们的nacos_config库中看到了
1个Nginx+3个Nacos+1个MySQL
点击这里下载
下载后解压
进入conf,编辑nginx.conf
将conf下的cluster.conf.example复制一份,然后改成cluster.conf,并编辑
编辑后的内容
127.0.0.1:3333
127.0.0.1:4444
127.0.0.1:5555
保存
修改application.properties,将8848改为3333
复制两份nacos,分别改名为nacos1,nacos2
分别修改application.properties,改为4444,5555
启动三个nacos
稍等片刻,启动好了之后访问:http://localhost:1111/nacos/
登录,再测试一下配置能不能正常存在MySQL,新建配置
查询,OK
server-addr: localhost:1111 #换成Nginx的1111端口,做集群