在【springcloud 服务注册与发现】中,我们注册了服务EurekaClient到注册中心EurekaServer。我们运行一次客户端EurekaClient之后将该工程中的配置文件中端口号进行更改,再次运行就可看到我们注册中心有一个注册的服务,后面对应两个端口号。这就相当于一个小小的集群。
因为现在的业务都是划分成一个一个独立的业务,所以接口之间的调用需要独立出来。我们将所有的接口注册到【注册中心】上,然后接口之间直接通过某种”方式“直接调用即可。
比如,在之前我们在EurekaClient向注册中心注册了两个端口号的服务,我们现在建立【消费者】将该注册的接口进行调用。
现在介绍spring cloud中的这两种方法。
【第一种:RestTemplate+ribbon】
1.在启动类中通过@Bean引入RestTemplate
2.在RestTemplate上通过@LoadBalanced进行负载均衡
3.service层调用RestTemplate,方法中直接调用getForObject(“服务名/接口”,String.class),来调用指定服务的接口。
4.提供接口,暴露服务。
ribbon : 通过@LoadBalanced注解进行负载均衡
Ribbon的工作 分为两步: (看不懂不重要,之后好好琢磨,啊哈哈)
1) 第一步有限选择Eureka Server,它优先选择在同一个Zone且负载较少的Server,
2) 第二步在根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多重策略,例如轮询round robin、随机Random、根据相应时间加权等。
我们现在开始建立一个消费者,调用EurekaClient注册的服务。
步骤一:编写配置文件application.yml (注册中心地址,该服务的端口号,服务发布的名称)
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10001/eureka/
server:
port: 8084
spring :
application:
name: service-ribbon
步骤二:编写启动类,需要注意的是spring boot有自动加载所有配置文件的功能,所以,这里我们在启动类中通过@Bean注解将RestTemplate进行注入,并且通过@LoadBalanced进行负载均衡。
package com.cloud;
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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
System.out.println("******ribbon已启动********");
}
@Bean
@LoadBalanced
//通过该@LoadBalanced注解进行负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
}
步骤三:在service层通过 @Autowired注解 引入 RestTemplate,编写方法获取EurekaClient发布的服务的接口。(这里它发布的服务名是service-hello,接口名是hello,传入参数name)
package com.cloud.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String helloService(String name) {
return restTemplate.getForObject("http://service-hello/hello?name="+name, String.class);
}
}
步骤四:编写控制类,调用service的方法,并提供一个接口(这个项目启动也是会注册到注册中心的,我们的操作是注册中心的服务与服务之间的调用)
package com.cloud.control;
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.cloud.service.HelloService;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return helloService.helloService(name);
}
}
最后看一下maven依赖
<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>springcloud-service-ribbon</groupId>
<artifactId>springcloud-service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>