I. Preface
Instructions for use of official website
Get Eureka Instance
Public String serviceurl () {Instanceinfo instance = Discoveryclient.getnextserverfromeureka ("STORES", false); return Instance.gethomepageurl ();}
Steps:
Two. Importing packages
Pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-eureka</artifactid></dependency><dependency> <groupId> Org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-hystrix</artifactId>< /dependency>
Three. Modify the startup application
@EnableCircuitBreaker @enablediscoveryclientpublic class Helllomain {@Bean @loadbalancedresttemplate resttemplate () { return new Resttemplate ();} public static void Main (string[] args) {Springapplication.run (hellomain.class, args);}}
Four. Business use
private int xxxx (String body) {resttemplate resttemplate = new Resttemplate (); Httpheaders headers = new Httpheaders (); MediaType type = Mediatype.parsemediatype ("Application/json"); Headers.setcontenttype (type); httpentity<string> formentity = new Httpentity<string> (body, headers); String result = Resttemplate.postforobject (Geteurkaclient ("Hello-module"), Formentity,string.class); return 0;}
public string Geteurkaclient (string end) {Instanceinfo instance = Discoveryclient.getnextserverfromeureka ("Smartho Me-phihome ", false); return Instance.gethomepageurl () + END;}
This makes it possible to call each other between applications
This article is from the "www.bogo.com" blog, make sure to keep this source http://483181.blog.51cto.com/473181/1954798
Using Resttemplate in Springboot.