利用idea快速搭建一個spring-cloud(圖文)
package com.example.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; @SpringBootApplication //掃描所有包 @ComponentScan("com.test") //聲明為注冊服務 @EnableEurekaClient //把調用注冊子模塊接口引入到Spring容器中(不加此注解會出現(xiàn)找不到@FeignClient修飾的接口) @EnableFeignClients("com.test")//包路徑解決啟動類在別的包下問題 public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
1、創(chuàng)建一個空的maven項目!
2、創(chuàng)建一個注冊中心模塊
3、配置注冊中心
package com.example.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication //聲明為注冊中心 @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
配置文件改用yml,配置如下:
server: #運行端口 port: 8888 eureka: instance: #注冊ip hostname: localhost client: #禁止自己當做服務注冊 register-with-eureka: false #屏蔽注冊信息 fetch-registry: false #注冊url serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
啟動成功后,訪問本地+端口即可看到注冊中心頁面,說明成功啦!
3、創(chuàng)建一個服務提供者(就是常寫的spring-boot)
服務提供者配置,如下:
package com.example.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication //聲明為注冊服務 @EnableEurekaClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
配置文件改用yml,配置如下:
eureka: client: serviceUrl: #服務注冊地址 defaultZone: http://localhost:8888/eureka/ server: #運行端口 port: 8001 spring: application: #服務注冊名稱 name: service-provider
按照寫springboot那樣寫一個查庫接口
package com.example.provider; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //聲明為注冊服務 @EnableEurekaClient //掃描所有包 @ComponentScan("com.test") //掃描mapper @MapperScan("com.test.mapper") public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
yml配置
eureka: client: serviceUrl: #服務注冊地址 defaultZone: http://localhost:8888/eureka/ server: #運行端口 port: 8001 spring: application: #服務注冊名稱 name: service-provider #數(shù)據(jù)庫鏈接 datasource: username: root password: yh996112 url: jdbc:mysql://localhost:3306/yanghao?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver #mybatis配置 mybatis: mapper-locations: classpath:mapper/*Mapper.xml type-aliases-package: com.test.doman
OK,查庫然后接口返回數(shù)據(jù)成功!
咱們的服務也在注冊中心中注冊成功啦!
4、創(chuàng)建一個消費者服務
點擊完成創(chuàng)建該模塊
啟動器配置
package com.example.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; @SpringBootApplication //聲明為注冊服務 @EnableEurekaClient //把調用注冊子模塊接口引入到Spring容器中(不加此注解會出現(xiàn)找不到@FeignClient修飾的接口) @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
配置文件改用yml,配置如下:
eureka: client: serviceUrl: #服務注冊地址 defaultZone: http://localhost:8888/eureka/ server: #運行端口 port: 8002 spring: application: #服務注冊名稱 name: service-consumer
將服務提供者模塊中的controller復制到消費者模塊,如果涉及doman中文件就一并復制過去。
package com.example.consumer.service; import com.example.consumer.doman.Test; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; /** * @Description:使用@FeignClient注解調用注冊子模塊接口方法 * @Author :y */ //注冊子模塊名稱 @FeignClient("service-provider") public interface TestService { //接口訪問地址 @GetMapping("index/index") public Test getTest(@RequestParam("id") Integer id); }
接口調用該service
啟動消費者,進行接口測試!
訪問消費者接口沒有問題,成功的調用了服務提供者的接口返回了數(shù)據(jù)!??!
以上就是idea快速部署springCloud的全部過程,其中發(fā)現(xiàn)了一個問題,在消費者模塊中啟動器貌似無法使用@ComponentScan注解掃描包,使用后啟動會報錯???具有原因沒有了解,建議不要把啟動類放在別的包。
2022-03-14:該問題解決!
問題描述:
在消費者模塊中,當啟動類在別的包下時,使用@ComponentScan掃描包來自動javaBean
但是因為service接口中的@FeignClient注解同樣不在啟動類的包下,所以僅用@ComponentScan掃描包而找不到@FeignClient同樣會報錯的。
所以在啟動類的@EnableFeignClients注解應該指定包去掃描一下?。?!
消費者模塊配置如下:
到此這篇關于利用idea快速搭建一個spring-cloud(圖文)的文章就介紹到這了,更多相關idea搭建spring-cloud內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
maven-maven使用-P參數(shù)打包不同環(huán)境問題
這篇文章主要介紹了maven-maven使用-P參數(shù)打包不同環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11java String類常量池分析及"equals"和"==”區(qū)別詳細介紹
這篇文章主要介紹了java String類常量池分析及"equals"和"==”區(qū)別詳細介紹的相關資料,需要的朋友可以參考下2016-12-12Java多線程--讓主線程等待所有子線程執(zhí)行完畢在執(zhí)行
Java主線程等待所有子線程執(zhí)行完畢在執(zhí)行,其實在我們的工作中經(jīng)常的用到,本篇文章就介紹了Java多線程--讓主線程等待所有子線程執(zhí)行完畢在執(zhí)行,有需要的可以了解一下。2016-11-11