1. Why is it called Nacos:
The first four letters are the first two letters of Naming and Configuration, and the last s is Service.
Nacos is a combination of registration center + configuration center
Nacos supports DNS-based and RPC-based service discovery. After the service provider uses the native SDK, OpenAPI, or an independent Agent TODO to register the Service, the service consumer can use DNS TODO or HTTP&API to find and discover the service.
Nacos provides real-time health checks on services to prevent sending requests to unhealthy hosts or service instances. Nacos supports health checks at the transport layer (PING or TCP) and application layer (such as HTTP, MySQL, user-defined). For the health check of services in complex cloud environments and network topology environments (such as VPC, edge networks, etc.), Nacos provides two health check modes: agent reporting mode and server-side active detection. Nacos also provides a unified health check dashboard to help you manage service availability and traffic based on your health status. java project www.fhadmin.org
Insert picture description here
Install and run nacos
Install nacos on docker
Pull the nacos mirror:
docker pull nacos/nacos-server
Run nacos:
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server
Enter in the browser: http://ip address:8848/nacos/ The
account and password are both nacos.
Insert picture description here
Insert picture description here
Demonstration of Nacos as a service registry
- New module cloudalibaba-provider-payment9001
- pom
<dependencies> <!--SpringCloud Alibaba nacos--> <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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
- marl
server: port: 9001 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: 120.92.164.250:8848 #Configured Nacos address (write localhost:8848 for this machine, write IP address for the server) management: endpoints: web: exposure: include: '*'
- Main start class
//java project www.fhadmin.org @EnableDiscoveryClient @SpringBootApplication public class PaymentMain9001 { public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class, args); } }
- Create a new controller package, and create a new PaymentController under the package
//java project www.fhadmin.org @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping("/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id){ return "nacos registry, serverPort: " + serverPort + "\t id: " + id; } }
- Start 9001
Insert picture description here
Consumer (integrated with Feign)
- 新建 cloudalibaba-consumer-nacos-order83
- pom
<dependencies> <!--SpringCloud Alibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Refer to the api universal package defined by yourself, you can use Payment to pay for Entity --> <dependency> <groupId>com.angenin.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
- L yml
server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: 120.92.164.250:8848 #Configured Nacos address (write localhost:8848 for this machine, write IP address for the server) #The name of the microservice that the consumer wants to access (service provider successfully registered into nacos) service-url: nacos-user-service: http://nacos-payment-provider
- Main start class
@EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class OrderNacosMain83 { public static void main(String[] args) { SpringApplication.run(OrderNacosMain83.class, args); } }
- Create a new feign package and add an interface PaymentFeignService under the package
@Component @FeignClient(value = "nacos-payment-provider") public interface PaymentFeignService { @GetMapping("/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id); }
- New controller
@RestController public class OrderNacosController { @Resource private PaymentFeignService paymentFeignService; @GetMapping("/consumer/payment/feign/nacos/{id}") public String paymentInfo2(@PathVariable("id") Integer id){ return paymentFeignService.getPayment(id); } }
start up