美文网首页
【二】Spring Cloud Eureka

【二】Spring Cloud Eureka

作者: lemonMT | 来源:发表于2019-03-08 14:30 被阅读0次

    1.搭建单机版服务注册中心

    1.1 增加Spring Cloud版本依赖

    首先创建基于Spring Boot 2.1.3.Release项目,然后增加Spring Cloud依赖

     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    1.2 增加Eureka版本依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    

    1.3 增加配置依赖

    server:
      port: 8761
    eureka:
      client:
        register-with-eureka: false  #不能自己注册自己
        fetch-registry: false  #是否同步eureka信息,单节点为false,集群则为true
      instance:
        hostname: demo-springcloud-eureka-one
        prefer-ip-address: true
    spring:
      application:
        name: demo-springcloud-eureka-one
    

    1.4 增加代码层次注解

    @EnableEurekaServer //开启服务注册能力,标明我们是服务注册中心
    @SpringBootApplication
    public class DemoSpringcloudEurekaApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoSpringcloudEurekaApplication.class, args);
        }
    }
    

    1.5 访问预览

    http://localhost:127.0.0.1:8761

    image.png

    2. 搭建安全认证的服务注册中心

    2.1 基于上述例子,增加安全认证依赖

    <!--不能让eureka裸奔,加上认证-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--不能让eureka裸奔,加上认证-->
    

    2.2 增加配置依赖

    spring:
      application:
        name: demo-springcloud-eureka-one
      security:
          user:
            password: user
            name: user
    

    2.3 增加代码依赖

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    
        // 因为新增的security覆盖了csf,所以需要重写一下,才能直接使用
        protected void configure(HttpSecurity httpSecurity)throws Exception{
    
            httpSecurity.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
        }
    }
    

    2.4 访问预览

    image.png

    3.搭建高可用集群服务注册中心

    3.1搭建两套eureka项目

    搭建两个项目分别为:
    demo-springcloud-eureka-one
    demo-springcloud-eureka-two

    3.2 两个项目的配置项

    demo-springcloud-eureka-one 配置改动:

    server:
      port: 8761
    eureka:
      client:
        service-url:
          defaultZone: http://user:user@demo-springcloud-eureka-two:8762/eureka/
        register-with-eureka: false  #不能自己注册自己
        fetch-registry: true  #是否同步eureka信息,单节点为false,集群则为true
      instance:
        hostname: demo-springcloud-eureka-one
        prefer-ip-address: true
    spring:
      application:
        name: demo-springcloud-eureka-one
      security:
          user:
            password: user
            name: user
    

    demo-springcloud-eureka-two 配置改动:

    server:
      port: 8762
    eureka:
      client:
        service-url:
          defaultZone: http://user:user@demo-springcloud-eureka-one:8761/eureka/
        register-with-eureka: false  #不能自己注册自己
        fetch-registry: true  #是否同步eureka信息,单节点为false,集群则为true
      instance:
        hostname: demo-springcloud-eureka-two
        prefer-ip-address: true
    spring:
      application:
        name: demo-springcloud-eureka-two
      security:
          user:
            password: user
            name: user
    

    如果是多个注册中心,请在defaultZone这里以逗号隔开即可。

    3.3 配置hosts

    image.png

    127.0.0.1 demo-springcloud-eureka-one
    127.0.0.1 demo-springcloud-eureka-two

    3.4 访问预览

    访问demo-springcloud-eureka-one可以看见 two已经注册进入


    image.png

    访问demo-springcloud-eureka-two可以看见 one已经注册进入

    image.png

    4. Eureka对应相关的配置

    相关直接查看文档即可。

    5. 使用服务注册到Eureka管理

    5.1 搭建一个用户Server,增加Eureka客户端依赖

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    

    5.2 增加配置文件

    spring:
      application:
        name: demo-springcloud-service-user
    eureka:
      client:
        service-url:
          defaultZone: http://user:user@demo-springcloud-eureka-one:8761/eureka/
      instance:
        prefer-ip-address: true
    server:
      port: 8899
    

    5.3 代码层增加配置

    //发现服务注册中心,将服务进行注册
    @EnableDiscoveryClient
    @SpringBootApplication
    public class DemoSpringcloudServiceUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoSpringcloudServiceUserApplication.class, args);
        }
    }
    

    5.4 效果预览

    访问: http://127.0.0.1:/8761

    image.png

    相关文章

      网友评论

          本文标题:【二】Spring Cloud Eureka

          本文链接:https://www.haomeiwen.com/subject/jbalpqtx.html