客户端负载均衡和服务端负载均衡最大的不一样点在于上面所提到服务清单所存储的位置。在客户端负载均衡中,全部客户端节点都维护着本身要访问的服务端清单,而这些服务端端清单来自于服务注册中心web
这样,咱们就能够将服务提供者的高可用以及服务消费者的负载均衡调用一块儿实现了。算法
首先拿到上两张咱们的代码:spring
在cloud-demo-parent上建立maven模块 cloud-demo-ribbon,一样项目结构修改为咱们习惯的模样~~~apache
项目结构是这样的bash
pom文件时这样的,这里之因此不引入ribbon的包是由于spring-cloud-starter-netflix-eureka-client包含了ribbon架构
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hewl</groupId>
<artifactId>cloud-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>cloud-demo-ribbon</artifactId>
<name>cloud-demo-ribbon</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
复制代码
application.yml文件是这样的,注意端口与其余项目分开app
server:
port: 8810 # 你的端口
spring:
application:
name: ribbon
eureka:
client:
service-url:
defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
复制代码
修改RibbonApplication类负载均衡
package com.hewl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced //注解代表这个restRemplate开启负载均衡的功能
public RestTemplate restTemplate () {
return new RestTemplate();
};
}
复制代码
新增service类,调用以前写的cloud-demo-eureka-client服务maven
package com.hewl.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class TestService {
@Autowired
public RestTemplate restTemplate;
public static final String SERVICE_ADDRESS = "http://SERVICE-EUREKA-CLIENT/";
public String testService(String name) {
return restTemplate.getForObject(SERVICE_ADDRESS + "test?name=" + name, String.class);
}
}
复制代码
新增controller类,ribbon访问接口spring-boot
package com.hewl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hewl.service.TestService;
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping(value = "/test") // 随便起个本身喜欢的访问名字
public String test(@RequestParam("name") String name) {
return testService.testService(name);
}
}
复制代码
启动项目:
这里能够看到负载均衡已经成功了