当前位置: 代码迷 >> 综合 >> 03.SpringCloud Aibaba Nacos 服务消费和负载均衡
  详细解决方案

03.SpringCloud Aibaba Nacos 服务消费和负载均衡

热度:66   发布时间:2023-11-17 12:29:48.0

代码结构如下图:
image.png

服务提供者实例

代码一模一样只是端口不同,代码如下:
user-api-9001

@RestController
public class UserService {
    @GetMapping("getUser")public String getUser(){
    return "terry-9001";}
}

user-api-9002

@RestController
public class UserService {
    @GetMapping("getUser")public String getUser(){
    return "terry-9002";}
}

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><artifactId>user-consumer</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version></parent><dependencies><!-- SpringBoot web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos 注册中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version></dependency></dependencies>
</project>

服务消费

在SpringBoot中可以通过一下方式达到项目通讯

  1. RestTemplate(SpringCloud自带负载均衡)
  2. Feign(SpringCloud自带负载均衡)
  3. HttpClient

服务消费者依赖:

<?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><artifactId>user-consumer</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version></parent><dependencies><!-- SpringBoot web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos 注册中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version></dependency></dependencies>
</project>

RestTemplate结合@LoadBalanced实现负载均衡

起一个配置类,配置RestTemplate和@LoadBanlanced

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {
    @Bean@LoadBalancedpublic RestTemplate restTemplate() {
    return new RestTemplate();}
}

Controller配置

    
@RestController
public class UserService {
    @Autowiredprivate RestTemplate restTemplate;@GetMapping("/restTemplateGetUser")public String restTemplateGetUser(){
    return restTemplate.getForObject("http://user-api/getUser", String.class);}
}

打开浏览器输入地址:http://localhost:8080/restTemplateGetUser
多次刷新可以看到是交替运行的。
image.png
image.png

使用LoadBalancerClient实现负载均衡

@RestController
public class UserService {
    @Autowiredprivate RestTemplate restTemplate;@Autowiredprivate LoadBalancerClient loadBalancerClient;/*** 使用 LoadBalancerClient实现负载均衡* @return*/@GetMapping("/loadBalancerClientGetUser")public String loadBalancerClientGetUser(){
    ServiceInstance choose = loadBalancerClient.choose("user-api");System.out.println(choose.getUri().toString());return restTemplate.getForObject(choose.getUri().toString() + "/getUser", String.class);}
}

使用OpenFeign实现负载均衡

消费者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><artifactId>user-consumer</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version></parent><dependencies><!-- SpringBoot web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos 注册中心 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version></dependency><!-- openfeign 客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.6.RELEASE</version></dependency></dependencies>
</project>

启动类配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class App {
    public static void main(String[] args) {
    SpringApplication.run(App.class, args);}
}

openFeign配置类

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;@Component
// 指定服务名称
@FeignClient("user-api")
public interface UserFeign {
    /*** 对应 user-api 的getUser* @return*/@GetMapping("getUser")public String getUser();
}

Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserService {
    @Autowiredprivate UserFeign userFeign;/*** 使用 openFeign实现负载均衡* @return*/@GetMapping("/openFeignGetUser")public String openFeignGetUser(){
    return userFeign.getUser();}
}

打开浏览器输入地址:http://localhost:8080/restTemplateGetUser
多次刷新可以看到是交替运行的。
image.png
image.png