代碼地址以下:
http://www.demodashi.com/demo/11927.htmlhtml
關係調用說明:java
Eureka是Spring Cloud Netflix微服務套件中的一部分,能夠與Springboot構建的微服務很容易的整合起來。
Eureka包含了服務器端和客戶端組件。服務器端,也被稱做是服務註冊中心,用於提供服務的註冊與發現。Eureka支持高可用的配置,當集羣中有分片出現故障時,Eureka就會轉入自動保護模式,它容許分片故障期間繼續提供服務的發現和註冊,當故障分片恢復正常時,集羣中其餘分片會把他們的狀態再次同步回來。
客戶端組件包含服務消費者與服務生產者。在應用程序運行時,Eureka客戶端向註冊中心註冊自身提供的服務並週期性的發送心跳來更新它的服務租約。同時也能夠從服務端查詢當前註冊的服務信息並把他們緩存到本地並週期性的刷新服務狀態。spring
單首創建一個Springboot項目,並添加以下的依賴緩存
<dependencies> <!--添加Eureka服務器端依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在Springboot項目中的main入口,添加@EnableEurekaServer註解,來開啓服務註冊中心springboot
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
在默認狀況下,服務註冊中心也會把本身當作是一個服務,將本身註冊進服務註冊中心,因此咱們能夠經過配置來禁用他的客戶端註冊行爲,在application.properties中添加以下配置:服務器
server.port=3333 eureka.instance.hostname=localhost #不要向註冊中心註冊本身 eureka.client.register-with-eureka=false #禁止檢索服務 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
啓動應用,並訪問http://localhost:3333/便可看到Eureka信息面板,以下:app
從上圖看到,在"Instances currently registered with Eureka"信息中,沒有一個實例,說明目前尚未服務註冊。框架
基於文章[微服務系列] 微服務構建框架--Spring Boot中構建的第一個Springboot項目(在附件中的springbootdemo.zip實例),來進行改造。首先在pom文件中添加Eureka客戶端相關的依賴:微服務
<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>Brixton.RS5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在以前的TestRestful方法中添加以下代碼,將org.springframework.cloud.client.discovery.DiscoveryClient;對象注入,而且在日誌中打印出與服務相關的一些信息。post
@RestController public class TestRestful { private final Logger logger=Logger.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String home(){ ServiceInstance instance=client.getLocalServiceInstance(); logger.info("serviceId"+instance.getServiceId()+"host:post="+instance.getHost()+":"+instance.getPort()); return "hello spring"; } }
在主類上添加@EnableEurekaClient註解以實現Eureka中的DiscoveryClient實現。
@EnableEurekaClient @SpringBootApplication public class SpringbootdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } }
最後在配置文件application.yml中配置與服務相關的一些基本信息,如服務名、註冊中心地址(即上一節中設置的註冊中心地址http://localhost:3333/eureka)
# 設置服務名 spring: application: name: hello-service # 設置註冊中心地址 eureka: client: service-url: defaultZone: http://localhost:3333/eureka
同時訪問http://localhost:3333/能夠在Instances currently registered with Eureka中看到已經有一個服務註冊了進來,而且名稱爲HELLO-SERVICE的服務
基於Eureka的服務治理
代碼地址以下:
http://www.demodashi.com/demo/11927.html
注:本文著做權歸做者,由demo大師代發,拒絕轉載,轉載須要做者受權