Spring Cloud Eureka 入门之服务消费者详解

企业动态
此小章节介绍了如何 Eureka 作为服务消费者,并向服务注册中心注册自己实例,更重要的是发现其他服务,并调用其他服务。

[[200512]]

本文提纲

1. springcloud-eureka-sample 工程介绍

2. 运行 springcloud-eureka-client-customer 服务消费者工程

3. 详解 springcloud-eureka-client-customer 服务消费者工程

一、springcloud-eureka-sample 工程介绍

还是回到Eureka 集群简单架构图:

 

***小节《Spring Cloud Eureka 入门之服务注册中心详解 ...》实现了 Eureka Server 作为注册中心,

第二小节《Spring Cloud Eureka 入门 (二)服务提供者详解》是 Provider Service B 的案例,实现了 Eureka Cleint 作为服务提供者,包括其服务的注册和心跳的功能。

本小节,是 Provider Service A 的案例,实现了 Eureka Cleint 作为服务消费者,包括其服务的注册和心跳的功能,还有其服务发现和通过 Ribbon 进行服务调用的功能。

springcloud-eureka-client-customer 服务消费者工程,他本身也是一个服务提供者。即具有服务提供功能和服务消费功能。下面去运行该工程

二、运行 springcloud-eureka-client-customer 服务消费者工程

运行环境:JDK 7 或 8,Maven 3.0+

技术栈:Spring Cloud Dalston.SR1、 spring-cloud-netflix 1.3.1、Spring Boot 1.5.4

自然,我们先得去上一小节《Spring Cloud Eureka 入门 (一)服务注册中心详解》 ,把注册中心工程启动完毕。

1. git clone 下载工程 springcloud-learning-example

项目地址见 GitHub - https://github.com/JeffLi1993/springcloud-learning-example

git clone https://github.com/JeffLi1993/springcloud-learning-example.git 
  • 1.

2. Maven 编译安装这个工程:

cd springcloud-learning-example 
mvn clean install 
  • 1.
  • 2.

3. 运行 Eureka 工程 springcloud-eureka-client-customer

启动 springcloud-eureka-client-customer 工程启动类 CustomerApplication,启动服务注册中心工程。

EurekaServerApplication 类路径:/springcloud-learning-example/springcloud-eureka-sample/springcloud-eureka-client-customer/src/main/java/org/spring/springcloud/CustomerApplication.java

控制台 Console 看到这类信息,代表启动成功:

2017-07-12 18:19:21.725  INFO 11314 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CUSTOMER-SERVICE/10.18.29.64:customer-service:8081: registering service... 
2017-07-12 18:19:21.814  INFO 11314 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CUSTOMER-SERVICE/10.18.29.64:customer-service:8081 - registration status:  
2042017-07-12 18:19:21.916  INFO 11314 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http) 
2017-07-12 18:19:21.918  INFO 11314 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8081 
2017-07-12 18:19:21.925  INFO 11314 --- [           main] o.s.springcloud.CustomerApplication      : Started CustomerApplication in 17.075 seconds (JVM running for 18.141) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

可以看出,注册了应用名为 CUSTOMER-SERVICE 的应用,该服务提供者的工程端口为 8081

4. 访问 Eureka 注册中心可视化界面

打开浏览器,访问 http://localhost:8888/ ,如图所示:

可以看到,服务提供者向服务注册中心注册自己的实例,展示了应用名和端口信息等。

5.访问服务消费者案例

打开浏览器,访问 http://localhost:8081/customer,如图所示:

可以看出,下面一句消息 Hello,Provider! ,是服务消费者调用服务提供者获取的信息。

三、详解 springcloud-eureka-client-customer 服务消费者工程

1.springcloud-eureka-client-customer 工程目录结构

├── pom.xml└── src 
    └── main 
        ├── java 
        │   └── org 
        │       └── spring 
        │           ├── springcloud 
        │           │    └──  CustomerApplication.java 
        │           └── web 
        │                └──  CustomerController.java 
        └── resources 
            └── application.yml 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

ProviderApplication.java Eureka Discovery Client 启动类,启动服务消费者工程,本身也会注册到注册中心,也能发现其他服务。

CustomerController.java 服务消费者 HelloWorld 案例

application.yml 配置文件

2. pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B 
    <modelVersion>4.0.0</modelVersion>    <groupId>springcloud</groupId>     
<artifactId>springcloud-eureka-client-customer</artifactId>     
<version>0.0.1-SNAPSHOT</version>     
<name>springcloud-eureka-client-customer :: 服务消费者</name>  
 
   <!-- Spring Boot 启动父依赖 --> 
    <parent> 
        <groupId>org.springframework.boot</groupId>         
   <artifactId>spring-boot-starter-parent</artifactId>        
    <version>1.5.4.RELEASE</version>     
    </parent> 
 
 
 
    <dependencies> 
        <!-- Spring Cloud Netflix Eureka 依赖 --> 
        <dependency> 
            <groupId>org.springframework.cloud</groupId>             
   <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency> 
 
 
        <!-- Spring Boot Test 依赖 --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId>  
              <artifactId>spring-boot-starter-test</artifactId>  
              <scope>test</scope>        
               </dependency> 
    </dependencies> 
 
 
    <dependencyManagement> 
        <dependencies> 
                           <!-- Spring Cloud Netflix 依赖 --> 
            <dependency> 
                <groupId>org.springframework.cloud</groupId>  
                                          
                                         <artifactId>spring-cloud-netflix</artifactId>               
                                           <version>1.3.1.RELEASE</version>               
                                             <type>pom</type> 
                                                             <scope>import</scope>            
                                                              </dependency> 
        </dependencies> 
 
 
    </dependencyManagement> 
    <build> 
        <plugins> 
            <plugin> 
 
 
                <groupId>org.apache.maven.plugins</groupId>  
                                                                             <artifactId>maven-compiler-plugin</artifactId>              
                                                                                <configuration> 
 
 
                    <source>1.8</source>     
                                                                                                <target>1.8</target>   
                                                                                                              </configuration> 
 
 
            </plugin> 
        </plugins> 
    </build></project> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.

使用的依赖是

- spring-cloud-netflix 1.3.1 是 Spring Cloud Dalston.SR1 版本。

- spring-cloud-starter-eureka Eureka Client 模块依赖,包含了客户端 client 的依赖,还有 Ribbon 的依赖,如:org.springframework.cloud:spring-cloud-netflix-eureka-client:1.3.1.RELEASE

org.springframework.cloud:spring-cloud-starter-ribbon:1.3.1.RELEASE

3. application.yml 配置

server: 
  port: 8081 # 服务端口 
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:8888/eureka/ # 服务注册中心地址 
 
spring: 
  application: 
    name: customer-service # 服务名称 
 
 
- server.port 设置工程服务端口 
- eureka.client.service-url.defaultZone 设置服务注册中心地址 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

4.服务消费者应用启动类

/** 
 * Spring Boot Eureka Server 应用启动类 
 * 
 * Created by bysocket on 21/06/17. 
 */ 
 
  
@EnableDiscoveryClient // Eureka Discovery Client 标识 
@SpringBootApplication // Spring Boot 应用标识 
 
public class CustomerApplication {     
@Bean 
    @LoadBalanced 
    RestTemplate restTemplate() {         
return new RestTemplate(); 
    }     
 
public static void main(String args) {         
// 程序启动入口 
         
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 
        SpringApplication.run(CustomerApplication.class,args); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

@EnableDiscoveryClient 标志该应用作为 Eureka Client ,并会自动化读取 Eureka 相关配置。还有向服务注册中心发现服务并进行调用。

@LoadBalanced 标志着 RestTemplate 是通过 Ribbon 客户端负载均衡去调用服务提供者集群的。即可以在获取的服务提供者实例列表中,通过 Ribbon 进行选择某实例,然后调用该服务实例。

6.服务消费者 Hello World 案例

/** 
 * Customer HelloWorld 案例 
 * <p> 
 * Created by bysocket on 06/22/17. 
 */ 
 
  
@RestController 
 
public class CustomerController {     
 
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);     
 
@Autowired 
    private RestTemplate restTemplate; // HTTP 访问操作类 
 
 
    @RequestMapping("/customer")     
 
public String customer() { 
 
        String providerMsg = restTemplate.getForEntity("http://PROVIDER-SERVICE/provider"
                String.class).getBody();         
 
         
 
        return "Hello,Customer! msg from provider : <br/><br/> " + providerMsg; 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

可以看到注入了 RestTemplate 对象,它是 HTTP 访问操作类。

然后 customer 方法,通过 restTemplate 通过 HTTP 协议调用服务提供者暴露的 provider 接口,并获取服务提供者的结果。然后组装输出。

四、小结

此小章节介绍了如何 Eureka 作为服务消费者,并向服务注册中心注册自己实例,更重要的是发现其他服务,并调用其他服务。

【本文为51CTO专栏作者“李强强”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2017-07-11 14:48:33

Spring Clou服务提供者

2017-07-03 08:29:42

Spring Clou服务详解

2022-07-07 09:00:49

RocketMQ消费者消息消费

2024-07-10 10:51:39

SpringEureka数据中心

2023-06-01 08:08:38

kafka消费者分区策略

2017-06-26 09:06:10

Spring Clou微服务架构

2015-08-26 09:39:30

java消费者

2022-08-08 10:55:31

5G物联网智能手机

2011-08-05 16:21:24

2011-07-22 16:25:38

CA TechnoloIT消费化

2017-06-25 13:33:25

Spring Clou微服务架构

2009-08-13 13:14:31

C#生产者和消费者

2021-04-20 08:32:51

消息MQ队列

2017-08-09 15:50:47

Spring Clou微服务架构

2017-08-10 11:15:05

Spring Clou微服务架构

2015-06-15 11:29:34

数据中心绿色数据中心

2021-12-22 11:00:05

模型Golang语言

2021-01-14 07:54:19

Spring Clou应用路由

2013-03-15 10:45:42

戴尔服务

2009-04-15 11:17:23

点赞
收藏

51CTO技术栈公众号