SpringCloud 能夠說是一門很是熱門的技術,依賴於SpringBoot進行實現。cloud就像一個大管家,而SpringBoot 纔是真正幹活的人。且SpringBoot能夠獨自運行,不依賴於SpringCloud。本篇主要介紹SpringCloud中五大神獸裏的兩大神獸,eureka和rabbin,其中eureka 是重點,而rabbin只是簡單使用了它的一個註解。java
①,pom.xmlweb
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent> <properties> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </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>
②,application.properties算法
#eueka 主機名 eureka.instance.hostname=eureka-service #不註冊本身 eureka.client.register-with-eureka=false #獲取服務 eureka.client.fetch-registry=false #提供者和消費者的註冊地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ server.port=8761
③,啓用註冊中心spring
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //啓用註冊中心 @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
①,pom.xmlapp
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent> <properties> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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>
②,application.properties負載均衡
server.port=8002 #服務名 spring.application.name=ticket-provider #使用ip進行註冊 eureka.instance.prefer-ip-address=true #註冊地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
③,定義服務ide
import org.springframework.stereotype.Service; @Service public class TicketService { public String buyTicket(){ System.out.println("我是8002"); return "《瘋狂的石頭》"; } }
④,提供服務spring-boot
import com.example.provider.service.TicketService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TicketController { @Autowired private TicketService ticketService; @RequestMapping("/") public String index(){ return ticketService.buyTicket(); } }
①,pom.xml測試
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> </parent> <properties> <spring-cloud.version>Edgware.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </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>
②,application.propertiesfetch
server.port=8200 spring.application.name=ticket-customer eureka.instance.prefer-ip-address=true #註冊地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
③,消費者配置
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; //開啓發現服務 @EnableDiscoveryClient @SpringBootApplication public class CustomerApplication { public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); } // 啓用負載均衡,默認算法是輪詢 @LoadBalanced @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
④,消費者消費方法
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class CustomerController { @Autowired private RestTemplate restTemplate; @RequestMapping("/") public String index(){ String result = restTemplate.getForObject("http://ticket-provider/", String.class); return result; } }
①,啓動eureka工程
②,啓動提供者工程
③,啓動消費者工程
④,最後會看到消費者和提供者都註冊到了eureka中,並能夠經過,消費接口訪問服務者提供的服務,
而且使用了輪詢的負載均衡策略