划分模塊
這里我划分了四個模塊
Common: 存放bean和Dao模塊
Consumer: 消費者模塊,提供對外暴露接口服務
EurekaServer: Eureka注冊中心模塊,主要用於啟動注冊中心
Provider: 提供者模塊,提供業務實現給消費者調用
依賴jar包
整合boot+cloud項目Maven依賴jar包,由於所有的bean都在Common模塊內,所以我把jar包依賴全部放在此模塊內,其他模塊只需要引用即可。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <artifactId>Common</artifactId> <groupId>com.boot</groupId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <modelVersion>4.0.0</modelVersion> <name>Common</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> </parent> <properties> <mybatis-spring-boot>1.3.2</mybatis-spring-boot> </properties> <!-- SpringCloud-dependencies版本管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- Springboot-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Springboot-aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- Springboot-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Springboot-jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Springboot-activemq監控包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <!-- SpringCloud-eureka注冊中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- SpringCloud-eureka注冊中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- SpringCloud-feign服務調用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- SpringCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> <version>1.3.5.RELEASE</version> </dependency> </dependencies> </project>
博主在整合生產版本時,遇到很多問題,故整理如下jar包依賴pom文件,供大家參考。
<dependencies> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- Springboot-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Springboot-aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- Springboot-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Springboot-jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- SpringCloud-eureka注冊中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.4.RELEASE</version> </dependency> <!-- SpringCloud-netflix 核心包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-core</artifactId> <version>1.4.4.RELEASE</version> </dependency> <!-- SpringCloud-feign服務調用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- SpringCloud-feign服務調用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.3.4.RELEASE</version> </dependency> <!-- SpringCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> <version>1.3.5.RELEASE</version> </dependency> </dependencies>
創建注冊中心啟動類
引用Common依賴jar包
<dependencies> <dependency> <groupId>com.boot</groupId> <artifactId>Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
編寫啟動類
package com.boot.eurekaServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args){ System.out.println(">>>>>>>>>>>>>>> 啟動EurekaServer <<<<<<<<<<<<<<<"); SpringApplication.run(EurekaServerApplication.class, args); System.out.println(">>>>>>>>>>>>>>> 運行EurekaServer成功 <<<<<<<<<<<<<<<"); } }
@EnableEurekaServer : 表示用來激活Eureka Server相關的配置,啟動注冊中心
application.properties配置文件:
#服務端口號
server.port=8001
#服務名稱
spring.application.name=eureka-server
#服務地址
eureka.instance.hostname=localhost
#禁用eureka注冊自己
#Eureka是為注冊中心,是否需要將自己注冊到注冊中心上(默認為true),
#本次位單機部署,不需要設置為true;但是注冊中心集群時候必須為true;因為集群時,其他Eureka需要從此Eureka中的節點上獲取數據;
eureka.client.register-with-eureka=false
#Erueka是為注冊中心,不需要檢索服務信息;
#(表示是否從Eureka Server獲取注冊信息,默認為true。 如果這是一個單點的 Eureka Server,不需要同步其他節點的數據,可以設為false)
eureka.client.fetch-registry=false
#關閉保護機制
eureka.server.enable-self-preservation=false
#標注其它服務注冊的目標地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
重點:
該警告是觸發了Eureka Server的自我保護機制。Eureka Server在運行期間,會統計心跳失敗的比例在15分鍾之內是否低於85%,如果低於,就會將當前實例注冊信息保護起來,讓實例不會過期,盡可能保護這些注冊信息。但是如果在保護期間,實例出現問題,那么客戶端很容易拿到實際已經不存在的服務實例,會出現調用失敗。這個時候客戶端的容錯機制就很重要了。(重新請求,斷路器)保護機制,可能會導致服務實例不能夠被正確剔除,可以關掉保護機制。
創建提供者啟動類
引用Common依賴jar包
<dependencies> <dependency> <groupId>com.boot</groupId> <artifactId>Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
編寫啟動類
package com.boot.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * * @EnableEurekaClient : 負責與Eureka Server 配合向外提供注冊與發現服務接口 */ @EnableEurekaClient @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @SpringBootApplication public class ProviderServerApplication { public static void main(String[] args){ System.out.println(">>>>>>>>>>>>>>> 啟動ProviderServer <<<<<<<<<<<<<<<"); SpringApplication.run(ProviderServerApplication.class, args); System.out.println(">>>>>>>>>>>>>>> 運行ProviderServer成功 <<<<<<<<<<<<<<<"); } }
編寫業務實現類
package com.boot.provider.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** *Feign遠程服務調用與正常暴露接口是一樣的,所以我們只需要仿照接口暴露編寫即可 */ @RestController @RequestMapping("/Provider") public class ProviderServerController { @RequestMapping("/gotoAlgorithmServer") public String gotoAlgorithmServer(){ return "調用Provider成功"; } }
application.properties配置文件:
#指定提供者名稱(消費者通過此名稱遠程訪問服務)
spring.application.name=eureka-provider
#端口號
server.port=8003
#服務注冊地址(注冊中心的服務注冊地址)
eureka.client.serviceUrl.defaultZone = http://localhost:8001/eureka/
創建消費者啟動類
引用Common依賴jar包
<dependencies> <dependency> <groupId>com.boot</groupId> <artifactId>Common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
編寫啟動類
package com.boot.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; /** * @EnableFeignClients 啟動feign客戶端 * @EnableEurekaClient 負責與 Eureka Server 配合向外提供注冊與發現服務接口 */ @EnableFeignClients @EnableEurekaClient @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @SpringBootApplication public class ConsumerServerApplication { public static void main(String[] args){ System.out.println(">>>>>>>>>>>>>>> 啟動ConsumerServer <<<<<<<<<<<<<<<"); SpringApplication.run(ConsumerServerApplication.class, args); System.out.println(">>>>>>>>>>>>>>> 運行ConsumerServer成功 <<<<<<<<<<<<<<<"); } }
編寫暴露接口服務類
package com.boot.consumer.controller; import com.boot.consumer.service.ConsumerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/Consumer") public class ConsumerServerController { @Autowired public ConsumerService consumerService; @RequestMapping("/gotoAlgorithmServer") public String gotoAlgorithmServer(){ return consumerService.gotoAlgorithmServer(); } }
編寫Feign調用接口
package com.boot.consumer.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** *@FeignClient : feign客戶端 * name屬性 :提供者的服務名稱 */ @FeignClient(name = "eureka-provider") public interface ConsumerService { @RequestMapping(method = RequestMethod.GET ,path = "/Provider/gotoAlgorithmServer") String gotoAlgorithmServer(); }
application.properties配置文件:
#消費者名稱
spring.application.name=eureka-consumer
#端口
server.port=8002
#注冊中心地址
eureka.client.serviceUrl.defaultZone = http://localhost:8001/eureka/
#設置連接超時
feign.client.config.default.connect-timeout = 10000
#設置讀取超時
feign.client.config.default.read-timeout = 600000
測試
成功!