preface
There are Spring Cloud1.x and Spring Cloud2.x versions in Spring Cloud. Currently, 2.x version is the mainstream use. Due to the suspension of service registration component Eureka, many companies are beginning to use other alternatives. And The Spring-Cloud-Alibaba of Ali has become a mainstream solution for the micro-service ecosystem. At present, my company is also planning to use This ali program for micro-service development. This article focuses on getting started with the service registry nacOS
concept
What is Nacos?
Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. The main features of Nacos (the ones I’m using so far) :
- Service discovery and Service Health Monitoring Nacos supports DNs-based and RPC-based service discovery. After a Service provider registers a Service using a native SDK, OpenAPI, or a separate Agent TODO, Service consumers can use DNS TODO or HTTP&API to find and discover services. Nacos provides real-time health checks on services to prevent requests from being sent to unhealthy hosts or service instances. Nacos supports health checks at the transport layer (PING or TCP) and the application layer (e.g. HTTP, MySQL, user-defined). Nacos provides two health check modes: Agent report mode and server active check mode for complex cloud environments and network topologies, such as VPCS and edge networks. Nacos also provides a unified health check dashboard to help you manage service availability and traffic based on health status
- Dynamically configured service
Nacos provides dynamic configuration of services, that is, dynamic modification of service configuration filesapplication.yml
To dynamically configure Spring Cloud Gateway dynamic routing.
Fast installation
- Install and run Nacos
Installation Tutorial ViewNacos websiteYou are advised to install the compressed package after compilation. Errors may occur if you compile the package yourself.
Run or close the software after installation:
Windows Directstartup.cmd
,shutdown.cmd
Execute scripts in Linuxsh startup.sh -m standalone
,sh shutdown.sh
- Inspection installation
accesshttp://ip:port/nacos
Open,nacos
Background home page The installation is successful
Integrate the Spring Cloud ecosystem
- Enabling service discovery
Service registration implementation: new project provider, add dependencyspring-cloud-starter-alibaba-nacos-discovery
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code
Add the @enableDiscoveryClient annotation to the startup class
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}Copy the code
Configure the application.yml file
Spring: Application: name: service-provider cloud: nacOS: Discovery: server-addr: 10.0.0.205:8848 Server: port: 9005 feign: sentinel: enabled:true
Copy the code
After the configuration is complete, start the provider and check the NACOS management background to check whether the service is successfully registered (detected).
- Start service Configuration
Spring Cloud Alibaba Nacos Config
是Spring Cloud Config Server
和Client
Alternatives to client and server conceptsSpring Environment and PropertySource
There are consistent abstractions in particularbootstrap
Phase, the configuration is loaded toSpring
In the environment. As the application moves through the deployment pipeline from development to test to production, you can manage the configuration between these environments and ensure that the application has everything it needs to run at migration time
The service configuration feature allows us to pass throughNacos
Modify the microservice profile in the background or write it here without restarting the project. Let’s take provider as an example to checkNacos
The power of service configuration.
Here we get the configuration filenanshen.name
The value of, for example
Add a service configuration to the Nacos configuration Management list:
After configuration, click publish! Since we have dynamically managed the service configuration on Nacos, we do not need to maintain the configuration file application.yml in the service. We can delete it, but there is a problem. How can my microservice find the configuration in Nacos? So we still need a bootstrap.properties configuration file to identify the configuration files on the server where Nacos is located.
ConfigurableApplicationContext
@Value
@Value
@Value
@RestController
public class TestController {
@Autowired
private ConfigurableApplicationContext configurableApplicationContext;
@GetMapping("/get_name")
public String getNanShenName(){
String username=configurableApplicationContext.getEnvironment().getProperty("user.name");
returnusername; }}Copy the code
As can be seen from the figure, the function of dynamically modifying configuration files is realized, which is of great significance to the gateway dynamic routing technology.
Sample source code update…