nacos user服务消费者(ribbon+RestTemplate)访问notice集群

创建工程

添加依赖pom.xml

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- springboot依赖,不写版本号表示继承父工程 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- springboot依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- mybatis与springboot的整合 -->
    <!--        <dependency>-->
    <!--            <groupId>org.mybatis.spring.boot</groupId>-->
    <!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
    <!--        </dependency>-->
    <!--        <dependency>-->
    <!--            <groupId>com.alibaba</groupId>-->
    <!--            <artifactId>druid-spring-boot-starter</artifactId>-->
    <!--        </dependency>-->
    <!--mysql-connector-java-->
    <!--        <dependency>-->
    <!--            <groupId>mysql</groupId>-->
    <!--            <artifactId>mysql-connector-java</artifactId>-->
    <!--        </dependency>-->
    <!--jdbc-->
    <!--        <dependency>-->
    <!--            <groupId>org.springframework.boot</groupId>-->
    <!--            <artifactId>spring-boot-starter-jdbc</artifactId>-->
    <!--        </dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

nacos底层依赖ribbon

底层依赖ribbon,所以默认使用客户端负载均衡

application.yml配置文件

在resources文件夹中创建application.yml配置文件

server:
  port: 9001   # 服务端口
spring:
  application:
    name: user-consumer   # 注册服务名
  cloud:
    nacos:
      discovery:
        server-addr: 106.13.165.100:8848   # nacos地址

# 配置服务提供者url,便于在类中调用
service-provider:
  notice-provider:
    name: http://notice-provider
    urls:
      notice: /notice/{id}
      getNoteById: /getNoteById/{id}

# 负载均衡策略
notice-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

负载均衡策略参见:
https://www.malaoshi.top/show_1EF5iJxtARRK.html
https://www.malaoshi.top/show_1EF5iInH93Gc.html

主启动类

package top.malaoshi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class UserMain {
    public static void main(String[] args) {
        SpringApplication.run(UserMain.class,args);
    }
}

配置类

package top.malaoshi.config;

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 ApplicationConfig {

    @Bean
    @LoadBalanced //负载均衡
    public RestTemplate getTemplate(){
        return new RestTemplate();
    }
}

controller

package top.malaoshi.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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;

import javax.annotation.Resource;

@RestController
@Slf4j
public class FollowCtrl {

    @Resource
    private RestTemplate restTemplate;

    //从application.yml中读取配置项
    @Value("${service-provider.notice-provider.name}${service-provider.notice-provider.urls.notice}")
    private String noticeUrl;

    @GetMapping(value="/follow/{id}")
    public String follow(@PathVariable("id") String id){
        log.info("user:id:"+id);
        return restTemplate.getForObject(noticeUrl, String.class,id);
    }
}

修改notice服务的NoticeCtrl

打印一段信息,区别user调用的是哪一个notice服务

package top.malaoshi.controllers;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j //添加注解
public class NoticeCtrl {

    @GetMapping(value = "/notice/{id}")
    public String notice(@PathVariable String id) {
        log.info("id--"+id); //直接使用log对象
        return "通知成功! " + id;
    }

}

测试

启动

启动nacos
启动2个notice
启动user

测试服务

访问 http://localhost:9001/follow/1 能够随机调用1个notice

其中一个notice服务会打印信息


原文出处:/show_1EF5l8AseRgw.html