簡介 爲初次接觸Spring Cloud的同窗準備的系列教程,從應用場景、編碼實戰等方面介紹其中的各個組件。web
Spring Cloud是一套完整的微服務解決方案,是一系列不一樣功能的微服務框架的集合。spring
Spring Cloud基於Spring Boot,簡化了分佈式系統的開發,集成了服務發現、配置管理、消息總線、負載均衡、斷路器、數據監控等各類服務治理能力。好比sleuth提供了全鏈路追蹤能力,Netflix套件提供了hystrix熔斷器、zuul網關等衆多的治理組件。config組件提供了動態配置能力,bus組件支持使用RabbitMQ、kafka、Activemq等消息隊列,實現分佈式服務之間的事件通訊。tomcat
基礎組件1-Eureka server 這個是微服務的通信錄(註冊中心),既然是微服務,那麼在調用別的微服務的時候確定須要其餘微服務的地址、端口等信息,而這些信息都有Eureka Server來管理。安全
而Eureka Server的啓動比較簡單,做爲一個Spring boot類型的項目來啓動。服務器
對於習慣了Dubbo開發的同窗來講,在使用Spring Cloud時遇到的第一個不習慣的地方就是,註冊中心Eureka不是一個像Zookeeper那樣獨立運行的中間件,而是能夠用Springboot來啓動運行,有點相似於嵌入式的tomcat,這是Spring Cloud體系的一大特色,除了註冊中心還有網關Zuul也是相似的啓動方式。架構
基本的serverapp
引入pom文件 org.springframework.cloud spring-cloud-starter-netflix-eureka-server 增長啓動類註解 @EnableEurekaServer @SpringBootApplication public class PlatformEurekaApplication { public static void main(String[] args) { SpringApplication.run(PlatformEurekaApplication.class, args); } } 配置yml文件 默認yml server: port: 7001 #(eureka 默認端口爲:8761) spring: application: name: platform-eureka #服務名 eureka: instance: #服務失效時間,Eureka多長時間沒收到服務的renew操做,就剔除該服務,默認90秒 leaseExpirationDurationInSeconds: 15 ip-address: {eureka.instance.ip-address} instanceId:
{server.port} preferIpAddress: true #將IP註冊到Eureka Server上 client: #是否註冊自身到eureka服務器,由於當前這個應用就是eureka服務器,不必註冊自身,因此這裏是false registerWithEureka: false fetchRegistry: false #表示是否從eureka服務器獲取註冊信息 serviceUrl: #是設置eureka服務器所在的地址,查詢服務和註冊服務都須要依賴這個地址(注意:地址最後面的 /eureka/ 這個是固定值) defaultZone: http://
{server.port}/eureka/ server: #設爲false,關閉自我保護,開發測試環境須要頻繁啓動註冊實例,須要關閉自我保護功能,以避免請求跑到舊實例中,生成環境須要開啓自我保護功能 enableSelfPreservation: false #eureka server清理無效節點的時間間隔,默認60000毫秒,即60秒 eviction-interval-timer-in-ms: 5000負載均衡
eureka.server.evictionIntervalTimerInMs: 20000 dev.yml eureka: instance: ip-address: 127.0.0.1框架
management: endpoints: enabled-by-default: true web: exposure: include: "*" 增長安全監控 http://xxx/Actuator分佈式
修改pom org.springframework.boot spring-boot-starter-actuator 修改yml
management: endpoints: enabled-by-default: true web: exposure: include: "*" 增長安全認證
引入安全模塊的pom org.springframework.boot spring-boot-starter-security 修改yml文件 security: basic: enabled: true # 開啓基於HTTP basic的認證 user: name: user # 配置登陸的帳號是user password: password123 #配置登陸的密碼是password123 基礎組件2-Eureka client之服務提供者 服務提供者要做爲Eureka的客戶端在註冊中心註冊爲服務提供者,這裏重點是註冊本身的名字和服務地址。
引入pom org.springframework.cloud spring-cloud-starter-netflix-eureka-client 默認yml server: port: {spring.cloud.client.ip-address} hostname:
{eureka.instance.ip-address}:
{eureka.instance.registry.default-open-for-traffic-count} registry.expected-number-of-renews-per-min:
{eureka.client.serviceUrl.defaultZone} dev.yml server: port: 7081 spring: profiles: dev eureka: instance: ip-address: 127.0.0.1 registry.default-open-for-traffic-count: 1 registry.expected-number-of-renews-per-min: 1 client: serviceUrl: defaultZone: http://127.0.0.1:7001/eureka/ management: endpoints: enabled-by-default: true web: exposure: include: "*" 增長註解 @EnableDiscoveryClient @SpringBootApplication @EnableTransactionManagement @MapperScan("com.itcast.mapper") public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } } 注意:
從Spring Cloud Edgware開始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相關依賴,並進行相應配置,便可將微服務註冊到服務發現組件上。
@EnableDiscoveryClient和@EnableEurekaClient共同點就是:都是可以讓註冊中心可以發現,掃描到改服務。
不一樣點:@EnableEurekaClient只適用於Eureka做爲註冊中心,@EnableDiscoveryClient 能夠是其餘註冊中心。
基礎組件3-Eureka client之服務消費者 經過註冊中心查找本身須要的服務地址,就想提供名字查電話同樣。
與服務提供者的編寫方式基本一致。爲了後面增長網關支持的方便,這裏面的服務消費者自己也是服務提供者。
基礎組件4-Feign 在消費者中使用,簡化Http API的調用,使消費者調用服務提供者就想調用本地接口同樣方便。
Spring Cloud對原生Feign進行了整合。
引入pom org.springframework.cloud spring-cloud-starter-openfeign 增長註解 @EnableFeignClients 按照服務提供者對應服務協議編寫對應接口(不用實現) @FeignClient(name = "PLATFORM-USER") public interface UserService { /***
增長fallback參數 @FeignClient(name = "PLATFORM-USER", fallback = UserServiceFallbackImpl.class) 實現備胎代碼 @Component //@Service public class UserServiceFallbackImpl implements UserService { private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceFallbackImpl.class); @Override public String searchUser(String userName){ LOGGER.error("用戶信息查詢接口調用異常:searchUser"); return JsonUtils.toText(ResponseUtils.failure("調用用戶信息查詢接口服務異常!")); } } 居然都看到最後了,給小編點個關注吧,小編還會持續更新的,只收藏不點關注的都是在耍流氓!