zoukankan      html  css  js  c++  java
  • Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现。

    • 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按照服务名分类组织服务清单,服务注册中心还需要以心跳的方式去监控清单中的服务是否可用,若不可用需要从服务清单中剔除,达到排除故障服务的效果。
    • 服务发现:由于在服务治理框架下运行,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。

    Spring Cloud Eureka 使用 Netflix Eureka 来实现服务注册与发现,即包括了服务端组件,也包含了客户端组件,并且服务端和客户端均采用 Java 编写,所以 Eureka 主要适用与通过 Java 实现的分布式系统,或是与 JVM 兼容语言构建的系统,但是,由于 Eureka 服务端的服务治理机制提供了完备的 RESTful API,所以他也支持将非 Java 语言构建的微服务纳入 Eureka 的服务治理体系中来。

    一、开发工具说明

    • 开发工具:Spring Tool Suite 3.9.4.RELEASE

    • JDK版本:1.8.0_162

    • MAVEN版本:3.5.3

    二、创建Eureka注册中心

    New Spring Starter Project

    选择 Eureka Server ,点击finish项目创建完成

    此时pom.xml内容为:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.carry.springcloud</groupId>
     7     <artifactId>eureka-server</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>eureka-server</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.4.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    26     </properties>
    27 
    28     <dependencies>
    29         <dependency>
    30             <groupId>org.springframework.cloud</groupId>
    31             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    32         </dependency>
    33 
    34         <dependency>
    35             <groupId>org.springframework.boot</groupId>
    36             <artifactId>spring-boot-starter-test</artifactId>
    37             <scope>test</scope>
    38         </dependency>
    39     </dependencies>
    40 
    41     <dependencyManagement>
    42         <dependencies>
    43             <dependency>
    44                 <groupId>org.springframework.cloud</groupId>
    45                 <artifactId>spring-cloud-dependencies</artifactId>
    46                 <version>${spring-cloud.version}</version>
    47                 <type>pom</type>
    48                 <scope>import</scope>
    49             </dependency>
    50         </dependencies>
    51     </dependencyManagement>
    52 
    53     <build>
    54         <plugins>
    55             <plugin>
    56                 <groupId>org.springframework.boot</groupId>
    57                 <artifactId>spring-boot-maven-plugin</artifactId>
    58             </plugin>
    59         </plugins>
    60     </build>
    61 
    62 
    63 </project>

    属性文件配置

    将resources目录下的application.properties重命名为application.yml(建议使用格式化yaml语言,好处的话用过就知道),添加如下内容

    server:
      port: 8761 #eureka默认端口号
    
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    解释:

    • eureka.client.registerWithEureka:表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false。
    • eureka.client.fetchRegistry:表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false。
    • eureka.client.serviceUrl.defalseZone:设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割。

    启动类配置

    添加@EnableEurekaServer注解即可

     1 package com.carry.springcloud;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
     6 
     7 @EnableEurekaServer
     8 @SpringBootApplication
     9 public class EurekaServerApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(EurekaServerApplication.class, args);
    13     }
    14 }

    使用SpringBootApp启动,访问地址:localhost:8761

    正常打开以上网页,说明Eureka注册中心配置成功。

    三、创建客户端项目

    创建项目步骤同上,这里我们选择Eureka Discovery、Web

    pom.xml内容:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.carry.springcloud</groupId>
     7     <artifactId>service-eureka-client</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>service-eureka-client</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.4.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    26     </properties>
    27 
    28     <dependencies>
    29         <dependency>
    30             <groupId>org.springframework.boot</groupId>
    31             <artifactId>spring-boot-starter-web</artifactId>
    32         </dependency>
    33         <dependency>
    34             <groupId>org.springframework.cloud</groupId>
    35             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    36         </dependency>
    37 
    38         <dependency>
    39             <groupId>org.springframework.boot</groupId>
    40             <artifactId>spring-boot-starter-test</artifactId>
    41             <scope>test</scope>
    42         </dependency>
    43     </dependencies>
    44 
    45     <dependencyManagement>
    46         <dependencies>
    47             <dependency>
    48                 <groupId>org.springframework.cloud</groupId>
    49                 <artifactId>spring-cloud-dependencies</artifactId>
    50                 <version>${spring-cloud.version}</version>
    51                 <type>pom</type>
    52                 <scope>import</scope>
    53             </dependency>
    54         </dependencies>
    55     </dependencyManagement>
    56 
    57     <build>
    58         <plugins>
    59             <plugin>
    60                 <groupId>org.springframework.boot</groupId>
    61                 <artifactId>spring-boot-maven-plugin</artifactId>
    62             </plugin>
    63         </plugins>
    64     </build>
    65 
    66 
    67 </project>

    属性文件配置

    application.yml内容如下:

    server:
      port: 8080
    spring:
      application:
        name: service-eureka-clienteureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/

    启动类配置

    添加@EnableEurekaClient注解即可

     1 package com.carry.springcloud;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     6 
     7 @EnableEurekaClient
     8 @SpringBootApplication
     9 public class ServiceEurekaClientApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(ServiceEurekaClientApplication.class, args);
    13     }
    14 }

    启动项目,如下图,客户端注册成功

    四、给Eureka添加安全认证

    默认情况下我们就直接直接访问到eureka的界面了。如果不想让所有人都能访问到eureka的界面,可以加上权限认证,输入账号密码才能访问。

    服务端:

    Eureka Server中pom文件添加spring-boot-starter-security依赖

    1 <dependency>
    2     <groupId>org.springframework.boot</groupId>
    3     <artifactId>spring-boot-starter-security</artifactId>
    4 </dependency>

    yml文件内容修改为

    server:
      port: 8761 #eureka默认端口号
    spring:
      security:
        user:
          name: admin
          password: 123456
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

    启动项目,访问地址:localhost:8761,会提示我们输入用户名和密码

    输入用户名和密码后就回进入Eureka的主页面

    客户端:

     application.yml配置文件内容修改:

    eureka:
      client:
        serviceUrl:
          defaultZone: http://admin:123456@localhost:8761/eureka/

    在地址里加上用户名密码,然后运行还是报错无法注册到eureka,是因为新版本的security默认开启csrf了,关掉就好了

    服务端Eureka Server项目中新建一个配置类,如下

     1 package com.carry.springcloud.config;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
     5 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
     6 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
     7 
     8 @EnableWebSecurity
     9 @Configuration
    10 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    11 
    12     @Override
    13     protected void configure(HttpSecurity http) throws Exception {
    14         http.csrf().disable(); // 关闭csrf
    15         http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
    16     }
    17 
    18 }

    重新启动eureka,访问localhost:8761,出现不一样的登录提示

    输入用户名和密码登录后,启动client项目,此时再刷新eureka页面,发现已经成功注册!

  • 相关阅读:
    2018.5.22 Oracle安装配置在虚拟机中外部电脑连接服务
    2018.5.20 oracle强化练习
    2018.5.19 Oracle数据操作和管理表的综合练习
    2018.5.18 AndroidStudio创建项目出错
    2018.5.17 oracle函数查询
    2018.5.14 XML文档类型定义----DTD
    2018.5.13 oracle遇到的问题
    二元搜索算法(分治法)
    循环队列(弥补队列顺序储存的不足)
    队列的顺序储存
  • 原文地址:https://www.cnblogs.com/carrychan/p/9447256.html
Copyright © 2011-2022 走看看