Nacos Introduction
Nacos committed to helping you discover, configure, and micro-management services. Nacos provides a set of simple-to-use set of features that help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.
Nacos help you more quickly and easily build, deliver and manage micro-service platform. Nacos is to build a "service" as the center of modern application architectures (such as micro-service paradigm, cloud native paradigm) service infrastructure.
The key characteristic of Nacos
- Service discovery and health monitoring services
- Dynamic configuration services, with a management interface, support for rich configuration dimensions.
- Dynamic DNS Service
- Service and metadata management
Nacos download and deploy
Official description document: Nacos Quick Start or download zip package, deploy download
Windows environment problems encountered during deployment summary
jdk version requirements
Must pay attention to, jdk version requires 64bit JDK 1.8+
Run error
Run startup.cmd, fleeting. Open startup.cmd script, add a pause so that the error will not end immediately easy to see the error message in the last line, then find the following error message:
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springfra
mework/boot/loader/PropertiesLauncher : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The cause of these problems is that I have some old projects on a computer using jdk 1.7, so the project is jdk1.7 and jdk1.8 crossed use. View version java -version output in cmd then there is 1.8.0_211 (only the highest output version)
solution
- Configuring java_home, increase jdk1.8 environment variables, how to add their own environment variables need to Baidu, the following posted my java environment variables
C:\Users\Administrator>set java_home
JAVA_HOME=D:\Program Files\Java\jdk1.7.0_71
C:\Users\Administrator>set java8_home
JAVA8_HOME=D:\Program Files\Java\jdk1.8.0_201
C:\Users\Administrator>
- Adjustment startup.cmd script, use java8_home variable, rather than allow it to call the correct jdk1.8 jdk1.7, the following adjustments are part of the code
if not exist "%JAVA8_HOME%\bin\java.exe" echo Please set the JAVA8_HOME variable in your environment, We need java(x64)! jdk8 or later is better! & EXIT /B 1
set "JAVA=%JAVA8_HOME%\bin\java.exe"
Here only to be replaced, the original JAVA_HOME planning to JAVA8_HOME, run startup.cmd, this time to run a proper service Nacos
View Interface
Successful start in the browser to visit: http: // localhost: 8848 / nacos, will jump to the login screen, the default login user name nacos, password is also nacos.
After the successful landing, you can operate the management interface
Use Nacos service registration and discovery
To use nacos, we need to add the necessary dependencies in pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Create a service registry and discovery sample code
In this case, the use of three service registered to the Nacos, respectively, for the service provider ali-nacos-provider and load balancing ribbon consumer ali-nacos-consumer-ribbon, declarative service call feign consumer ali-nacos-consumer -feign.
What is the ribbon and feign, using examples and are not repeated here introduced, you need to know you can see an example
Create a service provider ali-nacos-provider
Add nacos rely pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application.yml Configuration
server:
port: 9000 #指定为9000端口
spring:
application:
name: ali-nacos-provider #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #服务注册地址(nacos默认为8848端口)
management:
endpoints:
web:
exposure:
include: '*'
Start class increase @EnableDiscoveryClient comment
package com.easy.aliNacosProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class AliNacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(AliNacosProviderApplication.class, args);
}
}
Write hello service interface HelloController.java
package com.easy.aliNacosProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class HelloController {
@GetMapping(value = "/hello/{str}", produces = "application/json")
public String hello(@PathVariable String str) {
log.info("-----------收到消费者请求-----------");
log.info("收到消费者传递的参数:" + str);
String result = "我是服务提供者,见到你很高兴==>" + str;
log.info("提供者返回结果:" + result);
return result;
}
}
Create a service consumer ribbon
pom.xml and ribbon-dependent increase nocos
<dependencies>
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 9100
spring:
application:
name: ali-nacos-consumer-ribbon
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
Service call HomeController.java
package com.easy.ancRibbon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class HomeController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/", produces = "application/json")
public String home() {
log.info("-----------------consumer调用开始-----------------");
String param = "云天";
log.info("消费者传递参数:" + param);
String result = restTemplate.getForObject("http://ali-nacos-provider/hello/" + param, String.class);
log.info("收到提供者响应:" + result);
return "ribbon消费者," + result;
}
}
Enable class AncRibbonConsumerApplication.java
package com.easy.ancRibbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class AncRibbonConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(AncRibbonConsumerApplication.class, args);
}
}
Create a service consumer feign
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
application.yml
server:
port: 9101
spring:
application:
name: ali-nacos-consumer-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
main:
allow-bean-definition-overriding: true #允许一样的beanName
Here is a pit next to note that if allow-bean-definition-overriding is set to true, run the following error will be reported:
错误: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
There are problems in detail cause of the problem
Application Service HelloService.java
package com.easy.ancFeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("ali-nacos-provider")
public interface HelloService {
@RequestMapping(path = "hello/{str}")
String hello(@RequestParam("str") String param);
}
Service call HomeController.java
package com.easy.ancFeign;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class HomeController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/", produces = "application/json")
public String home() {
log.info("-----------------consumer调用开始-----------------");
String param = "云天";
log.info("消费者传递参数:" + param);
String result = helloService.hello(param);
log.info("收到提供者响应:" + result);
return "feign消费者" + result;
}
}
Start class AncFeignConsumerApplication.java
package com.easy.ancFeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.easy.ancFeign"})
public class AncFeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(AncFeignConsumerApplication.class, args);
}
}
Examples of Use
The three existing projects are as follows
ali-nacos-provider: a service provider, service name: ali-nacos-provider, port: 9000
Ali-nacos-Consumer-Ribbon: Ribbon service consumers, service name: ali-nacos-consumer-ribbon , port: 9100
ali-nacos-consumer-feign: feign consumers, service name: ali-nacos-consumer-feign , port: 9101
Run the test
First to start the service registry nacos
Secondly, were started ali-nacos-provider, ali- nacos-consumer-ribbon, ali-nacos-consumer-feign three services
- Access address: http: // localhost: 9100 /, return: ribbon consumer, I am a service provider, nice to meet you ==> the sky, indicating the success of the ribbon consumption.
- Access address: http: // localhost: 9101 /, return: feign consumer I am a service provider, nice to meet you ==> the sky, indicating that consumer costs feign success.
- Access address: http: // localhost: 9000 / hello / yuntian, return: I am a service provider, nice to meet you ==> yuntian, description of the service provider access to success (PS: service providers generally are not of foreign public, how subtle the interface will be introduced in the next article)
- Access address: http: // localhost: 8848 / nacos / # / login, enter the user: nacos, password: nacos. Enter the service management interface in the service management - where you can see the list of services we run the three services.