1.Nacos的安装与启动
Nacos支持三种部署模式:单机、集群、多集群,要求使用JDK1.8以上版本。
Nacos的安装有两种方式:源码安装和使用编译好的安装包,这里演示源码安装方式。
# step.1 下载源码
git clone https://github.com/alibaba/nacos.git
# step.2 进入根目录,执行构建命令
mvn -Prelease-nacos clean install -U -Dmaven.test.skip=true
# 这里使用-Dmaven.test.skip=true跳过单元测试的编译
# step.3 进入distribution模块生成的target目录中,执行以下命令启动服务
cd distribution/target/nacos-server-$version/nacos/bin
sh startup.sh -m standalone
Nacos服务启动之后,可以通过http://localhost:8848/nacos/#/login访问Nacos的控制台,账户和密码是nacos/nacos,如下图所示:
2.注册服务到Nacos Server
- 创建一个mvn空项目,命名为scanacos(随你自己起名),pom.xml配置如下填写:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.wy</groupId>
<artifactId>scanacos</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>nacos-sample-provider</module>
<module>nacos-sample-consumer</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</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>
<!-- 基于Nacos的服务注册与发现 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Cloud核心包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建第一个module:nacos-sample-provider,pom.xml配置如下填写:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>scanacos</artifactId>
<groupId>org.wy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-sample-provider</artifactId>
</project>
- nacos-sample-provider代码和配置如下:
package org.wy.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosSampleProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosSampleProviderApplication.class, args);
}
}
【HelloServiceImpl.java】
package org.wy.provider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloServiceImpl {
@GetMapping(value = "/hi")
public String sayHello(@RequestParam(name = "name") String name) {
return "Hello World: " + name;
}
}
【application.yml】
# nacos服务注册中心信息参数
nacos:
instance:
hostname: localhost
port: 8848
spring:
cloud:
nacos:
discovery:
# 服务注册中心地址
server-addr: ${nacos.instance.hostname}:${nacos.instance.port}
application:
# 服务名,对应service-id
name: nacos-sample-provider
# 该服务启动后使用的端口
server:
port: 8001
- 创建第二个module:nacos-sample-consumer,pom.xml配置如下填写:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>scanacos</artifactId>
<groupId>org.wy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-sample-consumer</artifactId>
<dependencies>
<!-- 引入openfeign依赖,用于负载均衡、统一调用接口 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入netflix的archaius包,用于解决使用openfeign的报错:"ClassNotFoundException: com.netflix.config.CachedDynamicIntProperty" -->
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.6</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
- nacos-sample-consumer代码和配置如下:
package org.wy.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosSampleConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosSampleConsumerApplication.class, args);
}
}
【ProviderFeign.java】
package org.wy.consumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("nacos-sample-provider")
public interface ProviderFeign {
@GetMapping("/hi")
String sayHello(@RequestParam(name = "name") String name);
}
【ConsumerController.java】
package org.wy.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private ProviderFeign providerFeign;
@GetMapping("/openfeign/hi")
public String sayHello(@RequestParam(name = "name") String name) {
return providerFeign.sayHello(name);
}
}
配置文件【application.yml】
# nacos服务注册中心信息参数
nacos:
instance:
hostname: localhost
port: 8848
spring:
cloud:
nacos:
discovery:
# 服务注册中心地址
server-addr: ${nacos.instance.hostname}:${nacos.instance.port}
application:
# 服务名,对应service-id
name: nacos-sample-consumer
# 该服务启动后使用的端口
server:
port: 8002
- 启动两个服务,调用一下url,验证服务是否正常
http://localhost:8001/hi?name=test
http://localhost:8002/openfeign/hi?name=test
- 查看Nacos控制台,也可以看到两个服务已经注册进来了
本文示例源码:
[commit id:5bb8995ee0746353f1bd4535bfcf7f94455cc668]