服务的发现与消费《Spring Cloud Eureka系列四》

接着上一个系列,我们这次创建一个简单的小示例,看看再Eureka的服务治理体系下,如何实现服务的发现与消费?

1、我们做一些准备的工作,开启上节中的两个服务:

java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1

java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2

2、启动成功之后就能看见如下图所示的8080端口和8081端口的服务;

服务的发现与消费《Spring Cloud Eureka系列四》

3、创建创建一个springboot的工程作为消费者,取名为:ribbon-consumer,并在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/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.didispace</groupId>
   <artifactId>ribbon-consumer</artifactId>
   <version>1.0.0</version>
   <packaging>jar</packaging>

   <name>eureka-server</name>
   <description>Spring Cloud In Action</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.3.7.RELEASE</version>
      <relativePath/>
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>
   </properties>

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

      <!--<dependency>-->
         <!--<groupId>org.springframework.boot</groupId>-->
         <!--<artifactId>spring-boot-starter-actuator</artifactId>-->
      <!--</dependency>-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-ribbon</artifactId>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.SR5</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>
   
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

4、更改启动类:

package com.didispace;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient  //注册为Eureka客户端
@SpringBootApplication
public class Application {
   /**
    * 在该类中创建RestTemplate实例,并通过@LoadBalanced注解开启客户端负载均衡
    * @return
    */
   @Bean
   @LoadBalanced
   RestTemplate restTemplate(){
      return new RestTemplate();
   }

   public static void main(String[] args) {
      new SpringApplicationBuilder(Application.class).web(true).run(args);
   }

}

5、创建ConsumerController,如下图:

package com.didispace;

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

/**
 * Created by lmy on 2018/3/7.
 */
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        return restTemplate.getForEntity("http://EUREKA-SERVER/hello",String.class).getBody();

    }
}

6、启动ribbon-consumer应用后,我们可以看到Eureka信息面板中,有三个服务,如下图所示:

服务的发现与消费《Spring Cloud Eureka系列四》


7、访问http://localhost:9000/ribbon-consumer,可以看到返回helloWorld,证明已成功;此时在ribbon-consumer程序的控制台上可以看到这些,如图所示:

服务的发现与消费《Spring Cloud Eureka系列四》

有一些:各个实例请求总数量、第一次连接的信息、上一次连接的信息、总的请求失败的数量等等。。。。。