第一步,搭建springboot项目,这个不多说
修改pom.xml文件:
引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
具体pom.xml如下:
<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>
<parent>
<groupId>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ribbon</artifactId>
<name>ribbon</name>
<description>ribbon消费者</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</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-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
第二步,修改配置文件application.properties,如下图,这里就是一个普通的服务消费者,定义服务名称ribbon-consumer,端口号3001和eureka服务的地址http://localhost:1001/eureka/
#配置服务及端口
spring.application.name=ribbon-consumer
server.port=3001
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒
eureka.client.registry-fetch-interval-seconds=5
第三步,找到我们的主类,RibbonApplication.java,基本上和前面的服务提供者配置差不多,此处我们定义了一个RestTemplate的模板的bean,这个bean帮助我们调用远程的服务。
@LoadBalanced是一个本地负载均衡的实现,具体的源码探究可以参考下面的链接
https://blog.youkuaiyun.com/u011063112/article/details/81295376
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
第四步,建设我们的controller,来调用其他服务提供者的服务。
package pers.cc.springCloud.ribbon.test.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.ResponseEntity;
@RestController
public class TestCoontroller {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return restTemplate.getForEntity("http://EUREKA-CLIENT/dc", String.class).getBody();
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
Map<String, Object> urlVariables = new HashMap<String, Object>();
urlVariables.put("a",new Integer(200));
urlVariables.put("b",new Integer(210));
return restTemplate.getForEntity("http://EUREKA-CLIENT/add?a={1}&b={2}", String.class,new Integer(200),new Integer(300)).getBody();
}
@RequestMapping(value = "/stop", method = RequestMethod.GET)
public String stop() {
return restTemplate.getForEntity("http://EUREKA-CLIENT/stop", String.class).getBody();
}
}
下面我们看一下效果:
http://localhost:3001/stop/ 因为服务提供方里面有一个睡眠5秒钟,所以我们调用下面这个链接的时候,5秒钟返回stop,这里我们只是模拟了关机的一个状态而已
好了,ribbon是不是很简单呢,我们是利用
import org.springframework.web.client.RestTemplate;
@Autowired
RestTemplate restTemplate;
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
这个注解来获取了一个rest调用的工具类,来帮助我们实现远程的http调用。
本章ribbon就讲到这里。