@RequestMapping("/persons")
public Persons getPersonById() {
final Persons persons = new Persons();
final Person person = new Person();
person.setName("Tomas");
person.setSurname("Kloucek");
person.setDepartment("Programmer");
persons.getPersons().add(person);
return persons;
}
}
Spring Cloud MicroService的客户端
现在你可以访问http://localhost:8080/persons ,也可以使用使用RestTemplate 直接访问这个微服务, 但这样做是愚蠢的。更聪明的是让您的客户端通过MicroService id(在我的情况下是personService)首先访问注册服务器,询问Ribbon loadbalancer负载平衡器来获取微服务的URL,然后调用该服务。查看客户端代码:
@HystrixCommand(fallbackMethod = "invokeMicroServiceFallback",
commandProperties = {
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "100"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "20000"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "6"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")
}
)
public void invokeMicroService() {
final RestTemplate restTemplate = new RestTemplate();
final ServiceInstance serviceInstance = loadBalancer.choose("personsService");
if (serviceInstance != null) {
System.out.println("Invoking instance at URL: "+serviceInstance.getUri());
System.out.println("Result :"+
restTemplate.getForObject(serviceInstance.getUri()+"/persons",
String.class));
} else {
System.out.println("Service is down...");
throw new IllegalStateException("PersonsService is not running!");
}
}
public void invokeMicroServiceFallback() {
System.out.println("Waiting for circuit-breaker to close again...");
}
}
运行发现服务器:
mvn clean install (in the spring-microservice-registry directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war
运行微服务
mvn clean install (in the spring-microservice-service directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war
mvn clean install (in the spring-microservice-client directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war
启动客户端你会看到:
Invocation number :16
Invoking instance at URL: http://192.168.1.112:8080
Result :{"persons":[{"name":"Tomas","surname":"Kloucek","department":"Programmer"}]}
Invocation number :17
Invoking instance at URL: http://192.168.1.112:8080
Result :{"persons":[{"name":"Tomas","surname":"Kloucek","department":"Programmer"}]}
Invocation number :18
Invoking instance at URL: http://192.168.1.112:8080
Result :{"persons":[{"name":"Tomas","surname":"Kloucek","department":"Programmer"}]}
Invocation number :19
关闭personService微服务 20秒, 你会看到输出:
Invocation number :18
Invoking instance at URL: http://192.168.1.112:8080
Waiting for circuit-breaker to close again...
Invocation number :19
Invoking instance at URL: http://192.168.1.112:8080
Waiting for circuit-breaker to close again...
一会儿看到输出:
Invocation number :78
Waiting for circuit-breaker to close again...
Invocation number :79
Waiting for circuit-breaker to close again...
@RequestMapping("/persons")
public Persons getPersonById() {
final Persons persons = new Persons();
final Person person = new Person();
person.setName("Tomas");
person.setSurname("Kloucek");
person.setDepartment("Programmer");
persons.getPersons().add(person);
mvn clean install (in the spring-microservice-registry directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war
运行被调用微服务:
mvn clean install (in the spring-microservice-service directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war
verify with http://localhost:9761 that MicroService is registered.
运行调用微服务:
mvn clean install (in the spring-microservice-client directory with pom.xml)
java -jar target/demo-0.0.1-SNAPSHOT.war [0-2]