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
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
<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
|
启动项,别忘了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;
@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;
@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;
@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数组] 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>
|