Spring cloud Eureka 是SpringCloud Netflix微服務套件中的一部分,它基於Netflix Eureka做了二次封裝。主要負責完成微服務服務架構的服務治理功能。
一:搭建服務註冊中心
首先,創建一個基礎SpringBoot工程,(這裏就不介紹怎麼搭建SpringBoot工程,可自行百度),命名爲:Spring-server,並 在pom.xml中引入必要的依賴內容
<dependencies> <!--增加eureka-server的依賴--> <dependency><groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> |
通過@EnableEurekaServer註解啓動一個服務註冊中心給其他應用進行對話
/** * @EnableEurekaServer: * 該註解表明應用爲eureka服務,有可以聯合多個服務作爲集羣,對外提供服務註冊以及發現功能 */ @EnableEurekaServer @SpringBootApplication public class SpringServceApplication { public static void main(String[] args) { new SpringApplicationBuilder(SpringServceApplication.class).web(true).run(args); } } |
默認配置下,該服務中心也會將自己作爲客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行不,在application.peopertise中心添加如下配置
#服務註冊中心端口號 server.port=1111 #註冊中心的IP地址 eureka.instance.hostname=localhost #是否向服務註冊中心註冊自己,默認爲true eureka.client.register-with-eureka=false #是否需要去檢索尋找服務,因爲是註冊中心,他的任務是維護服務實例,所以不需要去尋找服務,默認是true eureka.client.fetch-registry=false #註冊中心的url eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ #服務註冊完後,會有一個心跳來持續高收益Spring-server 我還活着,已防止spring-server 剔除任務, #將服務實例從服務中排除出去,我們就該操作爲服務續約 #用於定義服務續約任務的調用間隔時間默認30秒 euekka.instance.lease-renewal-interval-in-seconds=30 #用於定義服務失效時間 默認90秒 euekka.instance.lease-expiration-duration-in-seconds=90 #關閉保護機制 eureka.server.enable-self-preservation=false |
啓動程序後,
Instances currently registered with Eureka欄是空的,說明服務註冊中心還沒有註冊任何服務
至此:服務中心就配置完成了
二:搭建服務提供者
下面是搭建一個服務提供者來加入到eureka的服務治理體系中去
創建一個SpringBoot工程,命名爲Spring-offer 並在pox.xml 中加入如下依賴
</dependencies> <dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
接着創建一個Controller處理接口,注入DiscoveryClient對象
@RestController public class HelloController { private final Logger logger = Logger.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping(value = "/text", method = RequestMethod.GET) public String text(){ return "HELLO WORD"; } |
然後在啓動項中加入@EnableDiscoveryClient註解,激活eureka中的EnableDiscoveryClient實現(
自動裝配,創建EnableDiscoveryClient接口針對Eureka客戶端的EnableDiscoveryClient實例)
@EnableDiscoveryClient @SpringBootApplication public class SpringBoot1Application { public static void main(String[] args) { new SpringApplicationBuilder(SpringBoot1Application.class).web(true).run(args); } } |
最後修改application.propertion配置文件
#命名服務 這裏命名爲hello-server spring.application.name=hello-server #命名端口號 server.port=2222 #指定服務註冊中心的地址,這裏地址爲上面創建的服務中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ |
啓動後可以看到註冊中心上有一個命爲hello-server 端口號爲2222的 服務了
三:搭建服務消費者
接下來創建一個SpringBoot工程來命名來實現消費者,命名爲 spring-ribbon ,並在pox.xml中添加如下依賴
</dependencies> <dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
#給消費者的命名 spring.application.name=ribbon-cousumer #消費者的端口號 server.port=3333 #將消費者註冊要註冊中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ #修改緩存清單名稱,已確保不會調用已出現異常的提供者 eureka.client.registry-fetch-interval-seconds=30 |
啓動項加入如下2個註解
@EnableDiscoveryClient:讓該應用註冊到Euteka客戶端,以獲取服務發現的能力
@EnableFeignClients:Feign 是一個聲明web服務客戶端,使用Feign 創建一個接口並對它進行註解,它具有可插拔的註解支持包括Feign註解與JAX-RS註解SpringCloud對Feign進行了封裝,使其支持SpringMVC標準註解和HttpMessageConverters。Feign可以 與 Eureka和Ribbon組合使用以支持負載均衡。
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class RibbonConsumerApplication { public static void main(String[] args) { new SpringApplicationBuilder(RibbonConsumerApplication.class).web(true).run(args); } } |
/** * @FeignClient用於通知Feign組件對該接口進行代理(不需要編寫接口實現),使用者可直接通過@Autowired注入。 * @RequestMapping表示在調用該方法時需要向/text 發送GET請求。 * */ @FeignClient("hello-server") public interface Servers { @RequestMapping(value = "/text", method = RequestMethod.GET) String add(); } |
在創建一個Controller 來實現 server接口 路徑設爲/add
@RestController public class Controllers { @Autowired Servers server; @RequestMapping(value = "/add", method = RequestMethod.GET) public String add() { return server.add(); } } |
啓動消費者,在遊覽器打印消費的接口路徑,返回提供者的數據,如圖。
至此,Spring Cloud的 服務提供者,消費者 ,註冊中心已全部完成