Article directory
1. Nacos introduction
Nacos is the acronym for Dynamic Naming and Configuration Service, a dynamic service discovery, configuration management and service management platform that is easier to build cloud-native applications.
2. nacos vs. Eureka
registration center | Spring Cloud Nacos | Spring Cloud Eureka |
---|---|---|
CAP model | Supports AP and CP models | AP model |
Client update service information | Use register+DNS-f+health check mode. The DNS-F client uses push/pull in listening mode to pull update information | The client regularly polls the server to obtain the IP information of other services and compares it. In contrast, the server is under greater pressure and has a greater delay |
Scalability | Using the Raft election algorithm has better performance, availability, and fault tolerance, and new nodes do not need to broadcast synchronization information with all nodes | Due to the use of broadcast synchronization information, the eureka cluster is under great pressure after the cluster exceeds 1000 machines |
Health check mode/method | Support server/client/close inspection mode, inspection methods include tcp, http, sql. Support for building your own health checker | The client sends an http heartbeat to the server |
load balancing | support | support |
Manual online and offline service mode | Via console page and API | By calling the API |
Synchronization across centers | support | not support |
k8s integration | support | not support |
group | Nacos can be used for group management according to business and environment | not support |
Weights | Nacos provides the weight setting function by default to adjust the bearing traffic pressure | not support |
manufacturers | alibaba | Netflix |
3. nacos installation
Official installation (too slow to download): Download the installation guide
docker install
#拉取镜像
docker pull nacos/nacos-server
#创建容器
docker run -d -p 8848:8848 -e MODE=standalone -e PREFER_HOST_MODE=hostname -v /usr/app/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties -v /usr/app/nacos/logs:/home/nacos/logs --restart always --name nacos nacos/nacos-server
Enter the background page address localhost::8848/nacos default account/password admin/admin
4. nacos registration center
We use nacos as the registration center, create two services, A and B, and register them in nacos, and use openfeign+ribbon to implement B service to call A service interface
4.1 Service A is registered in nacos
- A service pom dependencies (only add nacos dependencies, add other dependencies according to your needs)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- A service configuration file bootstrap.yml
Note: When the springboot service starts, it first looks for the bootstrap.yml configuration file, then looks for the application.yml, and finally merges the two contents
server:
port: 8099
#服务名称
spring:
application:
name: a-server
#ncaos地址
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 指定开放的端点路径, * 代表全部
management:
endpoints:
web:
exposure:
include: '*'
3. Create a service startup class, add@EnableDiscoveryClient
During the project startup process, it is found that the startup class needs to be placed in the third-level directory, and an error will be reported
@EnableDiscoveryClient
@SpringBootApplication
public class AServerApplication {
public static void main(String[] args) {
SpringApplication.run(AServerApplication .class, args);
}
}
4. Create a controller class.
We expose a service interface to the outside, use B service later, obtain the interface address through nacos, and finally call
@RestController
@RequestMapping("/Aserver")
public class AserverController {
@GetMapping("/getRandom")
private String getRandom() {
Random random = new Random();
int i = random.nextInt();
return "本次刷新值为:" + i;
}
}
}
}
4.2 Create B service and put it in nacos
1. Add dependencies
<!-- openfeign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud </groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. Create B service configuration file bootstrap.yml configuration
server:
port: 9093
spring:
application:
name: b-server
cloud:
nacos:
discovery:
server-addr: 39.105.151.241:8848
management:
endpoints:
web:
exposure:
include: '*'
3. Create B service startup class
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class BServerApplication {
public static void main(String[] args) {
SpringApplication.run(BServerApplication .class, args);
}
}
4. Create an openfeign interface
The @FeignClient annotation is the annotation name in openfeign and is the spring.application.name value in the A service configuration file.
@Component
@FeignClient(name = "a-server")
@RequestMapping("/provider")
public interface AserverService{
@GetMapping("/getRandom")
String providerCase();
}
5. Create the Controller class of B service and call the interface of A service
@RestController
public class AServerController {
@Autowired
private AserverService AserverService;
@GetMapping("getId")
public String getId() {
String s = AserverService.getRandom();
return "A服务返回结果"+s;
}
}
6. We start A and B projects and enter the nacos page to view the service list
7. Call http://localhost:9093/getId
5 Configuration Center
The dynamic configuration service allows you to centralize, externalize and dynamically manage the application configuration and service configuration of all environments. Dynamic configuration eliminates the need to redeploy applications and services when configuration changes, making configuration management more efficient and agile
Introduce dependency in A service
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
A service bootsrap.yml configuration file content
server:
port: 8099
spring:
application:
name: a-server
#ncaos地址
cloud:
nacos:
discovery:
server-addr: 39.105.151.241:8848
#配置中心的地址
config:
server-addr: 39.105.151.241:8848
file-extension: yml
# 指定开放的端点路径, * 代表全部
management:
endpoints:
web:
exposure:
include: '*'
#我们需要更新配置内容
config:
info: a_server_test_01
Create a test interface
The @RefreshScope annotation will automatically update the configuration file
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigInfoController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/getInfo")
public String getConfigInfo(){
return "配置内容:"+configInfo;
}
Start the A project and enter the configuration list
Call the getConfigInfo interface
to create a configuration file and publish it
Data Id is spring.application.name + configuration file suffix in A service configuration file
Call the getConfigInfo interface again to view the returned results