0
点赞
收藏
分享

微信扫一扫

四、SpringCloud服务调用(RestTemplate模式) 负载均衡处理


 

四、SpringCloud服务调用(RestTemplate模式) 负载均衡处理_SpringCloud

也就是通过这个服务名可以找到具体的机器以及它的端口号;

 

Eureka服务中心去调用会涉及到两种方式;
一种是RestTemplate模式,一种是Feign接口对象模式

我先介绍RestTemplate模式,我们需要引入spring-cloud-starter-ribbon的jar包;
ribbon实现负载均衡,也就是说我们现在在服务器上注册了我们的服务是一个服务注册在一台机器上;将来我们可以把一个服务在多台机器上去注册,这样一个服务它就有好多服务器;那么好多服务器,我们去调用的话,就是涉及到具体去调用哪个服务器;

就好比去饭店吃饭有三个服务员,究竟哪个服务员招待你,这时有个主管在分配工作;

同样的,我们这里需要有个负载均衡的工具;这个工具在支配着,看谁在闲着就分配给谁;

 

第一步:

现在SpringCloud里面已经默认集成了ribbon这样一个工具,调用哪个服务就在哪个服务里pom.xml面追加spring-cloud-starter-ribbon和spring-cloud-starter-parent

<!-- ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

[elcipse] 选中项目右键->Maven->Update Project->OK

 

第二步:创建一个cn.xxx.xxx.config包,在下面创建一个创建RestTemplate对象,追加@LoadBalanced,启用ribbon负载功能

@Configuration
public class RestTemplateConfiguration {

@Bean
@LoadBalanced//追加ribbon负载功能
public RestTemplate createRestTemplate(){
return new RestTemplate();
}

}

 

第三步:直接注入Controller,并调用

@Controller
@RequestMapping("/exam")
public class ExamController {

@Autowired
private RestTemplate template;

@RequestMapping("/home.html")
public String home(String token,ModelMap model){

//调用http://localhost:8885/subject请求获取所有学科信息
//url规则: http://注册服务名/请求名
ResponseResult result = template.getForObject(
"http://SUBJECT-SERVICE/subject", ResponseResult.class);


ResponseResult result = subjectRemote.loadSubjects();
if(result.getStatus()==1){
List<Map> subjectList = (List)result.getData();//获取学科集合
Iterator<Map> it = subjectList.iterator();
while(it.hasNext()){//循环学科
Map subject = it.next();
Integer subjectId = (Integer)subject.get("id");
String subjectName = (String)subject.get("name");

//调用http://localhost:8884/paper/subject/xxx服务加载试卷
String url = "http://PAPER-SERVICE/paper/subject/"+subjectId+"?token="+token;
ResponseResult paperResult =
template.getForObject(url,ResponseResult.class);


ResponseResult paperResult = paperRemote.loadSubjectPapers(subjectId);
//获取试卷集合,有试卷添加papers属性,没有试卷将学科从集合移除
if(paperResult.getStatus()==9){
return "nologin";//跳转到一个未登录界面
}else if(paperResult.getStatus()==1){
List paperList = (List)paperResult.getData();
subject.put("papers", paperList);//将试卷集合添加到学科中
}else{
it.remove();//剔除subject元素
}
}

}
//将数据存入Model
model.put("result", result);
return "home";//templates/home.html
}

}

 

第四步:application.properties文件中指明服务中心地址

server.port=9992

#thymeleaf
spring.thymeleaf.mode=LEGACYHTML5

#eureka
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka

 

第五步:在当前启动类XXXXBootApplication添加@EnableDiscoveryClient  // 服务注册和查找

package cn.xxx.ovls;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableDiscoveryClient//启用服务注册和查找
@SpringBootApplication
public class XXXXBootApplication {

public static void main(String[] args){
SpringApplication.run(ExamWebBootApplication.class, args);
}

}

启动各个服务测试

 

 

 

 

 

举报

相关推荐

0 条评论