1.Eureka

Eureka是spring cloud中的服务框架,提供基于rest的服务。它将管理整个系统注册的服务,维护这些服务并检查它们的状态。简单来说就像超市收银台,所有的商品都会在收银台有记录,客人买单的商品如果没有记录(注册)是不能够购买的,不经过收银台也是不能直接带走商品的。可能不太形象,让我想就这个样子。
114342_7m9L_3665821.png

好了直接开始撸代码。

2.创建Eureka应用

使用你的ide创建一个maven工程,我使用的是spring官方推荐的STS,你可以使用其它ide。

在pom文件里加入如下依赖:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

编写启动类:

package com.demo.Eureka;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaServer
public class App 
{  
    public static void main( String[] args )
    {
        new SpringApplicationBuilder(App.class).run(args);
    }
}

配置服务端口以及其它信息:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
server:
  port: 8761

本身不提供服务也不抓取服务,只是作为服务管理中心,所以配置false,否则启动会抱错。
然后启动应用,访问:http://localhost:8761,就可以看到Eureka的管理界面,目前的服务数为0,应为还没有注册服务。

3.创建服务提供者

我们需要在Eureka中注册一个服务,这样就可以访问这个服务,对外提供服务。整个项目的搭建过程和上面的大致一样,只是依赖的jar包有所不同。

添加依赖:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

编写启动类:

package com.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class App {
   public static void main(String[] args) {
     new SpringApplicationBuilder(App.class).run(args);
}
}

编写一个提供服务的api:

package com.demo;

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

@RestController
public class RestApi {
    
    @GetMapping("/api/hello")
    public String Hello(){
        return "hello world";
    }
}

配置项目参数,注册服务到前面创建的Eureka服务管理中心:

spring:
  application:
    name: provider-server1
server:
  port: 8091
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动应用,访问http://localhost:8761,可以看到已经注册了一个名叫provider-server1得服务。

4.创建服务调用者

服务注册发布到Eureka服务器后就需要去调用它,所以要有一个调用者,它同样也要注册发布到服务器中,然后才能在服务器内部调用其他服务。当然不用调用者,我们也可以直接通过浏览器调用我们刚才写的hello api。但是这样就没办法实现集群,以及负载均很。

加入依赖:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

编写启动类:(就不写了,和上面的一样)

建立配置文件application.yml:

spring:
  application:
    name: invoker-server1
server:
  port: 8092
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

编写服务调用类:

package com.demo;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Configuration
public class CallService {
    @Bean
    @LoadBalanced
    public RestTemplate getRTL(){
        return new RestTemplate();
    }

    @GetMapping("/call")
    public String call(){
        RestTemplate rt = getRTL();
        return rt.getForObject("http://provider-server1/api/hello", String.class);
    }
}

@LoadBalanced注解使调用服务负载均衡。
依次启动Eureka服务器,provider-server1,invoker-server1。访问http://localhost:8761/,可以看到已经有两个服务启动,然后访问http://localhost:8092/call,可以看到输出:hello world,说明 调用服务成功。


Mike晓
95 声望18 粉丝