Eureka服务治理

Eureka是由Netflix开发的一款服务治理开源框架,Spring-cloud对其进行了集成。Eureka既包含了服务端也包含了客户端,Eureka服务端是一个服务注册中心(Eureka Server),提供服务的注册和发现,即当前有哪些服务注册进来可供使用;Eureka客户端为服务提供者(Server Provider),它将自己提供的服务注册到Eureka服务端,并周期性地发送心跳来更新它的服务租约,同时也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期性地刷新服务状态。这样服务消费者(Server Consumer)便可以从服务注册中心获取服务名称,并消费服务。

关系如下:

 

 

 #搭建服务注册中心:

说了那么多,我们先来搭建一个Eureka服务端来充当服务注册中心。

新建一个Spring Boot项目,artifactId填Eureka-Service,然后引入Spring Cloud Edgware.SR3spring-cloud-starter-eureka-server:

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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-security</artifactId>
        </dependency>

    </dependencies>

在启动类上添加@EnableEurekaServer注解,表明这是一个Eureka服务端:

 

 接着在application.yml中添加一些配置:

server:
  port: 8080
  
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

上面配置了服务的端口为8080,剩下几个为Eureka配置:

  • eureka.instance.hostname指定了Eureka服务端的IP;

  • eureka.client.register-with-eureka表示是否将服务注册到Eureka服务端,由于自身就是Eureka服务端,所以设置为false;

  • eureka.client.fetch-registry表示是否从Eureka服务端获取服务信息,因为这里只搭建了一个Eureka服务端,并不需要从别的Eureka服务端同步服务信息,所以这里设置为false;

  • eureka.client.serviceUrl.defaultZone指定Eureka服务端的地址,默认值为http://localhost:8761/eureka

配置完毕后启动服务,访问http://localhost:8080/,可看到:

QQ截图20180703185733.png

由于还没有Eureka客户端将服务注册进来,所以Instances currently registered with Eureka列表是空的。

下面我们接着搭建一个Eureka客户端来提供服务。

新建一个Spring Boot项目,artifactId填Eureka-Client,然后引入以下依赖:

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

接着编写一个TestController,对外提供一些REST服务:

@RestController
public class TestController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/info")
    public String info() {
        @SuppressWarnings("deprecation")
        ServiceInstance instance = client.getLocalServiceInstance();
        String info = "host:" + instance.getHost() + ",service_id:" + instance.getServiceId();
        log.info(info);
        return info;
    }
    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }
}

上面代码注入了org.springframework.cloud.client.discovery.DiscoveryClient对象,可以获取当前服务的一些信息。

编写启动类,在启动类上加@EnableDiscoveryClient注解,表明这是一个Eureka客户端:

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

最后配置application.yml:

server:
port: 8082

spring:
application:
name: Server-Provider

eureka:
client:
#Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8080/eureka/

稍微说明下这些配置的意思:

  • server.port指定了服务的端口为8082;

  • spring.application.name指定服务名称为Server-Provider,后续服务消费者要获取上面TestController中接口的时候会用到这个服务名;

  • eureka.client.serviceUrl.defaultZone指定Eureka服务端的地址,这里为上面定义的Eureka服务端地址;

  • eureka.client.register-with-eurekaeureka.client.fetch-registry上面已经解释了其意思,虽然这两个配置的默认值就是true,但这里还是显式配置下,使Eureka客户端的功能更为直观(即向服务端注册服务并定时从服务端获取服务缓存到本地)。

配置好后,启动Eureka-Client,可以从控制台中看到注册成功的消息:

 

posted @ 2023-02-23 17:02  这很周锐  阅读(62)  评论(0编辑  收藏  举报