前面一篇SpringCloud学习-(2)服务的注册与发现(Eureka),提供者向eureka注册中心注册了一个sayHello服务,然后访问提供者即可(是不是感觉有点儿绕路了呢?肯定有用啦,慢慢看)。
1.启动eureka注册中心和服务提供者
延续上一篇的项目(读者尽量不要从这里开始看,而是从SpringCloud学习-(2)服务的注册与发现(Eureka)开始)
springcloud-eureka-server保持不变,springcloud-eureka-client1也保持不变,同时对springcloud-eureka-client1做一份拷贝命名springcloud-eureka-client2,端口号设置为8763。
启动springcloud-eureka-server服务注册中心。然后再启动springcloud-eureka-client1和springcloud-eureka-client2两个工程。那么注册中心上面就可以看到两个sayHello提供者,那么消费者如何消费呢?
2.ribbon简介
ribbon是netflix提供的一个客户端负载均衡器,用于解决微服务的负载均衡问题。
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP
and TCP clients.
Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。
ribbon 已经默认实现了这些配置bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList ribbonServerList: ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
使用ribbon对上面的两个服务做负载均衡。
3.创建ribbon工程
3.1创建ribbon工程
按照前面的创建步骤
GroupId:com.tangjinyi
ArtifactId:springcloud-eureka-ribbon
pom.xml配置文件如下:
这里面需要引入spring-cloud-starter-netflix-ribbon依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tangjinyi</groupId>
<artifactId>springcloud-eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-eureka-ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3.2修改application.properties配置文件
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8764
spring.application.name=service-ribbon
3.3填写ribbon流程逻辑
同时创建ribbon的mvc服务,注意启动类的放置位置,与需要扫描的包同级。
UserController
package com.tangjinyi.controller;
import com.tangjinyi.service.UserService;
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;
@RestController
public class UserController {
@Autowired
public UserService userService;
@RequestMapping(value = "/sayHello")
public String sayHello(@RequestParam("name") String name){
return userService.sayHello(name);
}
}
UserService
package com.tangjinyi.service;
import com.tangjinyi.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
public UserRepository userRepository;
public String sayHello(String name){
return userRepository.sayHello(name);
}
}
UserRepository
使用restTemplate请求eureka上面的eureka服务,主机名为服务提供者提供的服务名,端口为eureka注册中心的端口。
这里的restTemplate需要通过@Bean加入到IOC容器中,该操作放在启动类中。
在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名。程序名称为VIP(虚拟ip,在HAproxy或者Nginx经常会提到)
package com.tangjinyi.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;
@Repository
public class UserRepository {
@Autowired
public RestTemplate restTemplate;
public String sayHello(String name){
String value = restTemplate.getForObject("http://service-sayHello:8761/sayHello?name="+name,String.class);
return value;
}
}
3.4配置启动类
通过@Bean注解将restTemplete添加到Ioc容器中。同时添加@LoadBalanced注解,表示负载均衡。
package com.tangjinyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* ribbon作为负载均衡器。<br>
* 启动eureka服务器,然后将服务提供者client1和client2的服务(sayHello)都注册到eureka注册中心,此时eureka服务器上面有两个sayHello
* 服务。<br>
* 此时消费者不直接访问这两个提供者,而是通过一个叫ribbon的负载均衡器提供对外的服务。<br>
* ribbon也是通过注册的形式先注册到eureka服务器上,在ribbon服务内部通过restTemplate调用client1和client2的接口。对消费者得请求做分发,起到负载均衡的效果。
*
* client1和client2提供的服务是在application.properties配置文件中配置的服务地址。但是实际访问并不直接访问client1和client2提供的服务。而是访问ribbon提供的服务和端口。
* 这里的访问地址为http://localhost:8764/sayHello?name=tangjinyi
*/
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudEurekaRibbonApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaRibbonApplication.class, args);
}
}
3.5启动ribbon工程
仍然是右键rus as SpringcloudEurekaRibbonApplication类,启动完毕后,访问eureka控制台,查看ribbon服务是否注册到eureka注册中心。
打开浏览器访问http://localhost:8764/sayHello?name=wangwu
刷新浏览器,发现端口号在8762和8763之间轮询切换。表示通过ribbon访问服务提供者正常,且负载均衡。
4.服务架构
eureka注册中心上面注册了3个服务,分别是服务提供者service-sayHello8762,service-sayHello8763和service-ribbon8764。ribbon工程中的Controller提供了sayHello访问入口。此时如果需要访问8762和8763,并不是直接访问提供者,而是访问ribbon提供的入口sayHello,由ribbon做转发访问真实的服务提供者(这里的ribbon需要从eureka注册中心获取到注册的可用服务列表,这也就是为什么要注册服务)。
通过ribbon转发做了轮询访问,实现负载均衡效果。