1 复制一个服务提供者工程,将端口号改为 9002,用来做负载均衡
2 配置服务消费者,同时来验证Nacos自带负载均衡
创建 消费者 工程
pom 文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.maliqiang</groupId>
<artifactId>spring-cloud-alibaba-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maliqiang</groupId>
<artifactId>spring-cloud-alibaba-nacos-consumer-8083</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-alibaba-nacos-consumer-8083</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意父项目要添加标记
<modules>
<module>spring-cloud-alibaba-nacos-provider-9001</module>
<module>spring-cloud-alibaba-nacos-provider-9002</module>
<module>spring-cloud-alibaba-nacos-consumer-8083</module>
</modules>
resources 下面,创建 application.yml
server:
port: 8083
spring:
application:
name: nacos-consumer # 应用程序名称
cloud:
nacos:
discovery:
server-addr: 192.168.133.135:8848 # Nacos 服务器地址
启动 消费者
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudAlibabaNacosConsumer8083Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudAlibabaNacosConsumer8083Application.class, args);
}
}
查看Nacos注册的服务
3 远程调用与Ribbon
什么是Ribbon
它是一个基于HTTP和TCP客户端负载均衡器。
它虽然只是一个工具类库,它却是每一个微服务的基础设施。
因为实际上,对于服务间调用、API网关请求转发都需要经过Ribbon负载均衡来实现。
总体来说,Ribbon的主要作用是:从注册服务器端拿到对应服务列表后以负载均衡的方式访问对应服务。
要注意的是
Nacos已经整合了Ribbon,所以我们想要使用只需要导入Spring Cloud Alibaba Nacos的依赖就可以直接使用了。
4 验证Nacos自带负载均衡
让服务消费者 spring-cloud-alibaba-nacos-consumer-8083 调用服务提供者 spring-cloud-alibaba-nacos-provider-9001,spring-cloud-alibaba-nacos-provider-9002 ,使用负载均衡Ribbon和远程调用RestTemplate,所以我们要做的第一件事情就是先让9001或者9002服务对外提供接口,用于访问。
package com.maliqiang.springcloudalibabanacosprovider9001.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/getServerPort")
public String getServerPort() {
return "Hello Nacos Discovery " + this.serverPort;
}
}
接下来我们就需要通过服务消费8083者来访问9001或者9002,但是在这之前,我们先在spring-cloud-alibaba-nacos-consumer-8083 模块中的yml文件里添加一句话
server:
port: 8083
spring:
application:
name: nacos-consumer # 应用程序名称
cloud:
nacos:
discovery:
server-addr: 192.168.133.135:8848 # Nacos 服务器地址
# 消费者将要去访问的微服务名称(注册成功的Nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-provider
因为我们要远程调用,所以我们还需要在启动类上配置restTemplate
package com.maliqiang.springcloudalibabanacosconsumer8083;
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 SpringCloudAlibabaNacosConsumer8083Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudAlibabaNacosConsumer8083Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
package com.maliqiang.springcloudalibabanacosconsumer8083.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
@Value("${service-url.nacos-user-service}")
private String serviceUrl;
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/consumer")
public String consumer() {
return restTemplate.getForObject(serviceUrl+"/getServerPort", String.class);
}
}
测试结果:
访问:http://localhost:8083/consumer
结果:Hello Nacos Discovery9001(9002)(负载均衡切换显示)
因为Nacos中本身就集成了Ribbon所以它本身就自带负载均衡