目錄
1.2.SpringCloud 與 SpringBoot 的關係
4)訪問Eureka主頁http://127.0.0.1:6868/
1.SpringCloud 簡介
1.1.什麼是 SpringCloud
1.2.SpringCloud 與 SpringBoot 的關係
1.3.SpringCloud 主要框架
1.4.SpringCloud 與 Dubbo 對比
Dubbo | SpringCloud | |
服務註冊中心 | Zookeeper | Spring Cloud Netflix Eureka |
服務調用方式 | RPC | REST API |
服務網關 | 無 | Spring Cloud Netflix Zuul |
熔斷器 | 不完善 | Spring Cloud Netflix Hystrix |
分佈式配置 | 無 | Spring Cloud Config |
服務跟蹤 | 無 | Spring Cloud Sleuth |
消息總線 | 無 | Spring Cloud Bus |
數據流 | 無 | Spring Cloud Stream |
批量任務 | 無 | Spring Cloud Task |
…… | …… | …… |
1.5.說說 SpringCloud 的版本
Spring Boot | Spring Cloud |
1.2.x | Angel 版本 |
1.3.x | Brixton 版本 |
1.4.x | Camden 版本 |
1.5.x | Dalston 版本、Edgware 版本 |
2.0.x | Finchley 版本 |
2.服務發現組件 Eureka
2.1.Eureka簡介
2.2.Eureka服務端開發
1)搭建Eureka服務端微服務項目
2)在pom.xml導入eureka服務端依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3)在application.yml配置eureka服務端
server:
port: 6868
eureka:
client:
registerWithEureka: false #是否將自己註冊到 Eureka 服務中,本身就是,無需註冊
fetchRegistry: false #是否從Euraka中獲取註冊信息
serviceUrl: #Eureka 客戶端與 Eureka 服務端進行交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka/
4)編寫啓動類(Eureka服務端註解)
package com.tensquare.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}
2.3.Eureka客戶端(服務註冊)
1)在pom.xml導入eureka客戶端依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2)在application.yml配置eureka客戶端
server:
port: 9010
spring:
application:
name: tensquare-friend
eureka:
client:
service-url:
defaultZone: http://localhost:6868/eureka
instance:
prefer-ip-address: true
3)在啓動類添加Eureka客戶端註解
package com.tensquare.friend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class FriendApplication {
public static void main(String[] args) {
SpringApplication.run(FriendApplication.class,args);
}
}
4)訪問Eureka主頁http://127.0.0.1:6868/
如圖所示,我們單個微服務註冊成功
2.4.保護模式
3. Feign 實現服務間調用
3.1.Feign簡介
3.2.案例
1)在pom.xml導入feign座標
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2)在啓動類添加Feign註解
package com.tensquare.friend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import util.JwtUtil;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FriendApplication {
public static void main(String[] args) {
SpringApplication.run(FriendApplication.class,args);
}
}
3)編寫遠程調用接口(*)
package com.tensquare.friend.client;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 調用其他微服務注意事項
* 1.調用方與被調用方都需要註冊eureka中
* 2.調用接口需要寫上被調用方的服務名,即application.yml中的spring: application: name: tensquare-user
* 3.服務名不能帶下劃線_,eureka會識別不到
* 4.調用方法跟被調用方contorller方法名一致
* 5.@RequestMapping--value記得帶上全url,例如最上方的/user
* 6.@PathVariable後一定要帶上("參數"),參數需要跟上方路徑參數一致,否則會識別不到參數
*/
@FeignClient("tensquare-user")
public interface UserClient {
/**
* 更新用戶關注數
*/
@RequestMapping(value = "/user/updateFollowcount/{userid}/{x}", method = RequestMethod.PUT)
public Result updateFollowcount(@PathVariable("userid") String userid, @PathVariable("x") int x);
/**
* 更新用戶粉絲
*/
@RequestMapping(value = "/user/updateFanscount/{userid}/{x}", method = RequestMethod.PUT)
public Result updateFanscount(@PathVariable("userid") String userid, @PathVariable("x") int x);
}
4)使用遠程調用接口完成服務調用
3.3.負載均衡(默認輪詢)
測試:同時開啓多個基礎微服務,看是否是輪流調用。