當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Cloud(二) 配置Eureka Client
生活随笔
收集整理的這篇文章主要介紹了
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:就是一個自動發現客戶端的實現
3.配置文件
spring.application.name=spring-cloud-producer server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/4.提供服務
@RestController public 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進行遠程調用
3.配置文件
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ feign.hystrix.enabled=true4.Feign調用實現
@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class) public interface HelloRemote { ?@RequestMapping(value = "/hello")String hello(@RequestParam(value = "name") String name); }5.web層調用遠程服務
@RestController public 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方法修改為
@RestController public 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud(一)Eurek
- 下一篇: Spring Cloud(三) 熔断器H