失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Spring Cloud(二) 配置Eureka Client

Spring Cloud(二) 配置Eureka Client

时间:2020-09-22 20:58:21

相关推荐

Spring Cloud(二) 配置Eureka Client

前文回顾:

Spring Cloud(一)Eureka Server-单体及集群搭建

本节我们将创建两个Eureka Client,注册到上节中的Eureka Server中,一个作为服务提供方,一个作为服务调用方。

一.服务提供方(生产者)

1.pom中添加依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/></parent>​<properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.RELEASE</spring-cloud.version></properties>​<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>​<dependencyManagement><dependencies><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>

2.Application启动类中添加注解

@EuableDiscoveryClient:就是一个自动发现客户端的实现

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

3.配置文件

spring.application.name=spring-cloud-producerserver.port=9000eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

4.提供服务

@RestControllerpublic class HelloController {​@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello " + name + ",welcome to Spring Cloud";}}

二.服务调用方(消费者)

1.pom中添加依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.yfy</groupId><artifactId>consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>consumer</name><description>Demo project for Spring Boot</description>​<properties><java.version>1.8</java.version></properties>​<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

2.Application启动类中添加注解

@EnableDiscoveryClient:启用服务注册与发现

@EnableFeignClients:启用feign进行远程调用

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

3.配置文件

spring.application.name=spring-cloud-consumerserver.port=9001eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/feign.hystrix.enabled=true

4.Feign调用实现

@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)public interface HelloRemote {​@RequestMapping(value = "/hello")String hello(@RequestParam(value = "name") String name);}

5.web层调用远程服务

@RestControllerpublic class HelloController {@AutowiredHelloRemote helloremote;@RequestMapping("/hello/{name}")public String index(@PathVariable("name") String name) {return helloremote.hello(name);}}

三.测试

依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

浏览器中输入:http://localhost:9001/hello/yfy

返回:hello yfy,welcome to Spring Cloud

4.负载均衡测试

将生产者的controller方法修改为

@RestControllerpublic class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello " + name + ",welcome to Spring Cloud:product2";}}

再启动一个生产者,端口为9003

浏览器中输入:http://localhost:9001/hello/yfy

第一次返回:hello yfy,welcome to Spring Cloud

第一次返回:`hello yfy,welcome to Spring Cloud:product2

不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。

如果觉得《Spring Cloud(二) 配置Eureka Client》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。