Spring Cloud框架搭建

github地址: https://github.com/Streamhu/springCloud_demo

一、前期準備

1. 環境
1) 開發環境:idea + maven + jdk1.8  

2) 主要框架:Spring Boot 
2. spring cloud版本
<!-- spring cloud版本 -->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Camden.SR6</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

二、Eureka

1. pom中添加依賴	

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>	
	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>	

2. 在啓動類中添加@EnableEurekaServer註解	

3. 配置文件

	spring.application.name=spring-cloud-eureka

	server.port=8000
	eureka.client.register-with-eureka=false
	eureka.client.fetch-registry=false

	eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

4. 打開Eureka頁面,http://localhost:8000/

三、服務提供者(producer)

1. pom中添加依賴		

	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>

2. 啓動類中添加@EnableDiscoveryClient註解

3. 配置文件

	spring.application.name=spring-cloud-producer
	server.port=9000
	eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/	

4. 建立Controller,提供hello服務

	@RestController
	public class HelloController {
		
	    @RequestMapping("/hello")
	    public String index(@RequestParam String name) {
	        return "hello "+name+",this is first messge";
	    }
	}

5. 添加@EnableDiscoveryClient註解後,項目就具有了服務註冊的功能。啓動工程後,就可以在註冊中心的頁面看到SPRING-CLOUD-PRODUCER服務

四、服務調用者(consumer)

1. pom中添加依賴	

	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>	

2. 啓動類添加@EnableDiscoveryClient和@EnableFeignClients註解

	1)@EnableDiscoveryClient :啓用服務註冊與發現

	2)@EnableFeignClients:啓用feign進行遠程調用		

3. 配置文件

	spring.application.name=spring-cloud-consumer
	server.port=9001
	eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/	

4. feign調用實現

	@FeignClient(name= "spring-cloud-producer")
	public interface HelloRemote {
	    @RequestMapping(value = "/hello")
	    public String hello(@RequestParam(value = "name") String name);
	}	

5. web層調用遠程服務

	@RestController
	public class ConsumerController {

	    @Autowired
	    HelloRemote HelloRemote;
		
	    @RequestMapping("/hello/{name}")
	    public String index(@PathVariable("name") String name) {
	        return HelloRemote.hello(name);
	    }

	}	


五、熔斷器Hystrix(熔斷只是作用在服務調用這一端,只需要改動spring-cloud-consumer項目相關代碼就可以)

1. Feign中已經依賴了Hystrix所以在maven配置上不用做任何改動

2. 配置文件

	feign.hystrix.enabled=true

3. 創建回調類(創建HelloRemoteHystrix類繼承HelloRemote實現回調的方法)

	@Component
	public class HelloRemoteHystrix implements HelloRemote{

	    @Override
	    public String hello(@RequestParam(value = "name") String name) {
	        return "hello" +name+", this messge send failed ";
	    }
	}	

4. 添加fallback屬性(在HelloRemote類添加指定fallback類,在服務熔斷的時候返回fallback類中的內容)

	@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)
	public interface HelloRemote {

	    @RequestMapping(value = "/hello")
	    public String hello(@RequestParam(value = "name") String name);

	}


六、Config-server(先建好git倉庫)

1. pom中添加依賴(他也是服務,所以註冊到Eureka) 		

	<!-- Config Server -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>	

2. 啓動類添加@EnableConfigServer,以及@EnableDiscoveryClient	

3. 配置文件

	server:
	  port: 8001
	spring:
	  application:
	    name: spring-cloud-config-server
	  cloud:
	    config:
	      server:
	        git:
	          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git倉庫的地址
	          search-paths: config-repo                             	  # git倉庫地址下的相對地址,可以配置多個,用,分割。
	          username:                                                   # git倉庫的賬號
	          password:                                                   # git倉庫的密碼	


4. 測試

	(1)測試server端是否可以讀取到github上面的配置信息,直接訪問:http://localhost:8001/neo-config/dev	
	
	(2)倉庫中的配置文件會被轉換成web接口,有幾種規則,上面的規則是:/{application}/{profile}[/{label}]      

	(3)以neo-config-dev.properties爲例子,它的application是neo-config,profile是dev。client會根據填寫的參數來選擇讀取對應的配置  


七、Config-client

1. pom中添加依賴(他也是服務,所以註冊到Eureka)

	<!-- Config-client -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>	
	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>	
	<!-- actuator, 配合@RefreshScope,是一套監控的功能 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>

2. 啓動類添加@EnableConfigServer,以及@EnableDiscoveryClient	

3. 配置文件(需要兩個)

	1)application.properties如下:

		spring.application.name=spring-cloud-config-client
		server.port=8002

	2)bootstrap.properties如下:
	
		spring.cloud.config.name=neo-config
		spring.cloud.config.profile=dev
		spring.cloud.config.uri=http://localhost:8001/
		spring.cloud.config.label=master	

4. web測試(@RefreshScope,使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中)

	@RestController
	@RefreshScope
	class HelloController {
	    @Value("${neo.hello}")
	    private String hello;

	    @RequestMapping("/hello")
	    public String from() {
	        return this.hello;
	    }
	}		


八、服務網關Zuul

1. pom中添加依賴(他也是服務,所以註冊到Eureka)

	<!-- Zuul -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-zuul</artifactId>
	</dependency>
	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>	

2. 啓動類添加@EnableZuulProxy,以及@EnableDiscoveryClient	 

3. 配置文件

	spring.application.name=gateway-service-zuul
	server.port=8888

	eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

	#這裏的配置表示,訪問/producer/** 直接重定向到spring-cloud-producer服務
	zuul.routes.api-a.path=/producer/**
	zuul.routes.api-a.serviceId=spring-cloud-producer

4. 測試,請求 http://localhost:8888/producer/hello?name=hh,會被轉發到serviceId對應的微服務

九、分佈式鏈路跟蹤Zipkin

1. pom中添加依賴(他也是服務,所以註冊到Eureka)

	<!-- Eureka -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<!-- Zipkin -->
	<dependency>
		<groupId>io.zipkin.java</groupId>
		<artifactId>zipkin-server</artifactId>
	</dependency>
	<dependency>
		<groupId>io.zipkin.java</groupId>
		<artifactId>zipkin-autoconfigure-ui</artifactId>
	</dependency>	

2. 啓動類添加@EnableZipkinServer,以及@EnableDiscoveryClient	 

3. 配置文件

	eureka:
	  client:
	    serviceUrl:
	      defaultZone: http://localhost:8761/eureka/
	server:
	  port: 9000
	spring:
	  application:
	    name: zipkin-server 

4. 訪問http://localhost:9000/zipkin/可以看到Zipkin後臺頁面		    
		
5. 項目添加zipkin支持(比如網關zuul,服務提供者producer,調用者conusmer)	

	1)添加zipkin依賴

		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>	

	2)配置文件中增加配置
	
		spring:
		  zipkin:
		    base-url: http://localhost:9000
		  sleuth:
		    sampler:
		      percentage: 1.0	

6. 進行驗證

	1)通過外部請求訪問Zuul網關,Zuul網關去調用spring-cloud-producer對外提供的服務		  

	2)打開zipkin頁面,可以看到相關信息(調用順序,各個微服務調用所消耗的時間等)  

參考網址

Spring Cloud 系列文章

注:文章是經過參考其他的文章然後自己整理出來的,有可能是小部分參考,也有可能是大部分參考,但絕對不是直接轉載,覺得侵權了我會刪,我只是把這個用於自己的筆記,順便整理下知識的同時,能幫到一部分人。
ps : 有錯誤的還望各位大佬指正,小弟不勝感激

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