项目地址 module:spring-cloud-service-ribbon
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。 Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。
3.1 ribbon简介
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
3.2 准备工作
启动eureka-server 工程;启动service-a工程,它的端口为7071;将service-a的配置文件的端口改为7072,并启动, 这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。
java -jar xxxx.jar --server.port=7071
java -jar xxxx.jar --server.port=7072
3.3 建一个服务消费者
重新新建一个spring-boot工程,取名为:spring-cloud-service-ribbon pom文件:
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- 添加熔断器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.4 添加配置
在工程的配置文件指定服务的注册中心地址为http://localhost:7070/eureka/,程序名称为 service-ribbon,7075。配置文件application.yml如下:
server:
port: 7075
spring:
application:
name: service-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7070/eureka/
instance:
hostname: localhost
3.5 修改启动类
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate; 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
3.6 添加HelloService
写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口, 在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,
/**
* description:
*
* @author sunjiamin
* @date 2018-05-11 16:22
*/
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
//return restTemplate.getForObject("http://localhost:7071/hi?name="+name,String.class);
return restTemplate.getForObject("http://service-hi/hi?name="+name,String.class);
}
}
3.7 添加Controller
写一个controller,在controller中用调用HelloService 的方法,代码如下:
/**
* description:
*
* @author sunjiamin
* @date 2018-05-11 16:23
*/
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
在浏览器上多次访问http://localhost:7075/hi?name=jojo ,浏览器交替显示:
hi jojo,i am from port:7071
hi jojjo,i am from port:7072
这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡, 访问了不同的端口的服务实例。
此时的架构:
一个服务注册中心,eureka server,7070
spring-cloud-service-a工程跑了两个实例,端口分别为7071,7072,分别向服务注册中心注册
spring-cloud-sercvice-ribbon端口为7075,向服务注册中心注册
当spring-cloud-sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:7071和7072 两个端口的hi接口;