上一篇完成了注册中心,服务提供者,这篇来完成服务消费,当然,不管是服务提供者还是消费者,对于注册中心来说,就是注册在注册中心的一个微服务。
1. rabbon方式微服务消费
新建springboot工程,选择支持如下,其中Discovery和Server任选一个就可以了
配置文件:
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://localhost:8000/eureka/
server:
port: 8002
spring:
application:
name: ribbon-client
cloud:
config:
discovery:
enabled: true
serviceId: eureka-server
主类添加支持
@EnableEurekaClient
@SpringBootApplication
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
@Bean
@LoadBalanced //负载均衡方式调用
RestTemplate restTemplate() { //rest调用模板
return new RestTemplate();
}
}
编写一个service调用服务提供者 的服务:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name="+name,String.class);
}
}
对外提供一个接口测试:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hello")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
启动程序。观察注册中心页面注册成功
调用8002的接口,8001服务提供者返回数据:
注意:客户端使用的是@EnableEurekaClient,而不是@EnableDiscoveryClient,
否则可能无法注册到注册中心,调用服务时报错:No instances available for xxxx
原因:https://blog.csdn.net/u012734441/article/details/78256256?locationNum=1&fps=1
2. feign方式微服务消费
配置文件同rabbon一样
主运行类:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
调用服务提供接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-client")
public interface HelloService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
String restFromClientOne(@RequestParam(value = "name") String name);
}
测试接口:
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return helloService.restFromClientOne(name);
}
}
测试方法同rabbon
3. 负载均衡
目前为止,我们有4个工程:
注册中心:eurekaServer
服务提供者:eurekaClient
服务消费者:ribbonClient 和feignClient
负载测试方案:(1)eurekaServer启动不变
(2)eurekaClient 启动三个实例(把eurekaClient启动三次),注意每次分别改动启动端口为:8001,8002, 8003
(3)选取ribbonClient或者feignClient之一,启动端口改为8010
全部启动后,注册中心实例列表 如下:
调用测试接口:http://localhost:8010/hello?name=gray,不断刷新,发现接口提供者在三个实例之前切换,效果如下:
这也是我们在消费时使用服务名而非ip+端口的形式访问,因为后者或锁定实例,失去负载均衡的意义。