eureka 用以服務(wù)發(fā)現(xiàn)、服務(wù)注冊(cè),比較流行的有consul
eureka為netflix開(kāi)源軟件,分為三個(gè)部分:
eureka服務(wù):用以提供服務(wù)注冊(cè)、發(fā)現(xiàn),已一個(gè)war的形式提供 http://search.maven.org/#search%7Cga%7C1%7Ceureka-server
或者編譯源碼,將war拷貝進(jìn)tomcat即可提供服務(wù)
eureka-server: 相對(duì)client端的服務(wù)端,為客戶端提供服務(wù),通常情況下為一個(gè)
集群
eureka-client:客戶端,通過(guò)向eureka服務(wù)
發(fā)現(xiàn)注冊(cè)的可用的eureka-server,向后端發(fā)送請(qǐng)求
spring cloud eureka
分為兩部分
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.lkl.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka</artifactId> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.0.7.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--表示為web工程--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--暴露各種指標(biāo)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies></project>
啟動(dòng)應(yīng)用 Application.Java
package com.lkl.springcloud.eureka;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by liaokailin on 16/4/30. */@SpringBootApplication@EnableEurekaClient@RestController@EnableAutoConfigurationpublic class Application { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}
創(chuàng)建配置application.properties
server.port=9090spring.application.name=eureka.client#eureka.client.serviceUrl.defaultZone=http://120.76.145.187:8080/eureka-server-1.4.6/v2/eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/eureka.instance.appname=eureka.client.01#eureka.client.registerWithEureka=true#eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone
配置eureka服務(wù)地址
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.lkl.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server</artifactId> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.0.7.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!--表示為web工程--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--暴露各種指標(biāo)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies></project>
啟動(dòng)應(yīng)用Application.java
package com.lkl.springcloud.eureka;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/** * Created by liaokailin on 16/4/30. */@EnableEurekaServer@SpringBootApplicationpublic class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}
配置application.yml
server: port: 7070spring: application: name: server-01eureka: client: serviceUrl: defaultZone: http://localhost:7070/eureka/ instance: metadataMap: instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
spring.application.name
表示應(yīng)用名稱,集群名稱需要一致 defaultZone
表示向自身注冊(cè),例子中有三個(gè)server節(jié)點(diǎn)構(gòu)成集群,其余兩個(gè)兩個(gè)節(jié)點(diǎn)也向該端口注冊(cè) instanceId
表示eureka instance 標(biāo)識(shí),需要唯一,如果不配置,多個(gè)節(jié)點(diǎn)最終只會(huì)有一個(gè)生效
同樣的配置第二個(gè)節(jié)點(diǎn),第三個(gè)節(jié)點(diǎn)
啟動(dòng)所有服務(wù)端應(yīng)用
訪問(wèn) http://localhost:7070 可以查看eureka注冊(cè)服務(wù)信息
訪問(wèn) http://localhost:7070/eureka/apps 可以查看metadata
在客戶端創(chuàng)建Component
DiscoveryService.java
package com.lkl.springcloud.eureka;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * Created by liaokailin on 16/4/30. */@Component@RestControllerpublic class DiscoveryService { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/discovery") public String doDiscoveryService(){ StringBuilder buf = new StringBuilder(); List<String> serviceIds = discoveryClient.getServices(); if(!CollectionUtils.isEmpty(serviceIds)){ for(String s : serviceIds){ System.out.println("serviceId:" + s); List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s); if(!CollectionUtils.isEmpty(serviceInstances)){ for(ServiceInstance si:serviceInstances){ buf.append("["+si.getServiceId() +" host=" +si.getHost()+" port="+si.getPort()+" uri="+si.getUri()+"]"); } }else{ buf.append("no service."); } } } return buf.toString(); }}
訪問(wèn) http://localhost:9090/discovery 實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)。
ok ~ it’s work ! more about is here
轉(zhuǎn)載請(qǐng)注明
http://blog.csdn.net/liaokailin/article/details/51314001
歡迎關(guān)注,您的肯定是對(duì)我最大的支持
聯(lián)系客服