1.Nacos服务提供者注册
1.1 引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
1.2 配置application.yml
server:
port: 9001
spring:
application:
name: nacos-payment-provider #服务名字
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址
management:
endpoints:
web:
exposure:
include: '*'
1.3 配置主启动类
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class,args);
}
}
2.Nacos服务消费者注册和负载
2.1 引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.2 配置application.yml
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
2.3 配置主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain83 {
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain83.class,args);
}
}
2.4 配置RestTemplate,开启负载均衡
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //实现负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.5 业务类
@RestController
@Slf4j
public class OrderNacosController {
@Resource
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping("/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Long id){
return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
}
}
3.Nacos服务配置中心
3.1 引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.2 配置bootstrap.yml和application.yml
bootstrap.yml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
application.yml
spring:
profiles:
active: dev #表示开发环境
3.3 配置主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientMain3377.class,args);
}
}
3.4 业务类
@RestController
@RefreshScope //支持Nacos的动态刷新功能
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo(){
return configInfo;
}
}
4.Nacos持久化配置
4.1 执行nacos-mysql.sql
在nacos-server-XXX\nacos\conf下,找到nacos-mysql.sql,新建nacos_config数据库,执行nacos-mysql.sql
4.2 修改conf/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&serverTimezone=Asia/Shanghai
db.user=root
db.password=root
4.3 重启nacos服务,重新登录
5.Nacos集群
5.1 环境
linux安装nacos和nginx
Nacos 1.4.2 (注意版本,我第一次用的1.4.4,结果nacos登录报用户名或密码错误)
Nginx 1.12.2
5.2 Linux系统访问Windows(本机)的Navicat数据库
参考link前两步
5.3 修改nacos/conf/application.properties
跟4.2一样,只不过是把3306端口前的ip换成了windows系统的ip
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.153.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=Asia/Shanghai
db.user=root
db.password=root
5.4 配置nacos/conf/nacos.conf
-
复制出nacos.conf
cp cluster.conf.example nacos.conf
-
修改nacos.conf,192.168.153.131是linux系统的ip,可以用hostname -i查看
192.168.153.131:3333 192.168.153.131:4444 192.168.153.131:5555
5.5 修改nacos/bin/startup.sh
新增port的设置
绿色框里是新增的
5.6 修改/usr/local/nginx/conf/nginx.conf
记得开放1111端口
upstream cluster{
server 192.168.153.131:3333;
server 192.168.153.131:4444;
server 192.168.153.131:5555;
}
server {
listen 1111;
server_name 192.168.153.131;
location / {
proxy_pass http://cluster;
}
}
5.7 启动3个nacos服务
/nacos/bin下执行
./startup.sh -o 3333
./startup.sh -o 4444
./startup.sh -o 5555
ps -ef | grep nacos | grep -v grep | wc -l #查看nacos实例返回3
5.8 启动nginx
在/usr/local/nginx/sbin目录下执行
./nginx -c /usr/local/nginx/conf/nginx.conf