Micro calls between spring cloud service

SpringCloud in order to solve the problem of service and service calls, and provides two ways. RestTemplate and Feign. Although these two different ways to call, but still the same at the bottom and HttpClient, using http way call. Encapsulation of HttpClient performed. Let us explain in detail the difference between these two approaches, we first look at the RestTemplate way.

RestTemplate way calling

Detecting whether the service registry is registered to the service center.

Directly on the code:

package com.mt.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class MtSpringcloudFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(MtSpringcloudFeignClientApplication.class, args);
}

@Bean
public RestTemplate initRestTemplate() {
return new RestTemplate();
}
}

RestTemplate a way to call

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@GetMapping("/get")
public Object get() {
String result = template.getForObject("http://127.0.0.1:8085/server/get", String.class);
return result;
}
}

RestTemplate call Second way

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/get")
public Object get() {
ServiceInstance serviceInstance = loadBalancerClient.choose("jilinwula-springcloud-feign-server");
String url = String.format("http://%s:%s/server/get", serviceInstance.getHost(), serviceInstance.getPort());
String result = template.getForObject(url, String.class);
return result;
}
}

It provides LoadBalancerClient interface SpringClourd in. We can get the address and port of the service through the Application's name through the center of the user interface.

Three ways to call RestTemplate

Change the startup class:

package com..feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class MtSpringcloudFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(MtSpringcloudFeignClientApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate initRestTemplate() {
return new RestTemplate();
}
}

In RestTemplate instantiated place added @LoadBalanced notes, in respect of the notes will automatically replace the call interface addresses into real service address we use RestTemplate. Here we look at the Controller changes:

package com.mt.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/client")
public class Controller {
@Autowired
private RestTemplate template;
@GetMapping("/get")
public Object get() {
String url = String.format("http://%s/server/get", "tcmp-measure-service");
String result = template.getForObject(url, String.class);
return result;
}
}

Code and the first code essentially the same, the only difference is to get local service address and port of replacing Application name became the registry, and our RestTemplate on first use and there is no difference, but in the url different.

Above it is all there, in the actual project development, these two methods can be achieved between service calls and service, and these two methods have drawbacks, it is not particularly recommended way.

Micro calls between spring cloud service

Guess you like

Origin www.cnblogs.com/mengtaoadmin/p/11184041.html