客戶端負載均衡和服務端負載均衡最大的不一樣點在於上面所提到服務清單所存儲的位置。在客戶端負載均衡中,全部客戶端節點都維護着本身要訪問的服務端清單,而這些服務端端清單來自於服務註冊中心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);
}
}
複製代碼
啓動項目:
這裏能夠看到負載均衡已經成功了