Nacos(官方网站:http://nacos.io)是一个易于使用的平台,专为动态服务发现,配置和服务管理而设计。它能够帮助您轻松构建云本机应用程序和微服务平台。java
下载地址:github.com/alibaba/nac… 本文版本:1.0.1git
下载完成以后,解压。github
cd 到nacos/bin目录下web
根据不一样平台,执行不一样命令,启动Nacos服务:spring
启动命令(standalone表明着单机模式运行,非集群模式):apache
sh startup.sh -m standalone
cmd startup.cmd -m standalone
或者双击startup.cmd
运行文件。不出意外 你会启动成功 启动完成以后,访问:http://localhost:8848/nacos/
,能够进入Nacos的服务管理页面,具体以下:浏览器
默认用户名/密码: nacos/nacos 登陆便可 目前Nacos注册中心搭建完毕bash
项目结构图:app
Spring Boot:
2.1.6.RELEASE负载均衡
Spring Cloud:
Greenwich.SR1
Spring Cloud Alibaba:
0.2.2.RELEASE
教程采用多模块化,先建立maven项目做为父项目,引入公共依赖
完整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>
<groupId>com.xd</groupId>
<artifactId>SpringCloudAlibabaLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>alibaba-nacos-provider-server</module>
<module>alibaba-nacos-consumer-server</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
复制代码
上述内容主要三部分:
parent
:定义spring boot的版本dependencyManagement
:spring cloud的版本以及spring cloud alibaba的版本,因为spring cloud alibaba还未归入spring cloud的主版本管理中,因此须要本身加入dependencies
:当前应用要使用的依赖内容。这里主要新加入了Nacos的服务注册与发现模块:spring-cloud-starter-alibaba-nacos-discovery
。因为在dependencyManagement
中已经引入了版本,因此这里就不用指定具体版本了。还有就是spring-boot的起步依赖
下面在父模块上建立两个应用(服务提供者与服务消费者)来验证服务的注册与发现
而后命项目名,组织名等便可
<?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>com.xd</groupId>
<artifactId>SpringCloudAlibabaLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>alibaba-nacos-provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alibaba-nacos-provider-server</name>
<description>服务提供者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
复制代码
配置服务名称和nacos注册地址
# 自定义端口号
server.port=8080
# 服务提供者
spring.application.name=provider-service
# 注册到nacos注册中心
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
复制代码
package com.xd.alibabanacosproviderserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @RestController 表示控制层
* @EnableDiscoveryClient 开启服务注册发现功能
*/
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosProviderServerApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosProviderServerApplication.class, args);
}
@GetMapping("/echo/{name}")
public String echo(@PathVariable String name) {
return "hello " + name;
}
}
复制代码
主要表示:
@SpringBootApplication
定义是个Spring Boot应用;
@EnableDiscoveryClient
开启Spring Cloud的服务注册与发现,因为这里引入了spring-cloud-starter-alibaba-nacos-discovery
模块,因此Spring Cloud Common中定义的那些与服务治理相关的接口将使用Nacos的实现
启动程序完成后你会发现控制台打印的 说明已经向nacos完成注册
切换到服务列表: 这里会显示当前注册的全部服务,以及每一个服务的集群数目、实例数、健康实例数。
点击详情,咱们还能看到每一个服务具体的实例信息,以下图所示:
目前为止服务提供者建立完毕
步骤同上,这里不作多解释
<?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>com.xd</groupId>
<artifactId>SpringCloudAlibabaLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>alibaba-nacos-provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alibaba-nacos-provider-server</name>
<description>服务消费者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
复制代码
配置服务名称和nacos注册地址
server.port=8081
#服务消费者
spring.application.name=consumer-service
#注册服务到nacos
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
复制代码
package com.xd.alibabanacosconsumerserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @RestController 表示控制层
* @EnableDiscoveryClient 开启服务注册发现功能
*/
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosConsumerServerApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosConsumerServerApplication.class, args);
}
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("/echo/{name}")
public String test(@PathVariable("name") String name) {
// 经过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
// serviceId为spring.application.name
ServiceInstance serviceInstance = loadBalancerClient.choose("provider-service");
String url = serviceInstance.getUri() + "/echo/" + name;
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "from: " + url + ",return: " + result;
}
}
复制代码
这里使用了Spring Cloud Common中的LoadBalancerClient
接口来挑选服务实例信息。而后从挑选出的实例信息中获取可访问的URI,拼接上服务提供方的接口规则来发起调用。
一样的打开nacos注册中心有没有注册成功
看来都已经注册成功,而后打开浏览器访问: http://localhost:8081/echo/lhd
能够看到,发送请求时,已经成功调用并返回信息 本文模拟在微服务中服务与服务之间相互调用的过程,可是很明显,这样的实现仍是比较繁琐,后面讲解服务消费的几种方式
若是您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!