Nacos是国内产品,中文文档比较丰富,而且同时具备配置管理、服务注册与发现功能
1. Docker配置
1.1. 拉取Nacos镜像
nacosxxx.tar
是nacos的镜像文件,nacos文件夹下是nacos配置文件
其中的nacos/custom.env
文件中,有一个MYSQL_SERVICE_HOST
也就是mysql地址,需要修改为虚拟机IP地址
1
| docker load -i nacosxxx.tar
|
1.2. 运行Nacos容器
1 2 3 4 5 6 7 8
| docker run -d \ --name nacos \ --env-file ./nacos/custom.env \ -p 8848:8848 \ -p 9848:9848 \ -p 9849:9849 \ --restart=always \ nacos/nacos-server:vxxx
|
1.3. 访问Nacos
浏览器访问http://虚拟机IP:8848/nacos/
,默认账号密码为nacos/nacos
2. 服务注册
2.1. 添加依赖
在父工程需要添加Spring Cloud及Spring Cloud Alibaba的版本约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency>
|
并定义nacos-client的版本
1 2 3 4 5
| <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.4.0</version> </dependency>
|
在服务工程的pom.xml
中添加nacos-client的依赖
1 2 3 4 5
| <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
|
2.2. 配置nacos
在服务工程的application.yml
中添加nacos的配置
1 2 3 4 5 6
| spring: application: name: xxxx-service cloud: nacos: server-addr: 虚拟机IP:8848
|
2.3. 启动服务
显示以下日志为注册成功
1
| Success to connect to server [192.168.101.68:8848] on start up, connectionId ...
|
3. 服务发现
3.1. 添加依赖
由于还需要负载均衡,在服务工程的pom.xml
中添加SpringCloud提供的LoadBalancer依赖
1 2 3 4 5 6 7 8 9 10
| <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>
|
3.2. 配置nacos地址
在服务工程的application.yml
中添加nacos的配置
1 2 3 4
| spring: cloud: nacos: server-addr: 虚拟机IP:8848
|
3.3. 调用其他服务
任何一个微服务都可以调用别人,也可以被别人调用,即可以是调用者,也可以是提供者
3.3.1. 方法一
见OpenFeign
篇,后续方法一个比一个麻烦
3.3.2. 方法二
调用者工程Config添加RemoteCallConfig
类,使用@LoadBalanced
注解标识RestTemplate
1 2 3 4 5 6 7 8
| @Configuration public class RemoteCallConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
|
调用者工程的Service使用RestTemplate
调用提供者
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Service public class RemoteCallService { @Autowired private RestTemplate restTemplate;
public String call() { return restTemplate.exchange("http://xxxx-service/xxxx?ids={ids}", HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, CollUtils.join(ids, ",")); } }
|
3.3.3. 方法三
使用DiscoveryClient工具实现服务发现,SpringCloud已经帮我们自动装配,我们可以直接注入使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Service public class RemoteCallService { @Autowired private DiscoveryClient discoveryClient;
public String call() { List<ServiceInstance> instances = discoveryClient.getInstances("xxxx-service"); if (CollUtils.isEmpty(instances)) { return null; } ServiceInstance instance = instances.get(0); return restTemplate.exchange("http://" + instance.getHost() + ":" + instance.getPort() + "/xxxx?ids={ids}", HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}, CollUtils.join(ids, ",")); } }
|