31.Springboot中使用RestTemplate-创新互联

一. 前言

在南阳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计、成都网站建设 网站设计制作按需网站开发,公司网站建设,企业网站建设,品牌网站制作,营销型网站,外贸营销网站建设,南阳网站建设费用合理。

官网使用说明

获取Eureka实例

public String serviceUrl() {
    InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
    return instance.getHomePageUrl();
}

步骤:

二. 导入包

pom.xml


    org.springframework.cloud
    spring-cloud-starter-eureka


    org.springframework.cloud
    spring-cloud-starter-hystrix

三. 修改启动Application

@EnableCircuitBreaker
@EnableDiscoveryClient
public class HellloMain {
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(HelloMain.class, args);

	}
	
}

四. 业务使用

private int xxxx(String body) {
	RestTemplate restTemplate = new RestTemplate();
	HttpHeaders headers = new HttpHeaders();
	MediaType type = MediaType.parseMediaType("application/json");
	headers.setContentType(type);
		
	HttpEntity formEntity = new HttpEntity(body, headers);
		
	String result = restTemplate.postForObject(getEurkaClient("hello-module"), 
				formEntity,
				String.class);
	return 0;
}
public String getEurkaClient(String end) {
        InstanceInfo instance = discoveryClient.getNextServerFromEureka("smarthome-phihome", false);
	return instance.getHomePageUrl() + end;
}

这样就可以在应用程序之间互相调用


网站题目:31.Springboot中使用RestTemplate-创新互联
网页路径:http://myzitong.com/article/jcgjd.html