创建client_two
像之前创建client一样创建一个client_two,将端口号改为8081,名称改为client2,如下
eureka:
client:
service-url: http://localhost:8761/eureka #注册服务器地址
instance:
hostname: client2
spring:
application:
name: client2
server:
port: 8081
创建ServerController
这里我们将之前的client当作服务端,client_two当作客户端来调用client的接口,在client项目中创建ServerController,如下
@RestController
public class ServerController {
@GetMapping("/msg")
public String msg(){
return "server msg";
}
}
创建ClientController
在client_two中创建ClientController,如下
@RestController
public class ClientController {
private Logger logger = LogManager.getLogger(this.getClass());
@GetMapping("/clientMsg")
public String clientMsg(){
//第一种方式
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/msg",String.class);
logger.info("response:" + response);
return response;
}
}
这里使用RestTemplate方式来调用服务端的接口,启动服务注册中心server,启动服务client,client_two,访问http://localhost:8081/clientMsg,页面将会显示"server msg"
这种方式的缺点是访问URL的地址写死,实际应用中是不能使用这种方式的,当有多个地址也就是集群的时候就需要用到负载均衡了
使用LoadBalancerClient获取url
更改client的配置文件如下
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注册服务器地址
instance:
prefer-ip-address: true
spring:
application:
name: client #应用名
这里将prefer-ip-address设为true是为了服务以IP注册。
如下图复制一个client,在VM options中通过输入-Dserver.port=9080设置端口为9080,名称设为ClientApplication1
保存之后启动ClientApplication1,访问http://localhost:8761/ 服务注册中心,如下图Client有两个地址注册上去了
image.png
更改ClientController如下:
@RestController
public class ClientController {
private Logger logger = LogManager.getLogger(this.getClass());
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/clientMsg")
public String clientMsg(){
//第一种方式
/* RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/msg",String.class);*/
//第二种方式
ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENT");
String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()) + "/msg";
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url,String.class);
logger.info("response:" + response);
return response;
}
}
启动client_two访问http://localhost:8081/clientMsg发现也没问题
第三种方式
client_two中新建RestTemplateConfig
@Component
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
修改ClientController
@RestController
public class ClientController {
private Logger logger = LogManager.getLogger(this.getClass());
/* @Autowired
private LoadBalancerClient loadBalancerClient;*/
@Autowired
private RestTemplate restTemplate;
@GetMapping("/clientMsg")
public String clientMsg(){
//第一种方式
/* RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/msg",String.class);*/
//第二种方式
/* ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENT");
String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()) + "/msg";
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(url,String.class);*/
//第三种方式
String response = restTemplate.getForObject("http://CLIENT/msg",String.class);
logger.info("response:" + response);
return response;
}
}
启动client_two访问http://localhost:8081/clientMsg发现也没问题
源码地址: https://gitee.com/swlfly/SpringCloud_Learn/tree/1.0/
更多技术文章可关注个人公众号: 码农Fly
网友评论