说白了就是通过算法去调用集群

1.在复制一份Eureka_user_service_2000

2.修改其中的端口号及显示名称

application.yml中的配置2000也可以改一下

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/#注册中心地址
  instance:
    ip-address: true #使用ip配置
    instance-id: user-server-2001 #指定服务的id
server:
  port: 2001
spring:
  application:
    name: user-server

3.在order中配置Ribbon

引入pom

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

4.修改RestTemplate

package cn.jiedada.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 WebConfig {
    /**
     *  @LoadBalanced
     *  这个标签可以方RestTemplate拥有负载均衡算法
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

5.修改url中的绝对路径

  @RequestMapping("/user/{id}")
    public User getUserById(@PathVariable("id")Long id){
        //拼接字符串
        String url="http://USER-SERVER/provider/user/"+id;
        //通过这个方法调用我们的
        return restTemplate.getForObject(url,User.class);
    }

访问的时候端口值会不断地变化

01-07 20:30