SpringCloud: 第四章Ribbon负载均衡

1.Ribbon是一个为客户端提供负载均衡功能的服务,它内部提供了一个叫做ILoadBalance的接口代表负载均衡器的操作,比如有添加服务器操作、选择服务器操作、获取所有的服务器列表、获取可用的服务器列表等等。

2.本章将提供一个负载均衡的实例:如图所示消费者会轮询地消费服务提供者2001,2002,2003在这里插入图片描述

3.创建服务提供者2001 (2002,2003跟2001一样改一下端口号即可,一定要保证yml文件中spring.application.name名字一致,且名字不能含有_符号)
其结构如图
在这里插入图片描述
yml文件

server:
  port: 2001

spring:
  application:
    name: clinet #注册名字

eureka:
  client:
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka,http://eureka7003.com:7003/eureka
  instance:
    instance-id: clinet_1:2001 #修改Eureka注册Status的名字显示更好的定位是哪个服务
    prefer-ip-address: true #修改Eureka注册Status的显示ip

Controller:

@RestController
public class ListContoller {

    @GetMapping("/list")
    public List getAll(){
        Student student1 = new Student("zhangsan_1",12);
        Student student2 = new Student("zhangsan_1",13);
        Student student3 = new Student("zhangsan_1",14);
        List list = new ArrayList();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        return list;
    }
}

Entity:

public class Student {

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Application:

@SpringBootApplication
public class Cllient_1_Application {

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

}

4.创建服务消费者2003
其结构如图:
在这里插入图片描述
Pom文件:

<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

yml:

server:
  port: 3001

spring:
  application:
    name: clinet_consumer 

eureka:
  client:
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka,http://eureka7003.com:7003/eureka
  instance:
    instance-id: consumer_1 
    prefer-ip-address: true 

Config:

@Configuration
public class MyRibbon {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

Controller

@RestController
public class Consumer {

    @Autowired
    private RestTemplate restTemplate;

    //即服务者的spring.application.name
    private String URL = "http://clinet/";

    @GetMapping("/all")
    public Object getlist(){
      return  restTemplate.getForEntity(URL+"list", Object.class);
    }
}

Application:

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

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

}

5.根据前面章节搭建Eureka集群7001,7002,7003,然后分别启动Eureka集群和服务提供者和服务消费者,然后访问http://localhost:3001/all,7001,7002,7003提供的name分别为zhangsan_1,zhangsan_2,zhangsan_3,
所以每访问一次name都会变化
在这里插入图片描述
(git地址git@gitee.com:zhu_can_admin/springcloud.git)

  @Autowired
    private RestTemplate restTemplate;

    //即服务者的spring.application.name
    private String URL = "http://nacos-config-client/";

    //get无参调用
    @GetMapping("test")
    public Student getRest(){
        Student student = restTemplate.getForObject(URL + "test", Student.class);
       return student;
    }

    //get参数调用
    @GetMapping("test2")
    public Student gettest2(){
        String age = "11";
        String name = "张三";
        String sex = "男";
        HashMap hashMap = new HashMap();
        hashMap.put("age",age);
        hashMap.put("name",name);
        hashMap.put("sex",sex);
        Student forEntity = restTemplate.getForObject(URL + "test2?age={age}&&name={name}&&sex={sex}", Student.class, hashMap);
        return forEntity;
    }

    //post参数调用
    @GetMapping("test3")
    public Student gettest3(){
        String age = "11";
        String name = "张三";
        String sex = "男";
        LinkedHashMap<String ,Object> hashMap = new LinkedHashMap();
        hashMap.put("age",age);
        hashMap.put("name",name);
        hashMap.put("sex",sex);
        Student forEntity = restTemplate.postForObject(URL + "test3", hashMap,Student.class, hashMap);
        return forEntity;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值