分布式配置中心
服务注册/发现(Eureka)
智能路由(Zuul)
服务间的调用
客户端负载均衡(Ribbon)
断路器(Hystrix)
分布式消息管理
Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others
Eureka是Netflix服务发现的一种服务和客户端。这种服务是可以被高可用性配置的和部署,并且在注册的服务当中,每个服务的状态可以互相复制给彼此
@EnableEurekaServer //注册服务器中心
@SpringBootApplication
public class SpringcloudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDemoApplication.class, args);
}
}
#SpringCloud - Eureka Configuration
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
# "defaultZone" is a magic string fallback value that provides the service URL for any client that doesn’t express a preference
# "defaultZone"是一个神奇的字符串回退值,它提供了服务的URL给任何客户端,而这不意味优先级
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
http://localhost:1111/
Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.
Eureka通过一个服务从各个实例接收心跳信息。如果心跳接收失败超过配置的时间,实例将会正常从注册里面移除。
@EnableEurekaServer //注册服务器中心
@EnableDiscoveryClient //激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。
@SpringBootApplication
public class SpringcloudServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceApplication.class, args);
}
}
@RestController
public class ComputeController {
private final Logger logger = Logger.getLogger(getClass());
/**通过DiscoveryClient对象,在日志中打印出服务实例的相关内容*/
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return r;
}
}
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
http://localhost:2222/add?a=1&b=2
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
更多配置请查看 EurekaInstanceConfigBean 和 EurekaClientConfigBean
HTTP basic authentication will be automatically added to your eureka client if one of the eureka.client.serviceUrl.defaultZone URLs has credentials embedded in it
如果其中一个eureka.client.serviceUrl.defaultZone的url已经把凭证嵌入到它里面,那么HTTP基本的身份验证将会被自动添加到你的eureka客户端
For more complex needs you can create a @Bean of type DiscoveryClientOptionalArgs and inject ClientFilter instances into it, all of which will be applied to the calls from the client to the server
对于更复杂的需求,您可以创建一个带“@Bean”注解的“DiscoveryClientOptionalArgs”类型并且为它注入“ClientFilter”实例
**由于Eureka的限制,Eureka不支持单节点身份验证。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
#security.basic.enabled=true
#fail
#user.name=admin
#user.password=123
Springboot监控
Springboot-admin
官网
eureka.instance.statusPageUrlPath=${management.context-path}/info
eureka.instance.healthCheckUrlPath=${management.context-path}/health
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-serverartifactId>
<version>1.4.2version>
dependency>
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-server-uiartifactId>
<version>1.4.2version>
dependency>
@EnableAdminServer //开启服务监控
spring.boot.admin.url=http://localhost:${server.port}
spring.jackson.serialization.indent_output=true
endpoints.health.sensitive=false
http://localhost:1111/metrics
http://localhost:1111/health
springcloud中文文档