SpringCloud的EureKaClient之间调用及负载均衡
作者:互联网
本文通过订单服务OrderService调用用户服务UserService做案例
1.UserService
UserService的eurekaclient
@RestController
@SpringBootApplication
public class UserApp {
@Value("${server.port}")
private String port;
public static void main(String[] args) {
new SpringApplicationBuilder(UserApp.class).web(WebApplicationType.SERVLET).run(args);
}
@RequestMapping("/")
public String findUserById(Integer id) {
return port+"_"+id;
}
}
UserService的yml
server:
port: 8001
spring:
application:
name: user-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.OrderService 的eurekaclient,将会调用UserService的方法
2.1 OrderService 的RestTemplate
package com.cloud.erkcli1;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class OrderConfig {
@LoadBalanced//负载均衡
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
RestTemplate是一个HTTP客户端,使用它我们可以方便的调用HTTP接口,支持GET、POST、PUT、DELETE等方法。
GET请求方法
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);
<T> T getForObject(URI url, Class<T> responseType);
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables);
<T> ResponseEntity<T> getForEntity(URI var1, Class<T> responseType);
getForObject方法
返回对象为响应体中数据转化成的对象,举例如下:
@GetMapping("/{id}")
public Result getUser(@PathVariable Long id) {
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
getForEntity方法
返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等,举例如下:
@GetMapping("/getEntityByUsername")
public Result getEntityByUsername(@RequestParam String username) {
ResponseEntity<Result> entity = restTemplate.getForEntity(userServiceUrl + "/user/getByUsername?username={1}", Result.class, username);
if (entity.getStatusCode().is2xxSuccessful()) {
return entity.getBody();
} else {
return new Result("操作失败", 500);
}
}
POST请求方法
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType);
postForObject示例
@PostMapping("/insert")
public Result insert(@RequestBody User user) {
return restTemplate.postForObject(userServiceUrl + "/user/insert", user, Result.class);
}
postForEntity示例
@PostMapping("/insert")
public Result insert(@RequestBody User user) {
return restTemplate.postForEntity(userServiceUrl + "/user/insert", user, Result.class).getBody();
}
PUT请求方法
void put(String url, @Nullable Object request, Object... uriVariables);
void put(String url, @Nullable Object request, Map<String, ?> uriVariables);
void put(URI url, @Nullable Object request);
PUT请求示例
@PutMapping("/update")
public Result update(@RequestBody User user) {
restTemplate.put(userServiceUrl + "/user/update", user);
return new Result("操作成功",200);
}
DELETE请求方法
void delete(String url, Object... uriVariables);
void delete(String url, Map<String, ?> uriVariables);
void delete(URI url);
DELETE请求示例
@DeleteMapping("/delete/{id}")
public Result delete(@PathVariable Long id) {
restTemplate.delete(userServiceUrl + "/user/delete/{1}", null, id);
return new Result("操作成功",200);
}
2.2 OrderService的请求
@RestController
@SpringBootApplication
public class OrderApp {
public static void main(String[] args) {
new SpringApplicationBuilder(OrderApp.class).web(WebApplicationType.SERVLET).run(args);
}
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.user-service}")
private String userServiceUrl;
@RequestMapping("/aaa")
public String findOrder(Integer id) {
ResponseEntity<String> responseEntity = restTemplate.getForEntity(userServiceUrl+"?id="+id, String.class);
return "order["+responseEntity.getBody()+"]";
}
}
2.3 OrderService的yml
server:
port: 8101
spring:
application:
name: order-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
service-url:
user-service: http://user-service
3测试
开启两个UserService,端口分别开启为8001/8002然后开启一个OrderService去调用.然后多次访问http://localhost:8101/aaa?id=3 .
结果分别是:order[8002_3]/order[8001_3]
可以看到服务调用及其负载均衡
标签:负载,String,EureKaClient,SpringCloud,Object,url,user,public,Result 来源: https://blog.csdn.net/dmw412724/article/details/115523534