Hello, I’m Chu Chen. In this chapter, we’ll learn about communication between SpringCloud components. Questions and comments can be sent to [email protected]

One: Create a SpringCloud Client

The creation process is the same as SpringCloud Practice 2: Client creation and high availability

The name of my service is Servce, so you can create it

The created service structure and configuration file are as follows



Set the port to 8083



Start Eureka, Client, and Servce successively

Go to http://localhost:8761/



The newly created Servce service has been registered

Two: Create a Controller

Create the ServerController in the Servce service

package com.example.server.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServerController {

    @GetMapping("getMsg")
    public String getMsg() {return "This is ServerMsg"; }}Copy the code

3. Use the RestTemplate method

1: Create clientController.java

package com.chuchen.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientController {

    @GetMapping("/getServerResult")
    public String getServerResult() {return restTemplate.getForObject("http://localhost:8083/getMsg",String.class); }}Copy the code

Restart the service, browser type

http://localhost:8081/getServerResult



We can see the result of calling the Servce service from the Client service

2: optimization

As we mentioned earlier, SpringCloud components are highly available. We may deploy multiple services for each service, and even on different machines, the IP and port of the service may be different. This method obviously does not meet our needs, so how to optimize it?

1: Create config→ ResttemPlateconfig. Java

package com.chuchen.client.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {returnnew RestTemplate(); }}Copy the code

2: Modify clientController.java

package com.chuchen.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getServerResult")
    public String getServerResult(){// Where SERVER is the name of the service to be requestedreturn restTemplate.getForObject("http://SERVER/getMsg",String.class); }}Copy the code

Restart the service, browser type

http://localhost:8081/getServerResult



We can see that it is still possible to get the requested result.

Four: Use OpenFeign

1: Introduce OpenFeign dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>Copy the code

2: Add @enableFeignClients to the start class



3: Create client→ServerClient

package com.chuchen.client.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // The value of the name attribute is the name of the component to call @feignClient (name =)"server") public interface ServerClient {// Call @getMapping ("/getMsg")
    String getMsg();
}
Copy the code

4: Modify ClientController

package com.chuchen.client.controller;

import com.chuchen.client.client.ServerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ClientController {

    @Autowired
    private ServerClient serverClient;

    @GetMapping("/getServerResult")
    public String getServerResult() {returnserverClient.getMsg(); }}Copy the code

5: test

Restart the service, browser type

http://localhost:8081/getServerResult


Five: the ending

Thank you for your support, and this series of articles will continue to be updated. Thank you.

For springBoot do not know friends can see my springBoot series tutorial