您的位置:首页 > 编程语言 > Java开发

服务注册与发现-spring cloud之eureka

2017-12-15 11:22 393 查看
一、创建eureka-server
1、创建Spring Boot项目

创建一个基础的Spring Boot工程,命名为eureka-server。
2、编辑POM

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

<relativePath/>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.SR1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>
3、启动服务注册中心

只需在Spring Boot应用中添加@EnableEurekaServer注解就能开启服务注册中心。

package com.mycompany.eurekaserver;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer

@SpringBootApplication

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}
4、禁用客户端注册

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册自己,需要禁用它的客户端注册行为。禁用客户端注册只需在application.properties配置文件中增加如下信息:

spring.application.name=eureka-server

server.port=1001

eureka.instance.hostname=localhost

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false
5、启动注册中心

Run As -> Java Application
6、浏览器访问

http://localhost:1001/

1001为服务注册中心的端口。

此时无服务。

二、创建服务提供方

下面创建提供服务的客户端,并向服务注册中心注册自己。本例尝试在服务提供方中提供一个接口用来获取当前所有的服务信息。
1、创建Spring Boot项目

创建一个基础的Spring Boot工程,命名为eureka-client。
2、编辑POM

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.SR1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>
3、服务内容的实现

实现请求处理接口,通过DiscoveryClient对象打印出服务实例的相关内容。

package com.mycompany.eurekaclient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.discovery.DiscoveryClient;

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

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

@RestController

public class DcController {

@Autowired

DiscoveryClient discoveryClient;

@GetMapping("/dc")

public String dc() {

String services = "Services: " + discoveryClient.getServices();

System.out.println(services);

return services;

}

}
4、激活Eureka中的DiscoveryClient实现

须给应用主类加上@EnableDiscoveryClient注解,才能激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。

package com.mycompany.eurekaclient;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient

@SpringBootApplication

public class EurekaClientApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaClientApplication.class, args);

}

}
5、配置服务

在application.properties配置文件中增加如下信息:

spring.application.name=eureka-client

server.port=2001

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

属性说明:

spring.application.name:指定微服务的名称,后续在调用的时候只需要使用该名称就可以进行服务的访问。

server.port:指定服务提供方的端口。

eureka.client.serviceUrl.defaultZone:指定服务注册中心的地址。
6、启动服务提供方

Run As -> Java Application
7、浏览器访问

http://localhost:1001/

可见定义的服务被成功注册了。

也可通过http://localhost:2001/dc访问服务提供方的dc接口,获得的应答结果为Services: [eureka-client]。

三、创建服务消费方

LoadBalancerClient,是一个负载均衡客户端的抽象定义。可使用Spring Cloud提供的负载均衡器客户端接口来实现服务的消费。

1、创建Spring Boot项目

创建一个基础的Spring Boot工程,命名为eureka-consumer。
2、编辑POM

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.4.RELEASE</version>

<relativePath/>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.SR1</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>
3、指定注册中心

配置application.properties,内容如下:

spring.application.name=eureka-consumer

server.port=2101

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
4、将当前应用加入服务治理体系

package com.mycompany.eurekaconsumer;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient

@SpringBootApplication

public class EurekaConsumerApplication {

@Bean

public RestTemplate restTemplate() {

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(EurekaConsumerApplication.class, args);

}

}

代码说明:

RestTemplate:初始化RestTemplate,用来真正发起REST请求。

@EnableDiscoveryClient:用来将当前应用加入到服务治理体系中。
5、创建接口消费eureka-client提供的接口

package com.mycompany.eurekaconsumer;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

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

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

import org.springframework.web.client.RestTemplate;

@RestController

public class DcController {

@Autowired

LoadBalancerClient loadBalancerClient;

@Autowired

RestTemplate restTemplate;

@GetMapping("/consumer")

public String dc() {

ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");

String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";

System.out.println(url);

return restTemplate.getForObject(url, String.class);

}

}

以上代码注入了LoadBalancerClient和RestTemplate,并在/consumer接口的实现中,先通过loadBalancerClient的choose函数来负载均衡的选出一个eureka-client的服务实例,这个服务实例的基本信息存储在ServiceInstance中,然后通过这些对象中的信息拼接出访问/dc接口的详细地址,最后再利用RestTemplate对象实现对服务提供者接口的调用。
7、测试eureka consumer

将eureka-server、eureka-client、eureka-consumer都启动起来,然后访问http://localhost:2101/consumer ,以观察eureka-consumer服务是如何消费eureka-client服务的/dc接口的。

参考: http://blog.didispace.com/Spring-Cloud基础教程/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: