欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-13 09:31:33
...

SpringCloud基本特性

  • 分布式配置中心

  • 服务注册/发现(Eureka)

  • 智能路由(Zuul)

  • 服务间的调用

  • 客户端负载均衡(Ribbon)

  • 断路器(Hystrix)

  • 分布式消息管理


demo结构

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

服务发现: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 //注册服务器中心
@SpringBootApplication
public class SpringcloudDemoApplication {
 public static void main(String[] args) {
   SpringApplication.run(SpringcloudDemoApplication.class, args);
 }
}

application.properties

#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/

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


注册到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中对服务信息的输出。
@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

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


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

eureka 客户端官方例子

@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);
    }

}

更多配置请查看 EurekaInstanceConfigBeanEurekaClientConfigBean


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

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


健康指标和状态页面

Springboot监控
Springboot-admin

官网

eureka.instance.statusPageUrlPath=${management.context-path}/info
eureka.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=true
endpoints.health.sensitive=false


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

springcloud中文文档