您的位置:首页 > 编程语言 > Java开发

Spring Cloud 基于eureka 服务注册中心

2017-03-16 20:06 621 查看
netflix 下开源组件eureka 提供了微服务注册和发现的解决方案,Spring整合了netflix 下若干组件用于快速构建微服务集群.本节围绕eureka
 展开,举例示意使用eureka实现微服务注册和发现的基本操作.

服务注册中心,基于eureka-server 提供,Spring为其提供了简洁的服务管理界面.
依赖如下:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<scope>runtime</scope>
</dependency>


建议添加如下依赖管理

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


服务注册中心的启用: 由核心注解@EnableEurekaServer 开启服务注册中心,当前项目仅仅作为注册中心使用不提供此外的其它服务.

@SpringBootApplication
@EnableEurekaServer
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
服务注册中心配置:Eureka 注册中心避免单点故障采用了对等架构的配置方式,使用yml配置文件能大大简化服务的配置,此处可以定义多个配置文件,指定之间的对等关系,逐个启动不同的节点即可,建议至少启动两个以上的节点.见下application.yml ,至此注册中心搭建完毕.

server:
port: 8761
spring:
profiles:
#当前激活的配置文件
active: peer1
---
#配置文件1
spring:
profiles:  peer1
eureka:
instance:
appname: ServerRegister
hostname: localhost
instance-id: ${spring.application.name}:${random.value}
server:
#关闭自我保护
enable-self-preservation: false
#清除无效节点的时间间隔
eviction-interval-timer-in-ms: 4000
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ ---
#配置文件2
spring:
profiles:  peer2
eureka:
instance:
appname: ServerRegister
hostname: localhost
instance-id: ${spring.application.name}:${random.value}
server:
enable-self-preservation: false
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

访问注册中心主管理界面:http://localhost:8761/    ,ip和端口均为注册中心配置的参数,当前没有服务注册



微服务注册,基于eureka和基于zookeeper的注册过程及配置方式有所不同,微服务消费的方式也略有不同,需要加以区分.
依赖如下:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies


作为服务提供者至少需要启用如下注解:

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan("cn.com.xiaofen")
public class ItgsQueryApplication {
public static void main(String[] args) {
SpringApplication.run(ItgsQueryApplication.class, args);
}
}
配置服务注册到服务注册中心的参数,以及注册中心的位置. 需要主意的是此处标识是相同服务的应该是virtual-host-name ,同一个服务的不同节点分布应该具有不同的instance-id
server:
port: 9911
spring:
4000
application:
name: QUERYSERVER
eureka:
instance:
#虚拟主机名称
virtual-host-name: QueryServer
#心跳时间间隔
lease-renewal-interval-in-seconds: 10
#无心跳多久后,该服务失效
lease-expiration-duration-in-seconds: 30
appname: 查询服务
#Server实例ID
instance-id: ${spring.application.name}:${random.value}
client:
service-url:
#默认的服务注册地址
defaultZone: http://host1:8761/eureka/,http://host2:8761/eureka/


该服务简单暴露一下接口用于消费.

@RestController
public class QueryController {
@RequestMapping("/query/{id}")
public String query(@PathVariable String id) {
return "{random:" + Math.random() + ",id:" + id + "}";
}
}


服务启动完毕后,刷新注册中心界面,发现已注册的服务及相关信息.



服务消费,下述依赖中部分特性暂时为使用,对于一个完整的微服务放来来说后续有必要进行补充.
依赖:

<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>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
</dependencies>


核心注解,下列@EnableEurekaClient 用于标识获取服务,@EnableFeignClients 启用REST 风格的方式访问服务暴露的接口restTemplate()方法为IOC容器主意RestTemplate Bean实例.

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RibbonClient(name = "app", configuration = RibbonConfig.class)
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}


定义服务接口,该例中引入了FeignClients 用于以REST风格同 服务提供者之间进行交互. Feign 允许通过注解的方式来定位服务及访问服务的URL来获取数据,非常便利,极大的简化了传统RPC通信接口需要统一的弊端.

@FeignClient(value = "QueryServer")
public interface QueryInterface {
@RequestMapping(value = "/query/{id}")
public String query(@PathVariable("id") String id);
}
访问微服,获取服务提供的数据.

@RestController
public class TestFeginController {
//自动注入服务接口对象,该对象由Fegin框架代理等同于访问接口中申明的服务
@Autowired
QueryInterface service;
@RequestMapping("/query/{id}")
public String query(@PathVariable("id") String id) {
return service.query(id);
}
}
配置文件application.yml ,此处主要体现服务注册中心的地址

server:
port: 7777
eureka:
client:
#当前程序不注册到eureka
register-with-eureka: false
#抓取注册中心的服务列表
fetch-registry: true
service-url:
defaultZone: http://host1:8761/eureka/,http://host2:8761/eureka/[/code] 
访问测试:



关于Spring Cloud 和 阿里Dobbo
个人更倾向于新技术,可以说未来几年基于Spring Cloud的微服务开发会形成一种趋势,Spring Cloud 为微服务提供了一站式的解决方案. 经验有限不能瞎说 可参考如下:

http://blog.didispace.com/microservice-framework/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息