当前位置:   article > 正文

springcloud ( Greenwich.SR4 ) 注册中心eureka_spring-cloud-starter-eureka-server greenwich.sr4

spring-cloud-starter-eureka-server greenwich.sr4
这里使用springboot 2.1.11.RELEASE springcloud Greenwich.SR4

一、单节点配置

  1. 新建springboot项目,添加maven依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  1. 启动类添加注解 @EnableEurekaServer,表示开启注册中心
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaApplication.class, args);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. 配置 application.yml 文件,这里需要注意一个点,如果是单节点,需要配置 register-with-eurekafetch-registryfalse ,不让他自己注册到注册中心上去,默认为真,双节点和集群环境需要配置为true,生产环境必须配置为集群
spring:
      application:
        name: eureka-one
      security:
        user:
          name: admin
          password: admin
server:
  port: 8000



#===========eureka config start =============
eureka:
  instance:
    hostname: localhost
  client:
    #表示是否将自己注册到Eureka Server,默认为true。
    register-with-eureka: false
    #表示是否从Eureka Server获取注册信息,默认为true。
    fetch-registry: false
    #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
    # 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
    server:
        waitTimeInMsWhenSyncEmpty: 0

#===========eureka config end  =============
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  1. 添加 security 验证,访问euraka 需要使用密码,

    1. 首先添加依赖
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 其次配置登录用户和 登录密码 ,defaultZone也要修改
    
    security:
            user:
              name: admin    登录用户
              password: admin  登录密码
    
    defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 禁止csrf验证
    @Configuration
    class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    //    @Override
    //    public void configure(AuthenticationManagerBuilder auth) throws Exception {
    //        auth.inMemoryAuthentication()
    //                .passwordEncoder(NoOpPasswordEncoder.getInstance())
    //                .withUser("admin").password("admin")
    //                .authorities("ADMIN");
    //    }
    //
    //    @Override
    //    protected void configure(HttpSecurity http) throws Exception {
    //        http
    //                .csrf()
    //                .disable()
    //                .authorizeRequests()
    //                .anyRequest().authenticated()
    //                .and()
    //                .httpBasic();
    //    }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().ignoringAntMatchers("/eureka/**");
            super.configure(http);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    1. 启动项目,访问 localhost:8000/
      在这里插入图片描述
双节点配置
  1. 修改application.yml文件,添加复制上一段,使用 profiles: peer1和 profiles: peer2 启动,
    第一个是修改需要配置 register-with-eureka 和 fetch-registry 为true ,让俩个服务相互注册,
    第二个是修改defaultZone,添加指向对方的地址

---
spring:
     profiles: peer1
     application:
       name: spring-cloud-eureka
     security:
       user:
         name: admin
         password: admin
server:
 port: 8001

#===========eureka config start =============
eureka:
 instance:
   hostname: peer1
 client:
   #表示是否将自己注册到Eureka Server,默认为true。双注册中心设置为true
   register-with-eureka: true
   #表示是否从Eureka Server获取注册信息,默认为true。双注册中心设置为true
   fetch-registry: true
   #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
   # 默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
   service-url:
     defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer2:8002/eureka/

#===========eureka config end  =============



---
spring:
     profiles: peer2
     application:
       name: spring-cloud-eureka
     security:
       user:
         name: admin
         password: admin
server:
 port: 8002

#===========eureka config start =============
eureka:
 instance:
   hostname: peer2
 client:
   register-with-eureka: true
   fetch-registry: true
   service-url:
     defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer1:8001/eureka/

#===========eureka config end  =============
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  1. 是在机器host文件上添加域名映射
127.0.0.1 peer1  
127.0.0.1 peer2  
  • 1
  • 2
  1. 分别启动test目录下两个启动类也可以
 com.xuyy.SpringCloudEurekaApplicationPeer1
 com.xuyy.SpringCloudEurekaApplicationPeer2
  • 1
  • 2
  1. 生产环境部署时打包启动依次执行下面命令
java -jar spring-cloud-eureka-two-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-two-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
  • 1
  • 2
三、三节点集群配置
1. 和双节点一样,只需要在启动一个相同实例,需要修改两处
2. 第一是 同样配置peer3
3. 第二是,修改defaultZone,添加分别指向其他两处的地址
  • 1
  • 2
  • 3
四、代码地址
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/315329?site
推荐阅读
相关标签