(D) service governance of the service consumer springcloud

I. Overview

Front by Eureka Client component provider to create a service provider, and complete the registration in the registry, the next step to implement a service consumer consumer, provider call related interfaces.

Second, build a service consumer

To build a micro-service applications through the Spring Boot, then registered it to Eureka Server through Eureka Client. This time from the perspective of the provider and the consumer code and see no difference, are Eureka client, we artificially distinguish them from a business point of view, to provide services provider, consumer calling service, the specific implementation needs to be done in conjunction with RestTemplate that in the service consumer consumer to call the service provider provider of related interfaces RestTemplate.

1, create a sub-module maven consumer.

2, pom add Eureka Client dependency.

    <dependencies>
        <!--添加 Eureka Client 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

3, created under the resources path profile application.yml, add Eureka Client configuration, at this time of Eureka Client service consumer consumer.

server:
  port: 8020 #当前 Eureka Client 服务端口
spring:
  application:
    name: consumer #当前服务注册在 Eureka Server 上的名称
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/ #注册中心的访问地址
  instance:
    prefer-ip-address: true #是否将当前服务的 IP 注册到 Eureka Server

4, create a startup springboot start class consumerApplication and add RestTemplate configuration.

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

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

5, create a controller, injection RestTemplate instance, the service provider business method invocation by RestTemplate.

@RequestMapping("/consumer")
@RestController
public class StudentHandler {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
    }

// .....完整实现参考https://gitee.com/zhengj/myspringcloud.git......
}

6, complete project directory as follows:

7, in order to start registration center, service provider provider, and run ConsumerApplication.

Open a browser to access the registry interface http: // localhost: 8761 can be seen service providers and consumers have been registered to the registry.

8, then you can access the consumer-related services.

Third, the summary

使用 Eureka Client 组件在 Eureka Server 注册一个服务消费者 consumer 的具体实现,无论是服务消费者还是服务提供者,都通过 Eureka Client 组件来实现注册,实现服务消费者 consumer 之后,通过 RestTemplate 完成对服务提供者 provider 相关服务的调用。

 

完整demo下载地址:

https://gitee.com/zhengj/myspringcloud.git

 

 

 

 

 

发布了9 篇原创文章 · 获赞 0 · 访问量 333

Guess you like

Origin blog.csdn.net/m0_37874989/article/details/104580869