Spring Cloud Alibaba Nacos 负载均衡策略 使用LoadBalancer替换Ribbon

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>springcloudalibaba</artifactId>
        <groupId>com.wsm.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-loadbalancer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--            <version>2.5.5</version>-->
        </dependency>

        <!-- nacos 服务注册发现  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <!-- 将ribbon排除 -->
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 添加loadbalancer依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

</project>

 

 

 

 

 

application.yml

server:
  port: 8031
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-loadbalancer-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
    loadbalancer:
      ribbon:
        enabled: false #SrpingCloud中禁用ribbon (2021前版本SpringCloud默认ribbon修改负载均衡策略先禁用ribbon)

 

package com.wsm.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
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 OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }

    @Bean
    @LoadBalanced //启用负载均衡
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        RestTemplate restTemplate = builder.build();
        return restTemplate;
    }
}

 

package com.wsm.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
//        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        return "hello world "+msg;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-11-06 18:47  残星  阅读(2386)  评论(0编辑  收藏  举报