在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http
restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。
1.ribbon简介
ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。
2.准备工作
这一篇文章基于上一篇文章的工程,启动eureka-server
工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。
3.建立服务消费者
重新新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml继承了父pom文件,并引入了以下依赖:
<modelVersion>4.0.0</modelVersion>
<groupId>com.forezp</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
</dependencies>
在启动类编写:
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean:
restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //向服务中心注册;并且向程序ioc注入一个bean
public class ServiceRibbionApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbionApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
编写一个服务:
/**
* @PACKAGE_NAME com.springcloud.serviceribbon
* @PROJECT_NAME sc-f-chapter1
* @创建人 huang
* @创建时间 2018/11/27
*/
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
/* 通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,
在这里我们直接用的程序名替代了具体的url地址,
在ribbon中它会根据服务名来选择具体的服务实例,
根据服务实例在请求的时候会用具体的url替换掉服务名*/
/**
* 调用SERVICE-HI这个服务
*
* @param name
* @return
*/
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
}
在控制层调用这个服务:
/**
* @PACKAGE_NAME com.springcloud.serviceribbion.controller
* @PROJECT_NAME sc-f-chapter1
* @创建人 huang
* @创建时间 2018/11/27
*/
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi/{name}")
public String hi(@PathVariable("name") String name) {
return helloService.hiService(name);
}
}
修改全局配置文件:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 9002
spring:
application:
name: service-ribbon
多次访问:http://localhost:9002/hi/黄世民
页面交替出现:
hi黄世民,i am from port:9000
hi黄世民,i am from port:8889
此时的服务列表
- 一个服务注册中心,eureka server,端口为8888
- service-hi工程跑了两个实例,端口分别为8889,9000,分别向服务注册中心注册
- sercvice-ribbon端口为9002,向服务注册中心注册
- 当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8889和9000两个端口的hi接口;
4.参考资料:
https://blog.csdn.net/forezp/article/details/69788938
https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/