客户端负载均衡和服务端负载均衡最大的不同点在于上面所提到服务清单所存储的位置。在客户端负载均衡中,所有客户端节点都维护着自己要访问的服务端清单,而这些服务端端清单来自于服务注册中心
通过Spring Cloud Ribbon的封装,我们在微服务架构中使用客户端负载均衡调用非常简单,只需要如下两步:
这样,我们就可以将服务提供者的高可用以及服务消费者的负载均衡调用一起实现了。
首先拿到上两张我们的代码:
在cloud-demo-parent上创建maven模块 cloud-demo-ribbon,同样项目结构修改成我们习惯的模样~~~
项目结构是这样的
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文件是这样的,注意端口与其他项目分开
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服务
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访问接口
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);
}
}
复制代码
启动项目:
这里可以看到服务已经启动成功,我们访问下http://localhost:8810/test?name=张三
这里可以看到负载均衡已经成功了