springcloud(第三篇)springcloud eureka 服务注册与发现 *****
来源:互联网 发布:vb的flag 编辑:程序博客网 时间:2024/04/29 17:47
http://blog.csdn.net/liaokailin/article/details/51314001
***********************************************************
spring cloud eureka
eureka 用以服务发现、服务注册,比较流行的有consul
简介
eureka为netflix开源软件,分为三个部分:
eureka服务:用以提供服务注册、发现,已一个war的形式提供 http://search.maven.org/#search%7Cga%7C1%7Ceureka-server
或者编译源码,将war拷贝进tomcat即可提供服务eureka-server: 相对client端的服务端,为客户端提供服务,通常情况下为一个
集群eureka-client:客户端,通过向
eureka服务
发现注册的可用的eureka-server,向后端发送请求
spring cloud eureka
spring cloud eureka
分为两部分
- @EnableEurekaClient: 该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务
- @EnableEurekaServer: 该注解表明应用为eureka服务,有可以联合多个服务作为集群,对外提供服务注册以及发现功能
client 端
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> <!--暴露各种指标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
启动应用 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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
创建配置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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
eureka.client.serviceUrl.defaultZone
配置eureka服务地址
server
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> <!--暴露各种指标--> <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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
启动应用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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
配置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}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
spring.application.name
表示应用名称,集群名称需要一致 defaultZone
表示向自身注册,例子中有三个server节点构成集群,其余两个两个节点也向该端口注册 instanceId
表示eureka instance 标识,需要唯一,如果不配置,多个节点最终只会有一个生效
同样的配置第二个节点,第三个节点
启动所有服务端应用
访问 http://localhost:7070 可以查看eureka注册服务信息
访问 http://localhost:7070/eureka/apps 可以查看metadata
服务发现
在客户端创建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(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
访问 http://localhost:9090/discovery 实现服务发现。
ok ~ it’s work ! more about is here
- 顶
- 4
- 踩
- 1
- springcloud(第三篇)springcloud eureka 服务注册与发现
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
- springcloud(第三篇)springcloud eureka 服务注册与发现
- SpringCloud服务的注册与发现(Eureka)
- SpringCloud的服务注册与发现Eureka
- springcloud eureka服务注册与发现
- springcloud(第一篇)springcloud eureka 服务注册与发现
- SpringCloud--服务的注册与发现(Eureka)
- SpringCloud 学习记录(一):服务注册与发现(eureka+feign)
- springcloud学习笔记一:eureka服务注册与发现
- SpringCloud : 服务的注册与发现(Eureka)
- SpringCloud第一篇-Eureka:服务的注册与发现
- SpringCloud教程一:服务注册与发现(Eureka)
- 【Spring Cloud】--SpringCloud的服务注册与发现【Eureka】
- SpringCloud服务发现Eureka
- springcloud-服务提供与注册Eureka
- eclipse springcloud服务发现服务注册(Eureka) 学习日志
- SpringCloud——Eureka服务注册和发现
- 超级厉害的JavaScript —— ECMAScript 6 标准(2) —— let 和 const
- github上传文件,删除远端文件(GUI)
- 10-Oracle入门之子查询
- Linux下调整根目录的空间大小
- Android 对图片的缩放,拖地,跟系统图库的效果一样
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
- 深入理解java异常处理机制
- java SSH所需要导入的jar包
- MySQL 5.7中新增的表gtid_executed,看看是否解决了你的痛点
- 用“p2p+去中心化”做CDN的云熵科技
- Struts遍历标签s:iterator总结
- 编译提示"_sqlite3_column_int", referenced from: 错误 ---IOS中添加sqlite报错
- android之内存泄漏检测和解决方法及LeakCanary使用
- Redis 基础