On the spring cloud Registry and the client registration, so this article is about how services and services are called
Not set up a small partner please refer to my previous blog: Idea Quick build Spring cloud-Registration Center and Registration
Based on the previous article I built a client-side microservices service:
So now there are two microservices, and what we're implementing is a call between MicroServices 1 and MicroServices 2
The registry will not have to say, specific look at the two micro-services
APPLICATION.YML configuration also needless to say, do not know how to configure, please refer to my previous blog
In the Constroller in PROJECT-SOLR:
@RestController// This makes all the methods in this Constroller return not the page publicclass Solrsearchconstroller { @RequestMapping ("/solrsearch") public String Solrsearch () { return "Here is Solr"; }}
This is to make another service call
In another microservices Project-shopping-mall startup class, we must define a method:
@SpringBootApplication @enablediscoveryclient//represents the Eureka client Public classShoppingmallprovider {@Bean @LoadBalanced//find microservices in the registration center Publicresttemplate resttemplate () {resttemplate resttemplate=Newresttemplate (); returnresttemplate; } Public Static voidMain (string[] args) {Springapplication.run (shoppingmallprovider.class, args); }}
The Constroller in PROJECT-SOLR is then called in the controller in the Project-shopping-mall:
@Controller public class Pagecontroller {@Autowired private resttemplate resttemplate; @RequestMapping ( "/toindex" public string Toindex (model model) {String msg =resttemplate.getforentity ("http ://project-solr/solrsearch ", String. class ). GetBody (); // PROJECT-SOLR is the name of the calling registry Model.addattribute ("MSG" ,msg); return "/index" ; }}
Here PROJECT-SOLR is configured in the name of each service registered to the registry, according to the name of the service to invoke the IP address, you can implement the dynamic microservices call effect, it will not be replaced by the computer error
Next build the page, here I'm using the Thymeleaf component
We will first add dependencies to the Build.gradle:
// Thymeleaf Components Compile ' org.springframework.boot:spring-boot-starter-thymeleaf '
Then create a new default directory:
You must create a new templates package here, because it is the default directory
Then in index.html:
Run:
Success!
Here's a brief look at the self-protection mechanism of spring cloud Eureka Registry
Advantages: When the service and the registry for some reason, the service and service can also be connected, this time Eureka will not immediately clean up, will still change tiny information to save.
Cons: When a service is disconnected from the registry for some reason, the service is not able to connect with the service, which may lead to bad other servers.
Of course, the advantages outweigh the disadvantages.
How does the Eureka registry know that MicroServices are still alive?
In fact, each service every minute to the registry heartbeat, and the registry will accept the heartbeat, if the registry does not accept the heartbeat will think the service died
Official definition of self-protection mechanism: Eureka Official self-protection mechanism
Calls between microservices in spring Cloud and Eureka's self-protection mechanism