Start Nacos console
Nacos GitHub official address: https://github.com/alibaba/nacos
- Download the package from the address and extract it to a local directory
- cd into the bin directory and run
startup.cmd -m standalone - Access the console address, default username and password
nacos
Console address: http://localhost:8848/nacos
Register service with Nacos
- Add the spring-cloud-alibaba management dependency in the parent project
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
- Add the Nacos client dependency in the child module
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- Modify the configuraton file
spring:
cloud:
nacos:
server-addr: localhost:8848
Nacos service classification storage model
- Set the instance cluster attribute
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ # Cluster name
NacosRule load balancing
- Configuration file method
userservice: # Microservice name to configure
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
NacosRuleprioritizes instances in the same cluster, then uses random load balancing after determining the instance list.
- Code method: Define a new IRule:
@Bean
public IRule nacosRule(){
return new NacosRule();
}
Nacos console can also set instance weights
Environment isolation ---- namespace
- Create a new namespace in the Nacos console
- Modify the configuratoin file
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: SH # Cluster name
namespace: 16035e26-9a58-403c-9308-083325c0f8ee # Namespace
Group ---- group
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: SH # Cluster name
namespace: 16035e26-9a58-403c-9308-083325c0f8ee # Namespace
group: xiaoyuxia # Group
Services with different
namespaceorgroupcannot communicate with each other.
Ephemeral instance
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: SH # Cluster name
namespace: 16035e26-9a58-403c-9308-083325c0f8ee # Namespace
ephemeral: false # Whether it is an ephemeral instance