🌥️SpringCloud实战-服务注册与发现之Eureka Client
|字数总计:1.1k|阅读时长:5分钟|阅读量:|
使用Eureka Client
实现服务注册与发现。
服务注册与发现
将一个微服务(服务提供者)注册至Eureka Server
注册中心上。其它微服务(服务消费者)在启动时就可从注册中心进行订阅。
服务提供者
在IDEA
中创建一个名叫eureka-client-provider
的maven
工程,然后在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
| <properties> <spring.boot.version>2.2.0.RELEASE</spring.boot.version> <spring.cloud.version>Hoxton.SR1</spring.cloud.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <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>
<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.yml
配置文件,并添加以下配置项:
1 2 3 4 5 6 7 8 9
| server: port: 9001 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.netflix.eureka.client.provider;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder;
@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.netflix.eureka.client.provider.controller;
import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @Slf4j public class ProviderController { @GetMapping("/provider") public String provide(String id) { log.info("provide invoked: id={}", id); return "provide:" + id; } }
|
运行启动类EurekaClientProviderApplication
,即可将该服务提供者注册至注册中心。
服务消费者
在IDEA
中新建一个名叫eureka-client-consumer
的maven
工程,然后在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.yml
配置文件,并添加以下配置项:
1 2 3 4 5 6 7 8 9
| server: port: 9002 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.netflix.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;
@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.netflix.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;
@RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @GetMapping("/consumer/{id}") public String consume(@PathVariable String id) { return restTemplate.getForObject("http://localhost:9001/provider?id=" + id, String.class); } }
|
运行启动类EurekaClientConsumerApplication
,即可将服务消费者注册至注册中心。
发送GET
请求:GET
http://127.0.0.1:9002/consumer/1
,可看到返回结果为:provide:1
。至此我们已经完成了一次正常的服务调用。eureka-client-service-consumer
服务消费者成功消费了一次eureka-client-service-provider
服务提供者提供的服务。