Spring Cloud(二) 配置Eureka Client
前文回顧:
Spring Cloud(一)Eureka Server-單體及集群搭建
本節(jié)我們將創(chuàng)建兩個(gè)Eureka Client,注冊(cè)到上節(jié)中的Eureka Server中,一個(gè)作為服務(wù)提供方,一個(gè)作為服務(wù)調(diào)用方。
一.服務(wù)提供方(生產(chǎn)者)
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啟動(dòng)類中添加注解
-
@EuableDiscoveryClient:就是一個(gè)自動(dòng)發(fā)現(xiàn)客戶端的實(shí)現(xiàn)
3.配置文件
spring.application.name=spring-cloud-producer server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/4.提供服務(wù)
@RestController public class HelloController { ?@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello " + name + ",welcome to Spring Cloud";} }二.服務(wù)調(diào)用方(消費(fèi)者)
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啟動(dòng)類中添加注解
-
@EnableDiscoveryClient :啟用服務(wù)注冊(cè)與發(fā)現(xiàn)
-
@EnableFeignClients:啟用feign進(jìn)行遠(yuǎn)程調(diào)用
3.配置文件
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ feign.hystrix.enabled=true4.Feign調(diào)用實(shí)現(xiàn)
@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class) public interface HelloRemote { ?@RequestMapping(value = "/hello")String hello(@RequestParam(value = "name") String name); }5.web層調(diào)用遠(yuǎn)程服務(wù)
@RestController public class HelloController {@AutowiredHelloRemote helloremote;@RequestMapping("/hello/{name}")public String index(@PathVariable("name") String name) {return helloremote.hello(name);} }三.測(cè)試
依次啟動(dòng)spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三個(gè)項(xiàng)目
瀏覽器中輸入:http://localhost:9001/hello/yfy
返回:hello yfy,welcome to Spring Cloud
4.負(fù)載均衡測(cè)試
將生產(chǎn)者的controller方法修改為
@RestController public class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello " + name + ",welcome to Spring Cloud:product2";} }再啟動(dòng)一個(gè)生產(chǎn)者,端口為9003
?
瀏覽器中輸入:http://localhost:9001/hello/yfy
第一次返回:hello yfy,welcome to Spring Cloud
第一次返回:`hello yfy,welcome to Spring Cloud:product2
不斷的進(jìn)行測(cè)試下去會(huì)發(fā)現(xiàn)兩種結(jié)果交替出現(xiàn),說明兩個(gè)服務(wù)中心自動(dòng)提供了服務(wù)均衡負(fù)載的功能。
總結(jié)
以上是生活随笔為你收集整理的Spring Cloud(二) 配置Eureka Client的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud(一)Eurek
- 下一篇: Spring Cloud(三) 熔断器H