使用Eureka Client实现服务注册与发现。

服务注册与发现

将一个微服务(服务提供者)注册至Eureka Server注册中心上。其它微服务(服务消费者)在启动时就可从注册中心进行订阅。

服务提供者

IDEA中创建一个名叫eureka-client-providermaven工程,然后在pom.xml中引入相关依赖:

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
<!-- version -->
<properties>
<spring.boot.version>2.2.0.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>
</properties>

<!-- 预定义依赖 -->
<dependencyManagement>
<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<!-- eureka-client依赖引入 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

src/main/resources目录下创建application.properties配置文件,并添加以下配置项:

1
2
3
server.port=9000
spring.application.name=eureka-client-service-provider
eureka.client.service-url.defaultZone=http://localhost:520/eureka/,http://localhost:521/eureka/

编写启动类EurekaClientProviderApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sunchaser.sparrow.microservice.springcloud.eureka.client.provider;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

/**
* @author sunchaser admin@lilu.org.cn
* @since JDK8 2021/1/7
*/
@SpringBootApplication
public class EurekaClientProviderApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientProviderApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}

注意:这里虽然没有在启动类上写@EnableEurekaClient注解,但服务还是会自动注册至注册中心。这是因为Eureka的配置项eureka.client.register-with-eureka默认为true

我们提供一个很简单的参数输出的服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.sunchaser.sparrow.microservice.springcloud.eureka.client.provider.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author sunchaser admin@lilu.org.cn
* @since JDK8 2021/2/6
*/
@RestController
@Slf4j
public class ProviderController {
@GetMapping("/provider")
public String provide(String id) {
log.info("provide invoked: id={}", id);
return "provide:" + id;
}
}

运行启动类EurekaClientProviderApplication,即可将该服务提供者注册至注册中心。

eureka-client-service-provider

服务消费者

IDEA中新建一个名叫eureka-client-consumermaven工程,然后在pom.xml中引入相关依赖:

1
2
3
4
5
6
7
8
9
10
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

src/main/resources目录下创建application.properties配置文件,并添加以下配置项:

1
2
3
server.port=9001
spring.application.name=eureka-client-service-consumer
eureka.client.service-url.defaultZone=http://localhost:520/eureka/,http://localhost:521/eureka/

编写启动类EurekaClientConsumerApplication

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
package com.sunchaser.sparrow.microservice.springcloud.eureka.client.consumer;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
* @author sunchaser admin@lilu.org.cn
* @since JDK8 2021/2/5
*/
@SpringBootApplication
public class EurekaClientConsumerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientConsumerApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

使用@Bean注解将restful接口的访问模板工具类RestTemplate交给Spring进行管理。

接下来我们使用RestTemplate来消费eureka-client-provider微服务中提供的/provider服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.sunchaser.sparrow.microservice.springcloud.eureka.client.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
* @author sunchaser admin@lilu.org.cn
* @since JDK8 2021/2/6
*/
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("/consumer/{id}")
public String consume(@PathVariable String id) {
return restTemplate.getForObject("http://localhost:9000/provider?id=" + id, String.class);
}
}

运行启动类EurekaClientConsumerApplication,即可将服务消费者注册至注册中心。

eureka-client-service-consumer

发送GET请求:GET http://127.0.0.1:9001/consumer/1,可看到返回结果为:provide:1。至此我们已经完成了一次正常的服务调用。eureka-client-service-consumer服务消费者成功消费了一次eureka-client-service-provider服务提供者提供的服务。