Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端的负载均衡的工具。
简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
server:
port: 8001 #服务端口
eureka:
instance:
hostname: user8001
instance-id: user8001
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:7000/eureka
spring:
application:
name: user8001
服务间的访问就需要使用RestTemplate
, 它是专门用于服务间访问的
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
RestTemplate restTemplate;
/**
* getForObject(String url,Object.class); get方式访问url指定的微服务,Object.class 返回的数据类型
* getForEntry(String url,Object.class)
* getForObject(String url, Class responseType, Map uriVariables) 这三个参数分别代表 REST请求地址、HTTP响应转换被转换成的对象类型、请求参数
* postForObject(String url,Object.class)
* postForEntry(String url,Object.class)
*/
@GetMapping("/callDept")
public Object callDept(){
String msg = "访问Dept服务";
return restTemplate.getForObject("http://localhost:8002/dept/called",Object.class);
}
}
server:
port: 8002 #服务端口
eureka:
instance:
hostname: dept8002
instance-id: dept8002
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:7000/eureka
spring:
application:
name: dept
@RestController
@RequestMapping("/dept")
public class DeptController {
@GetMapping("/called")
public Object beCall() {
System.out.println("dept : 被访问了");
return "{\"code\":\"200\"}";
}
}
将两个服务启动起来,使用User模块调用Dept模块
2020-11-06 10:01:06.929 ERROR 10253 --- [nio-8001-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [class java.lang.Object] and content type [application/xml;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]] with root cause
com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:666) ~[woodstox-core-5.3.0.jar:5.3.0]
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2130) ~[woodstox-core-5.3.0.jar:5.3.0]
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1179) ~[woodstox-core-5.3.0.jar:5.3.0]
at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:685) ~[jackson-dataformat-xml-2.10.1.jar:2.10.1]
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:568) ~[jackson-dataformat-xml-2.10.1.jar:2.10.1]
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:29) ~[jackson-dataformat-xml-2.10.1.jar:2.10.1]
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:972) ~[jackson-core-2.10.1.jar:2.10.1]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3258) ~[jackson-databind-2.10.1.jar:2.10.1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:104) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:318) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.lsh.controller.UserController.callDept(UserController.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) [tomcat-embed-core-9.0.29.jar:9.0.29]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.29.jar:9.0.29]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_211]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_211]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.29.jar:9.0.29]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]
将restTemplate.getForObject的返回数据类型修改给String
对调用方法设置对返回值的格式为json即可解决
public <T> ResponseEntity<T> getJson(String url, String requestJson, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//设置接收返回值的格式为json
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
headers.setAccept(mediaTypeList);
HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
ResponseEntity<T> responseEntity;
System.out.println(requestJson);
responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
return responseEntity;
}
完整的的UserController代码:
package com.lsh.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2020/11/6 9:45 上午
* @desc :
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
RestTemplate restTemplate;
/**
* getForObject(String url,Object.class); get方式访问url指定的微服务,Object.class 返回的数据类型
* getForEntry(String url,Object.class)
* getForObject(String url, Class responseType, Map uriVariables) 这三个参数分别代表 REST请求地址、HTTP响应转换被转换成的对象类型、请求参数
* postForObject(String url,Object.class)
* postForEntry(String url,Object.class)
*/
@GetMapping("/callDept")
public Object callDept(){
String msg = "访问Dept服务";
// return restTemplate.getForObject("http://localhost:8002/dept/called",Object.class);
return getJson("http://localhost:8002/dept/called",msg,Object.class);
}
public <T> ResponseEntity<T> getJson(String url, String requestJson, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//设置接收返回值的格式为json
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
headers.setAccept(mediaTypeList);
HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
ResponseEntity<T> responseEntity;
System.out.println(requestJson);
responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
return responseEntity;
}
// public ResponseEntity postJson(String url, String requestJson, Class responseType) {
//
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//
// //设置接收返回值的格式为json
// List mediaTypeList = new ArrayList<>();
// mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
// headers.setAccept(mediaTypeList);
//
// HttpEntity entity = new HttpEntity<>(requestJson, headers);
//
// ResponseEntity responseEntity;
//
// System.out.println(requestJson);
//
// responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
//
// return responseEntity;
// }
}
UserController:
@GetMapping("/callDeptAndParam")
public String callDeptAndParam(){
String msg = "访问Dept服务 带参数";
HashMap<String, String> map = new HashMap<>();
map.put("id","1");
String forObject = restTemplate.getForObject("http://localhost:8002/dept/called/{id}", String.class, map);
System.out.println("forObject:"+forObject);
return forObject;
}
DeptController:
@GetMapping("/called/{id}")
public Object beCallById(@PathVariable("id") String id) {
System.out.println("dept : 被访问了");
return "{\"code\":\"8002\",\"id\":\""+id+"\"}";
}
UserController:
@PostMapping("/CallDeptPost")
public String userCallDeptPost () {
/**
* url : 要调用的其他服务端的地址
* responseType : 被调用服务的方法的返回数据类型
*/
Map<String,String> map = new HashMap<>();
map.put("id","2");
map.put("name","招生部");
/**
* uri : 其他微服务地址
* request : 请求中的数据
* responseType :响应回的数据类型
* @ return : ResponseEntity --> 所有响应数据
* 响应消息头
* 响应消息行
* 响应正文 --> 有dept返回的ResultObject数据
*/
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:8002/dept/beCalledPost", map, String.class);
// getBody 获取其中的响应正文
String body = stringResponseEntity.getBody();
return body;
}
注意:被调用的方法必须使用@RequestBody注解接收
DeptController:
@PostMapping("/beCalledPost")
public Object beCalledPost(@RequestBody Map<String,String> map){
System.out.println("dept beCalledPost : 被访问了");
return "{\"code\":\"8002\",\"id\":\""+map.get("id")+"\",\"name\":\""+map.get("name")+"\"}";
}
注意: 项目的名称不变 ,
spring:
application:
name: dept
server:
port: 8003 #服务端口
eureka:
instance:
hostname: dept8003
instance-id: dept8003
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:7000/eureka
spring:
application:
name: dept
在User模块的pom文件添加Ribbon的pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
在User微服务模块的RestTemplateConfig加@LoadBalanced //开启负载均衡
在User模块的调用Dept服务的方法中,url不能使用IP+RORT来访问,需要使用Dept的项目名称。