【微服务】Springcloud学习笔记(一) —— Eureka

来源:互联网 发布:php防止ajax重复提交 编辑:程序博客网 时间:2024/06/07 08:55

SpringCloud基本特性

  • 分布式配置中心

  • 服务注册/发现(Eureka)

  • 智能路由(Zuul)

  • 服务间的调用

  • 客户端负载均衡(Ribbon)

  • 断路器(Hystrix)

  • 分布式消息管理


demo结构

这里写图片描述

服务发现:Eureka客户端

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服务发现的一种服务和客户端。这种服务是可以被高可用性配置的和部署,并且在注册的服务当中,每个服务的状态可以互相复制给彼此

开启Eureka

@EnableEurekaServer //注册服务器中心@SpringBootApplicationpublic class SpringcloudDemoApplication { public static void main(String[] args) {   SpringApplication.run(SpringcloudDemoApplication.class, args); }}

application.properties

#SpringCloud - Eureka Configurationserver.port=1111eureka.client.register-with-eureka=falseeureka.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

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中对服务信息的输出。@SpringBootApplicationpublic class SpringcloudServiceApplication {    public static void main(String[] args) {    SpringApplication.run(SpringcloudServiceApplication.class, args);    }}

@RestControllerpublic 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-serviceserver.port=2222eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

访问

http://localhost:2222/add?a=1&b=2

这里写图片描述这里写图片描述


  • @EnableEurekaClient
    • 声明Eureka注册服务(etc.实例的行为由eureka.instance.*的配置项来决定,但是你最好确保你的spring.application.name有个默认值)
  • @EnableDiscoveryClient
    • 定位发现Eureka客户端

eureka 客户端官方例子

@Configuration@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@RestControllerpublic 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


Eureka服务的身份验证

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.boot</groupId>  <artifactId>spring-boot-starter-security</artifactId></dependency>

#security.basic.enabled=true#fail#user.name=admin    #user.password=123

这里写图片描述


健康指标和状态页面

Springboot监控
Springboot-admin

官网

eureka.instance.statusPageUrlPath=${management.context-path}/infoeureka.instance.healthCheckUrlPath=${management.context-path}/health


        <!-- Springboot监控 -->        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-server</artifactId>             <version>1.4.2</version>        </dependency>        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-server-ui</artifactId>            <version>1.4.2</version>        </dependency>

@EnableAdminServer //开启服务监控

spring.boot.admin.url=http://localhost:${server.port}spring.jackson.serialization.indent_output=trueendpoints.health.sensitive=false

http://localhost:1111/metrics
http://localhost:1111/health

springcloud中文文档

0 0
原创粉丝点击