Eureka是一個服務(wù)治理組件,它主要包括服務(wù)注冊和服務(wù)發(fā)現(xiàn),主要用來搭建服務(wù)注冊中心。
Eureka 是一個基于 REST 的服務(wù),用來定位服務(wù),進(jìn)行中間層服務(wù)器的負(fù)載均衡和故障轉(zhuǎn)移;
Eureka是Netflix 公司開發(fā)的,Spring Cloud發(fā)現(xiàn)eureka很好使,因此將eureka封裝到自己的模塊中。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-568601.html
1、要使用eureka,首先要創(chuàng)建一個服務(wù),eureka本身也是一個微服務(wù)
引入springcloud和eureka-server
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
這個地方有一個坑,我折騰了半天才啟動成功。springboot和springcloud是有對應(yīng)關(guān)系的
SpringCloud版本 SpringBoot版本 2021.0.1-SNAPSHOT Spring Boot >=2.6.4-SNAPSHOT and <2.7.0-M1 2021.0.0 Spring Boot >=2.6.1 and <2.6.4-SNAPSHOT 2021.0.0-RC1 Spring Boot >=2.6.0-RC1 and <2.6.1 2021.0.0-M3 Spring Boot >=2.6.0-M3 and <2.6.0-RC1 2021.0.0-M1 Spring Boot >=2.6.0-M1 and <2.6.0-M3 2020.0.5 Spring Boot >=2.4.0.M1 and <2.6.0-M1 Hoxton.SR12 Spring Boot >=2.2.0.RELEASE and <2.4.0.M1 Hoxton.BUILD-SNAPSHOT Spring Boot >=2.2.0.BUILD-SNAPSHOT Hoxton.M2 Spring Boot >=2.2.0.M4 and <=2.2.0.M5 Greenwich.BUILD-SNAPSHO Spring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4 Greenwich.SR2 Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT Greenwich.M1 Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE Finchley.BUILD-SNAPSHOT Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3 Finchley.SR4 Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT Finchley.RC2 Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE ?Finchley.RC1 ?Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE Finchley.M9 Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE Finchley.M7 Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2 Finchley.M6 Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1 Finchley.M5 Spring Boot >=2.0.0.M7 and <=2.0.0.M7 Finchley.M4 Spring Boot >=2.0.0.M6 and <=2.0.0.M6 Finchley.M3 Spring Boot >=2.0.0.M5 and <=2.0.0.M5 Finchley.M2 Spring Boot >=2.0.0.M3 and <2.0.0.M5 Edgware.SR5 1.5.20.RELEASE Edgware.SR5 1.5.16.RELEASE Edgware.RELEASE 1.5.9.RELEASE Dalston.RC1 1.5.2.RELEASE
為eureka服務(wù)增加注解說明
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
為eureka服務(wù)增加配置
server: port: 9000 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka?
這里我使用9000端口作為eureka的服務(wù)端口?
啟動后可以看到eureka服務(wù)的展示頁面,可以看到,這時候是沒有服務(wù)注冊進(jìn)來的。
?2、創(chuàng)建微服務(wù)注冊到eureka服務(wù)
pom文件,簡化版,其他springcloud和springboot的引用和eureka相同
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
服務(wù)啟動類
@SpringBootApplication @EnableEurekaClient public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
配置文件
server: port: 1001 eureka: instance: prefer-ip-address: true hostname: localhost client: service-url: defaultZone: http://${eureka.instance.hostname}:9000/eureka/ spring: application: name: orderservice
這里我使用1001作為第一個微服務(wù)?端口號,下邊使用1002創(chuàng)建另一個微服務(wù),配置同理
啟動后
?可以看到,兩個服務(wù)均已經(jīng)成功注冊到eureka服務(wù)。
3、創(chuàng)建另一個微服務(wù)去調(diào)用已經(jīng)注冊進(jìn)去的服務(wù),配置相同,這里只截圖調(diào)用相關(guān)代碼
@Autowired RestTemplate restTemplate; @RequestMapping("indexClient.do") public @ResponseBody String indexClient() { return restTemplate.getForEntity("http://orderservice/index.do", String.class).getBody(); }
使用的是RestTemplate,RestTemplate簡化了發(fā)起 HTTP 請求以及處理響應(yīng)的過程,并且支持 REST,類似于自己創(chuàng)建的http請求類。
這里有個小坑,如果clean服務(wù)后配置文件application.yml有可能會被從target中刪除,這里需要手動復(fù)制到target中,不然會啟動失敗,而且配置的server端口不生效,謹(jǐn)記。
為了不至于學(xué)習(xí)資料丟失,代碼已經(jīng)上傳至以下地址:
(3條消息) springboot整合eureka源代碼,學(xué)習(xí)資料-Java文檔類資源-CSDN文庫文章來源:http://www.zghlxwxcb.cn/news/detail-568601.html
?
到了這里,關(guān)于SpringBoot整合eureka簡記的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!