SpringCloud achieve inter-service call (RestTemplate way)

Previous article "SpringCloud set up registration centers registered with the service," describes the set-up and registration services registry, this article will introduce the next customer service call process service providers.

Article directory

First, the service call process Second, the service provider Third, the service consumer Fourth, combat service call

First, the service call process

The overall process is to first start the registration center, service provider and registered to provide services to the registry, consumer access to services from the registry and executed.

Achieve service call needs to have three roles: service registry, service providers and service consumers, which the service registry and service providers on those who have achieved an article, demonstrates in detail below under the service consumers to build and call the service process .

Second, the service provider

Previous article next transformation service provider, add a controller, UserController.java code is as follows:

/**
 * 用户服务
 */

@Slf4j
@RestController
@RequestMapping("/provider")
public class UserController {

    static Map<Integer, User> userMap = new HashMap<>();

    static {
        //模拟数据库
        User user1 = new User(1"张三""123456");
        userMap.put(1, user1);
        User user2 = new User(2"李四""123123");
        userMap.put(2, user2);
    }

    /**
     * 根据id 查询
     */

    @RequestMapping("/getUser")
    public String getUser(Integer id) {
        User user = userMap.get(id);
        return JSON.toJSONString(user);
    }

}

Third, the service consumer

First create a SpringBoot project named spring-cloud-user-consumer, then follow the steps writing code.

  1. pom.xml Code

Add dependent eureka-server code is as follows:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version><!-- eureka版本 -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. Start Class Code

Start class add annotations @EnableDiscoveryClient, after adding the comment, the project has registered a service function, the code is as follows:

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudUserConsumerApplication {

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

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudUserConsumerApplication.class, args);
    }

}
  1. Profiles

Use yml profile, application.yml configuration is as follows:

server:
  port: 8082 #服务端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/
spring:
  application:
    name: user-service-consumer

Fourth, combat service call

  1. Start Service Center and register services

After writing the code, in order to start spring-cloud-eureka, spring-cloud-user-service and spring-cloud-user-consumer, to access the registry http: // localhost: 9001 /, figure illustrates the emergence of two registration centers and a service and register success.


Registry run shot
  1. Service call

Open the postman visit http: // localhost:? 8082 / consumer / getUser id = 2, the following figure appears description of the service call has been successful.


postman calls the shots

 

SpringCloud implementation calls this function has been achieved between all the services in question are welcome to communicate a message Oh!

Complete Source Address: https://github.com/suisui2019/springboot-study

Recommended Reading
1.SpringCloud set up registration centers registered with the service
2.SpringBoot integration ActiveMQ, see this is enough!
3. Do not Cookin log in Java code, this is the correct posture to fight the log!
4. coding artifact Lombok, after learning to improve development efficiency at least double!
5. Use Spring Boot + zxing, generate two-dimensional code can be so simple


Limited Time receive a free Java-related information, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo / Kafka, Hadoop, Hbase, Flink high concurrent distributed, big data and machine learning techniques.
Watch below the number to receive a free public:

Java annoying chatters public No. Java annoying chatters public No.

 

Guess you like

Origin www.cnblogs.com/haha12/p/11612887.html