引言
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
spring cloud
创建“服务注册中心”
创建一个基础的Spring Boot工程,工程目录如下:
并在pom.xml中引入需要的依赖内容:
org.springframework.cloud
spring-cloud-starter-eureka
-->
通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:
@SpringBootApplication
@EnableEurekaServer
public classApplicaiton {
public static voidmain(String[] args) {
//SpringApplication.run(Applicaiton.class,args);
//new SpringApplicationBuilder(Applicaiton.class).web(true).run(args);
newSpringApplicationBuilder(Applicaiton.class).run(args);
}
}
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties中问增加如下配置:
server.port=1111
eureka.client.register-with-eureka=false#设置是否从注册中心获取注册信息(缺省true),因为这是一个单点的EurekaServer
#不需要同步其他EurekaServer节点的数据,故设为falseeureka.client.fetch-registry=false#设置是否将自己作为客户端注册到注册中心(缺省true)#这里为不需要(查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient)#在未设置defaultZone的情况下,注册中心在本例中的默认地址就是http://127.0.0.1:1100/eureka/
#但奇怪的是,启动注册中心时,控制台还是会打印这个地址的节点:http://localhost:8761/eureka/
#而实际服务端注册时,要使用1100端口的才能注册成功,8761端口的会注册失败并报告异常--说法不正确
#打印信息如下:2017-05-24 11:16:49.148INFO 14800 --- [main] c.n.eureka.DefaultEurekaServerContext: Initializing ...
2017-05-24 11:16:49.156INFO 14800 --- [main] c.n.eureka.cluster.PeerEurekaNodes: Adding new peer nodes [http://localhost:1111/eureka/]eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/#实际测试:若修改尾部的eureka为其他的,比如/myeureka,注册中心启动没有问题,但服务端在注册时会失败#报告异常:com.netflix.discovery.shared.transport.TransportException:cannot execute request on any known servereureka.server.enable-self-preservation=falseeureka.server.eviction.interval-timer-in-ms=4000
为了与后续要进行注册的服务区分,这里将服务注册中心的端口通过server.port属性设置为1111。
可以看到下面的页面,其中还没有发现任何服务
创建“服务提供方”
下面我们创建提供服务的客户端,并向服务注册中心注册自己。
假设我们有一个提供计算功能的微服务模块,我们实现一个RESTful API,通过传入两个参数a和b,最后返回a + b的结果。
首先,创建一个基本的Spring Boot应用,在pom.xml中,加入如下配置:
org.springframework.cloud
spring-cloud-starter-eureka-server
-->
其次,实现/add请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。
@RestController
public classComputerController {
private finalLoggerlogger= Logger.getLogger(getClass());
@Autowired
privateDiscoveryClientdiscoveryclient;
@Autowired
privateEurekaClienteurekaClient;
@RequestMapping(value="/add",method= RequestMethod.GET)
publicInteger add(@RequestParamInteger a,@RequestParamInteger b) {
//ServiceInstance instance = client.getLocalServiceInstance();
InstanceInfo instance =eurekaClient.getNextServerFromEureka("compute-service",false);
Integer r = a + b;
logger.info("/add, host:"+instance.getHostName() +",serviceId:"+ instance.getInstanceId() +",result:"+ r);
returnr;
}
}
最后在主类中通过加上@EnableEurekaClient注解,该注解能激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。
@SpringBootApplication
@EnableEurekaClient
public classComputeServiceApplication {
public static voidmain(String[] args) {
//new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
newSpringApplicationBuilder(ComputeServiceApplication.class).run(args);
}
}
我们在完成了服务内容的实现之后,再继续对application.properties做一些配置工作,具体如下:
server.port=2222
spring.application.name=compute-serviceeureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
eureka.instance.lease-renewal-interval-in-seconds=10eureka.instance.lease-expirtion-duration-in-seconds=30eureka.client.healthcheck.enabled=true
通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。
为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。
可以看到,我们定义的服务被注册了。
验证
发布的微服务所暴露出去的都是HTTP的接口
目前为止,我们完成了Spring Cloud
Netflix Eureka搭建注册中心的基本示例,不过也只是尝尝鲜
因为它还存在着很多问题,比如
什么是自我保护模式
服务提供方关闭之后,在注册中心看到的状态还是UP
注册中心的服务提供方显示的名字,是不是可以自定义?
总结
以 上就是我对Java开发大型互联网Spring Cloud服务注册中心源码分析 问题及其优化总结,分享给大家,觉得收获的话可以点个关注收藏转发一波喔,谢谢大佬们支持!
最后,每一位读到这里的网友,感谢你们能耐心地看完。希望在成为一名更优秀的Java程序员的道路上,我们可以一起学习、一起进步!都能赢取白富美,走向架构师的人生巅峰!
想了解学习Java方面的技术内容以及Java技术视频的内容可加群:722040762 验证码:(666 必过)欢迎大家的加入哟!