2.2 搭建Nacos服务端
略...
2.3 创建父工程
为了规范依赖的版本,这里创建父工程,指定依赖的版本。
父工程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.itheima.nacos</groupId>
<artifactId>nacos‐discovery</artifactId>
<version>1.0‐SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐maven‐plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.4 服务生产者
以下步骤演示了如何将一个服务生产者注册到 Nacos。
1、pom.xml的配置。
包括Spring Cloud Feign组件、Spring Cloud Alibaba Nacos Discovery组件以及Spring boot web相关组件依赖。
<parent>
<artifactId>nacos‐discovery</artifactId>
<groupId>com.itheima.nacos</groupId>
<version>1.0‐SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>quickstart‐provider</artifactId>
<dependencies>
<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.cloud</groupId>
<artifactId>spring‐cloud‐starter‐openfeign</artifactId>
</dependency>
</dependencies>
2、application.yml配置。一些关于 Nacos 基本的配置也必须在application.yml(也可以是application.properties)配置,如下所示: application.yml
server:
port: 56010 #启动端口
spring:
application:
name: quickstart‐provider
cloud:
nacos:
discovery:
server‐addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
Note: spring.cloud.nacos.discovery.server-addr 指定了Nacos Server的网络地址和端口号。
3、Provider(生产者)服务实现
@RestController
public class ProviderController {
private static final Logger LOG = LoggerFactory.getLogger(ProviderController.class);
@GetMapping("/service")
public String service(){
LOG.info("provider invoke");
return "provider invoke";
}
}
4、启动Provider(生产者)
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosProviderApp {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApp.class, args);
}
}
Note: @EnableDiscoveryClient 在spring cloud项目中表明此项目是一个注册发现客户端,这里注册服务发现使用的是Nacos
Note: @EnableFeignClients 开启FeignClient
2.5 服务消费者
以下步骤演示了如何结合Nacos对spring cloud rest接口进行消费。
1、pom.xml的配置。
<parent>
<artifactId>nacos‐discovery</artifactId>
<groupId>com.itheima.nacos</groupId>
<version>1.0‐SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>quickstart‐consumer</artifactId>
<dependencies>
<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.cloud</groupId>
<artifactId>spring‐cloud‐starter‐openfeign</artifactId>
</dependency>
</dependencies>
2、application.yml配置
server:
port: 56020 #启动端口 命令行注入
spring:
application:
name: quickstart‐consumer
cloud:
nacos:
discovery:
server‐addr: 127.0.0.1:8848
3、Provider(生产者)远程代理定义
@FeignClient(name = "quickstart‐provider")
public interface ProviderClient {
@GetMapping("/service")
String service();
}
4、Consumer(消费者)服务实现
@RestController
public class ConsumerController {
private static final Logger LOG = LoggerFactory.getLogger(ConsumerController.class);
@Autowired
private ProviderClient providerClient;
@GetMapping("/service")
public String service(){
LOG.info("consumer invoke");
String providerResult = providerClient.service();
return "consumer invoke" + "|" + providerResult;
}
}
5、启动Consumer(消费者)
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApp {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApp.class, args);
}
}
访问地址:http://127.0.0.1:56020/service
浏览器访问 http://127.0.0.1:8848/nacos ,打开nacos控制台,并点击菜单服务管理->服务列表,可展示出刚刚注册的两个服务:
QQ截图20200102162051.png
2.6 测试多实例负载均衡
修改quickstart-provider工程的application.yaml,实现port动态传入:
server:
port: ${port:56010} #启动端口
设置启动参数:
启动quickstart-provider两个实例。
QQ截图20200102162349.png
请求http://127.0.0.1:56020/service测试负载均衡。
跟踪quickstart-provider两个实例的控制台的日志输出,默认负载均衡策略是轮询。
网友评论