SpringBoot整合SpringCloud

SpringCloud 可以說是一門非常熱門的技術,依賴於SpringBoot進行實現。cloud就像一個大管家,而SpringBoot 纔是真正幹活的人。且SpringBoot可以獨自運行,不依賴於SpringCloud。本篇主要介紹SpringCloud中五大神獸裏的兩大神獸,eureka和rabbin,其中eureka 是重點,而rabbin只是簡單使用了它的一個註解。

0,工程結構圖

1,eureka 配置

①,pom.xml


 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka-server</artifactId>

  15. </dependency>

  16. </dependencies>

  17.  
  18. <dependencyManagement>

  19. <dependencies>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-dependencies</artifactId>

  23. <version>${spring-cloud.version}</version>

  24. <type>pom</type>

  25. <scope>import</scope>

  26. </dependency>

  27. </dependencies>

  28. </dependencyManagement>

②,application.properties


 
  1. #eueka 主機名

  2. eureka.instance.hostname=eureka-service

  3. #不註冊自己

  4. eureka.client.register-with-eureka=false

  5. #獲取服務

  6. eureka.client.fetch-registry=false

  7. #提供者和消費者的註冊地址

  8. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

  9.  
  10. server.port=8761

③,啓用註冊中心


 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

  4.  
  5. //啓用註冊中心

  6. @EnableEurekaServer

  7. @SpringBootApplication

  8. public class EurekaApplication {

  9.  
  10. public static void main(String[] args) {

  11. SpringApplication.run(EurekaApplication.class, args);

  12. }

  13. }

2,provider配置

①,pom.xml


 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka</artifactId>

  15. </dependency>

  16. <dependency>

  17. <groupId>org.springframework.boot</groupId>

  18. <artifactId>spring-boot-starter-web</artifactId>

  19. </dependency>

  20. </dependencies>

  21.  
  22. <dependencyManagement>

  23. <dependencies>

  24. <dependency>

  25. <groupId>org.springframework.cloud</groupId>

  26. <artifactId>spring-cloud-dependencies</artifactId>

  27. <version>${spring-cloud.version}</version>

  28. <type>pom</type>

  29. <scope>import</scope>

  30. </dependency>

  31. </dependencies>

  32. </dependencyManagement>

②,application.properties


 
  1. server.port=8002

  2. #服務名

  3. spring.application.name=ticket-provider

  4. #使用ip進行註冊

  5. eureka.instance.prefer-ip-address=true

  6. #註冊地址

  7. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,定義服務


 
  1. import org.springframework.stereotype.Service;

  2.  
  3. @Service

  4. public class TicketService {

  5.  
  6. public String buyTicket(){

  7. System.out.println("我是8002");

  8. return "《瘋狂的石頭》";

  9. }

  10. }

④,提供服務


 
  1. import com.example.provider.service.TicketService;

  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5.  
  6. @RestController

  7. public class TicketController {

  8.  
  9. @Autowired

  10. private TicketService ticketService;

  11.  
  12. @RequestMapping("/")

  13. public String index(){

  14. return ticketService.buyTicket();

  15. }

  16. }

3,customer配置

①,pom.xml


 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>1.5.12.RELEASE</version>

  5. </parent>

  6.  
  7. <properties>

  8. <spring-cloud.version>Edgware.SR3</spring-cloud.version>

  9. </properties>

  10.  
  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.cloud</groupId>

  14. <artifactId>spring-cloud-starter-eureka</artifactId>

  15. </dependency>

  16. </dependencies>

  17.  
  18. <dependencyManagement>

  19. <dependencies>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-dependencies</artifactId>

  23. <version>${spring-cloud.version}</version>

  24. <type>pom</type>

  25. <scope>import</scope>

  26. </dependency>

  27. </dependencies>

  28. </dependencyManagement>

②,application.properties


 
  1. server.port=8200

  2.  
  3. spring.application.name=ticket-customer

  4. eureka.instance.prefer-ip-address=true

  5. #註冊地址

  6. eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

③,消費者配置


 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

  4. import org.springframework.cloud.client.loadbalancer.LoadBalanced;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.web.client.RestTemplate;

  7.  
  8. //開啓發現服務

  9. @EnableDiscoveryClient

  10. @SpringBootApplication

  11. public class CustomerApplication {

  12.  
  13. public static void main(String[] args) {

  14. SpringApplication.run(CustomerApplication.class, args);

  15. }

  16.  
  17. // 啓用負載均衡,默認算法是輪詢

  18. @LoadBalanced

  19. @Bean

  20. public RestTemplate restTemplate(){

  21. return new RestTemplate();

  22. }

  23. }

④,消費者消費方法


 
  1. import org.springframework.beans.factory.annotation.Autowired;

  2. import org.springframework.web.bind.annotation.RequestMapping;

  3. import org.springframework.web.bind.annotation.RestController;

  4. import org.springframework.web.client.RestTemplate;

  5.  
  6. @RestController

  7. public class CustomerController {

  8.  
  9. @Autowired

  10. private RestTemplate restTemplate;

  11.  
  12. @RequestMapping("/")

  13. public String index(){

  14. String result = restTemplate.getForObject("http://ticket-provider/", String.class);

  15.  
  16. return result;

  17. }

  18. }

4,測試

①,啓動eureka工程

②,啓動提供者工程

③,啓動消費者工程

④,最後會看到消費者和提供者都註冊到了eureka中,並可以通過,消費接口訪問服務者提供的服務,

並且使用了輪詢的負載均衡策略

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章