1. 程式人生 > >spring cloud demo解釋

spring cloud demo解釋

lds ont pat slf4j fas autowired param random vnr

我也是一個初學者,如果有建議請留言哦!

下一個博客解釋最近我學習Spring cloud 的感觸以及理解。

這個博客主要是通過一個簡單的demo來加深對Spring cloud 的理解。

1.首先要建一個父工程,用來收集子項目中要使用到的共有的jar

對於一個Spring cloud架構的項目來說,肯定是需要將Spring cloud和Spring boot引入進來。

而對於項目來說一般要引入測試用的jar和日誌jar

 1 <parent> 
 2     <groupId>org.springframework.boot</groupId>
 3     <artifactId>spring-boot-starter-parent</artifactId> 
 4
<version>1.5.2.RELEASE</version> 5 <relativePath/> 6 <!-- lookup parent from repository --> 7 </parent> 8 <!-- 使用spring cloud必須引入 --> 9 <dependencyManagement> 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework.cloud</groupId> 13
<artifactId>spring-cloud-dependencies</artifactId> 14 <version>Dalston.SR2</version> 15 <type>pom</type> 16 <scope>import</scope> 17 </dependency> 18 </dependencies> 19 </dependencyManagement> 20
<dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 <!-- logback + slf4j --> 30 <dependency> 31 <groupId>ch.qos.logback</groupId> 32 <artifactId>logback-classic</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.slf4j</groupId> 36 <artifactId>jcl-over-slf4j</artifactId> 37 </dependency> 38 <!-- 測試模塊,包括JUnit、Hamcrest、Mockito --> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 </dependency> 44 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> 45 <groupId>com.alibaba</groupId> 46 <artifactId>fastjson</artifactId> 47 <version>1.2.37</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId> spring-boot-configuration-processor </artifactId> 52 </dependency> 53 </dependencies>

2.接下來需要一個工程用來保存項目的公共配置文件(從git/SVN獲取到的),並通過rest方式供其他服務獲取配置信息。

兩種方式:

(1)git

application.properties

server.port=8888

application-gitsimple.properties

# 指定配置文件所在的git工程路徑 spring.cloud.config.server.git.uri=https://github.com/hryou0922/spring_cloud.git# 表示將搜索該文件夾下的配置文件 spring.cloud.config.server.git.searchPaths=cloud-config-git/simple

serverapplication:

@SpringBootApplication @EnableConfigServer // 激活該應用為配置文件服務器:讀取遠程配置文件,轉換為rest接口服務public class public static void main(String[] args) { args = new String[1]; args[0] = "--spring.profiles.active=gitsimple"; SpringApplication.run(CloudGitConfigServerApplication.class, args); Git的配置文件放置在這個工程中:cloud-config-git 在simple有兩個文件,名稱和內容如下: cloud-config-dev.properties: simple.config.name=git-dev simple.config.age=112 #註冊服務的zone registercenter.eureka.defaultzone=http://localhost:8761/eureka/ cloud-config-test.properties: simple.config.name=git-test simple.config.age=1 #註冊服務的zone registercenter.eureka.defaultzone=http://localhost:8761/eureka/
  • simple.config.name和simple.config.age做為測試數據,用於後續服務和客戶讀取配置。
  • registercenter.eureka.defaultzone:服務註冊到服務註冊的zone

(2)native

工程名稱: cloud-config-center 在resoucres的目錄下config/simple的創建配置文件 cloud-config-dev.properties simple.config.name=native_dev simple.config.age=113 # 註冊服務的zone registercenter.eureka.defaultzone=http://localhost:8761/eureka/ cloud-config-test.properties simple.config.name=native_test simple.config.age=1 # 註冊服務的zone registercenter.eureka.defaultzone=http://localhost:8761/eureka/ application-nativesimple.properties server.port=8888# native:啟動從本地讀取配置文件,必須指定active的值,才可以使用本場配置模式 spring.profiles.active=native # 自定義配置文件路徑 spring.cloud.config.server.native.searchLocations=classpath:/config/simple/ CloudNativeConfigServerApplication @SpringBootApplication@EnableConfigServer // 激活該應用為配置文件服務器:讀取遠程配置文件,轉換為rest接口服務public class CloudNativeConfigServerApplication { public static void main(String[] args) { args = new String[1]; // 使用native不可以使用spring.profiles.active的方式使用native模式 // args[0] = "--spring.profiles.active=nativesimple"; args[0] = "--spring.config.name=application-nativesimple"; SpringApplication.run(CloudNativeConfigServerApplication.class, args); } }

(3)接下的就簡單了,開始創建註冊中心的工程eureka,這個我就不再贅述了,網上一大堆。

(4)server和client

道理簡單就是將server將服務註冊到eureka,而client從eureka中進行調用。

但是需要註意一下兩個地芳:

1.feign客戶端傳值

  • simple:無參請求
  • simpleWithOneParam:帶單個參數請求
  • simpleWithQry:帶多個參數請求
  • @Configuration
  • @ConfigurationProperties(prefix = "simple.config" ,ignoreUnknownFields = false)
  • public class SimpleConfig { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString(){ return "name="+name+" | age=" + age; } }
  • controller中
@Autowired private Service Service; /** * 無參服務 * @return */ @RequestMapping(value = "/simple") public SimpleDto simple() { return Service.simple(); }   service中: @Servicepublic class SimpleService { @Autowired private SimpleConfig simpleConfig; // public SimpleDto simple(){ SimpleDto simpleDto = new SimpleDto(); simpleDto.setAge(simpleConfig.getAge()); simpleDto.setName(simpleConfig.getName()); simpleDto.setRandomNum(new Random().nextInt(1000)); return simpleDto; } 2.熔斷hytrix的處理,簡單,其實就是重寫客戶端類,將裏面的方法進行重寫,有退路

整個流程差不多就是這樣,這個我也是看網上資料學習到的,很有用。

先啟動配置中心,再啟動註冊中心,然後啟動服務端,最後啟動客戶端。

嗯,我對這部分內容又加深了,你呢,如果你覺得對你有幫助的話給我贊吧。

spring cloud demo解釋