nacos入门

部署

这是全部了

nacos/nacos-server - Docker Image | Docker Hub

1
2
3
4
docker run --name nacos -e MODE=standalone -p 8849:8848 -d -e SPRING_
DATASOURCE_PLATFORM=mysql -e MYSQL_SERVICE_HOST=sdadgz.cn -e MYSQL_SERVICE_PORT=3307 -e MYSQL_SERVICE_DB_NAME=nacos -e M
YSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=sdadgz.cn -e JVM_XMS=840m -e JVM_XMX=840m -e TZ="Asia/Shanghai" -e LC_A
LL=en.UTF-8 -v /www/www.sdadgz.cn/nacos/:/home/nacos/ --network website_net nacos/nacos-server:1.4.2

mysql配置

初始化数据库

去官网找RELEASE下载,在conf里找nacos-mysql.sql

注册与发现

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--    nacos discovery   -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

yaml配置

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 8000
spring:
application:
name: demo
cloud:
nacos:
server-addr: sdadgz.cn:8849
username: nacos
password: sdadgz.cn
discovery:
namespace: 85198eed-65a8-4c00-9547-76fa3d77e973 # 手动去创建一个命名空间,name用作查看,id用作这里,手动创建多少有点不讲究啊,官网有api

启动项,别忘了soringboot依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.sdadgz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* @author sdadgz
* @since 2023/2/12
*/
@SpringBootApplication
@EnableFeignClients // 重中之重
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

feign包,来自其他服务的接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package cn.sdadgz.feign;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author sdadgz
* @since 2023/2/13
*/

@FeignClient(name = "demo-02",path = "/test")
public interface TestService {

@GetMapping("/m")
String m();

}

controller包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cn.sdadgz.controller;

import cn.sdadgz.feign.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
* @author sdadgz
* @since 2023/2/12
*/

@RestController
@RequestMapping("/test")
public class TestController {

// 其他服务
@Resource
private TestService testService;

@GetMapping("/hello")
public String test(){
// 就像自己人一样调用
String m = testService.m();
return "hello world:" + m;
}

}

配置中心

依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

bootstrap.yaml,别自己造名字,别仍application里

config命名规范 *.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
nacos:
server-addr: sdadgz.cn:8849
username: nacos
password: sdadgz.cn
config:
namespace: test
file-extension: yaml # 重中之重
shared-configs: [config数组] # 低优先级 d进去自己看格式
extension-configs: [config数组] # 高优先级
application:
name: config

application.yaml

1
2
3
4
5
6
7
server:
port: 9000
spring:
cloud:
nacos:
discovery:
namespace: test

注解

  • @Value(“${[my.config]}”) // 配置中心取值
  • @RefreshScope // 让上面的值实时同步

问题

nacos负载均衡报错

自某个版本之后。。。反正需要手动加个依赖了,其他的照官网抄就完事了

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

nacos配置中心启动报错

同上,他妈的

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>