RestTemplate :
First write an interface in the controller of a project:
his application name is server:
spring:
application:
name: server
But in fact, what you see when you open eureka is capitalized in SERVER.
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Open the second project: to call the interface in the first project The
first way: call directly
RestTemplate restTemplate=new RestTemplate();
String msg=restTemplate.getForObject("http://localhost:8080/server/msg",String.class);
log.info("msg:{}",msg);
The second way: Use LoadBalanceClient.
If the other party opens multiple projects and engages in a cluster, then use LoadBalanceClient, which is the client's default polling method, select one for you to call. The meaning here is to give me the name of this application. If you open more than one application (here refers to the SERVER application, which means my project one), then it chooses one for me. I use getHost and getPort to know which one it gave me, and then call it with RestTemplate.
ServiceInstance serviceInstance=loadBalancerClient.choose("SERVER");
String url= String.format("http://%s:%s/server/msg",serviceInstance.getHost(),serviceInstance.getPort());
RestTemplate restTemplate=new RestTemplate();
String msg=restTemplate.getForObject(url,String.class);
log.info("msg:{}",msg);
The third method: simple processing of the second method:
here is the processing of RestTemplate, and then to spring processing:
so it can be called in the controller:
first inject the restTemplate
@Resource
private RestTemplate restTemplate;
Then call:
String msg=restTemplate.getForObject("http://SERVER/server/msg",String.class);
log.info("msg:{}",msg);
The host and port can be directly changed to the application name (SERVER):
briefly @Bean annotation and @Resource
When @Resource is injected, the name is the name of the property, not the class.
Here: I gave it to the spring as a bean:
so when I define the member property here, I must write the same name:
feign:
Dependency: It ’s very pitted here. It depends on the version. I ’m new here. If you use the old maven, you will get a red one. There is an upgraded dependency here: very complete: [Upgraded dependency], (https: // zhuanlan. zhihu.com/p/111927645)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Annotation of startup class: @EnableFeignClients
Write an interface class to call the api: (There is a pit here! Do not set the name of the interface class to FeignClient, because this is the same as its own FeignClient, it will cause an error !!!)
@FeignClient(name = "server")
public interface Client {
@GetMapping("/server/msg")
String msg();
@GetMapping("/server/category")
List<ProductCategory> category();
}
@FeignClient (name = "server") is to specify the application name, the project is set to sever.
Then behind is the @RequestMapping in the controller configured in project one.
Then inject the interface you just wrote in the controller:
use feign to call the interface