SpringCloud元件之(Eureka)的註冊服務與發現服務的實踐
吐槽:不得不說,學完了Springboot,,發現沒學到啥玩應,,將白了就是正常的單體應用多了一個容器,預設給你整合好了tomcat的容器,利用maven的打包方式,管你是幾個model的工程,還是單獨的model的工程,(ps:吐槽下IDEA和Eclipse的設計思想,能不能吸收點對方好處,IDEA要是吸收了Eclipse的多模組建立工程,獨立工作環境的模式,會不會更好一點,eclipse要是吸收了IDEA的程式碼提示功能,和多個專案開發的功能是不是更完美,僅僅個人吐槽,歡迎噴我),打成jar包,在伺服器裡面,一句“java -jar xxxx" ,就能執行起來這個專案了,嘖嘖,,是不是剛剛學習的同學都覺得美滋滋,不用xml配置,不用繁瑣的打包方式,不用。。。。。反正就是簡單是不是?????但是!!!但是!!難道忘記了當初我們用dubbo時候,用zookeper時候服務進行註冊,分發,負載均衡的時候了麼??那時候是單體應用麼???不是吧,那就來了,,我們用了Springboot難道還要弄個dubbo或者zookeper這種東西去整合???反正作為一個懶人,懶到極致的人,我是覺得,不想,非常非常不想,那麼就扔掉那一套不是親生兒子的框架吧,扎身於Springcloud的知識中吧,,,
首先,我想先來個總結,我這個人,可能比較好高騖遠,,(ps:就是就是),所以導致學習知識時候,我希望掌握所有的東西,都大概瞭解了一下的,然後在逐個擊破,例如這個Springcloud是個啥,,剛剛開始我還以為是個框架,後來發現,,這特麼不是框架,是外掛,一堆的外掛,而且是基於Springboot的,學吧,然後發現了eureka這個東西,簡單一句話:eureka就是負責,服務的註冊和使用,舉個例子就是:個人<client>--中介<servercenter>--賣房子的個人<server>), 所以,,根據我們以前的經驗,,我們要有三個東西,,,其實也是兩個東西,,一個是服務中心
第一步:建立三個普通的專案
使用maven建立simple的三個mavenproject,
第一個:起名叫做,,,,first-114-,,,對,,你沒看錯,,為了加強理解,,114就想當於中介了
第二個:起名叫做,,,,first-person,,,對,,就是個人的意思,,為了加強理解,相當於一個服務了
第三個:起名叫做,,,,first-police,,,對,,就是警察局的意思,,為了加強理解,相當於另一個服務了了
然後,說一下個人怎麼規劃的,,警察在114中心註冊了自己的一個聯絡方式,,然後個人通過找到這個聯絡方式,與114聯絡,然後找到警察的這個聯絡方式
其實就是,其中的一個服務A在服務控制中心註冊了,,另一個服務B想要用到服務A裡面的內容,通過服務控制中心去找服務A註冊的名字,然後並使用
第二步:開始搭建114(服務註冊中心這個專案)
1、pom檔案下引入Springcloud的依賴:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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-server</artifactId>
</dependency>
</dependencies>
2、建立服務註冊中心的啟動類
package yjc.crazy.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//Springboot的啟動的那個註解,不明白的,先看Springboot的知識去
@SpringBootApplication
//啟動eurekaServer的註解
@EnableEurekaServer
public class ServerApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApp.class)
.web(true)
.run(args);
}
}
3、在resources目錄下面編寫配置檔案application.yml檔案
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
肯定有同學問我這兩個配置了fale的東西是啥,,,我就給你在它的文件裡面找到了
百度翻譯解釋了下:(ps:自行理解)
指示此例項是否應該將其資訊註冊到尤里卡伺服器以供其他人發現。 在某些情況下,您不希望您的例項被發現,而您只是想發現其他例項。/*****************************************到這裡第一個服務註冊中心的demo就實現了**************************************************************/
第三步:建立服務提供者的一端(警察)
1、在pom檔案中引入需要的依賴
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2、編寫一個啟動類
package yjc.crazy.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PoliceServer {
public static void main(String[] args) {
new SpringApplicationBuilder(PoliceServer.class).web(true).run(args);
}
}
3、在編寫一個實體類
package yjc.crazy.cloud;
public class Police {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
4、在編寫一個Controller層
package yjc.crazy.cloud;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PosiceController {
@RequestMapping("call/{id}")
private Police call(@PathVariable Integer id) {
Police police = new Police();
police.setId(id);
police.setName("first eureak");
return police;
}
}
5、在寫好配置檔案
server:
port: 8081
spring:
application:
name: first-police
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
其實這樣就編寫好了,並且我們通過正常的請求是可以進行訪問的
可以看到這個服務是可以進行使用的了,,
第三步:編寫另外一個呼叫者的服務,就是個人的服務
1、在pom引入依賴
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
2、編寫啟動類(其實這個啟動類跟警察那個啟動類是一個樣子的,都是用了SpringbootApplication和EnableEurekaclient的這個註解)
package yjc.crazy.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PersonServer {
public static void main(String[] args) {
new SpringApplicationBuilder(PersonServer.class).web(true).run(args);
}
}
3、寫一個Controller層來支援訪問
package yjc.crazy.cloud;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Configuration
public class TestController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@GetMapping("/router")
public String router() {
RestTemplate tpl = getRestTemplate();
String json = tpl.getForObject("http://first-police/call/1", String.class);
return json;
}
}
看到沒,這裡面的寫法就有點有意思了,,,是不是,,用的是RestTemplate這個類,,大家不懂得可以百度看看,漲漲知識,這裡面有一個地方就是那個看起來像url的地方,用的竟然不是正兒八經的url,,,竟然是我們剛剛寫的那個警察的服務的名字,,看到了麼,,,然後後面跟的就是平常寫的那個Controller的攔截的欄位就可以了,
4、編寫配置檔案
server:
port: 8082
spring:
application:
name: first-person
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
這裡面person這個服務也是需要驚醒註冊的才可以
總結:這裡面我們其實寫了三個Springboot的專案,每一個專案呢,都有一個啟動類,只是伺服器中心用的註解是EnableEurekaServer這個註解,客戶端用的是EnableEurekaClient這個註解,其次就是引入的依賴的不同,不同的地方只有最後一個依賴,,,大家可以看pom檔案的東西,,這裡放一個對比圖
///*********************************************************************************************************************************************************************
可以看到區別,只有這個jar包的依賴是不一樣的哦,,,,,,,然後重點,,,!!!!重點!!!!!我們的啟動順序是:
先啟動114這個服務,,,在啟動警察這個服務,,最後啟動個人的這個服務,,,,當我們啟動都不報錯的時候,我們進入eureka的後臺其實是可以看到這兩個服務的名字的
好了寫到這裡基本也就可以了,,然後我會把我的程式碼上傳到github上面,供大家訪問下載