zoukankan      html  css  js  c++  java
  • Spring Cloud微服务的服务治理组件eureka(一)

    目录

    1. 背景

    2. 注册中心项目搭建

    3. 高可用集群

    一、背景

       Spring Cloud Eureka是Spring Cloud Netflix的微服务套件一部分,基于Netflix Eureka做了二次封装,增加了Spring Boot风格的自动化配置,通过简单增加依赖、配置和注解就能与Eureka服务治理框架进行整合。

       在微服务中扮演服务治理的角色。

    二、注册中心项目搭建

       开发工具:idea

    1、File---> New ---> Project---->spring Initialzr

     

    2、填写项目包名、项目名,继续NEXT

    3、选中Eureka Server,Next-->(选择项目目录)Finish

     4、项目生成后目录结构如下,其中config为新建的包,其他文件及目录是默认生成的

     5、我们来看看pom.xml

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

    实际上我们使用注册中心服务时通常加上安全组件

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

    加入security后登录注册中心是需要用户名密码(用户名密码在后面会提到)

     

     

    6、在来看看application.properities

    server.port=1111
    spring.application.name=eureka-sever
    eureka.instance.hostname=localhost
    # 代表不向注册中心注册自己
    eureka.client.register-with-eureka=false
    # 注册中心就是维护服务实例的,不需要去检索服务
    eureka.client.fetch-registry=false
    #域路径指向其他服务注册中心(1112)的路径实现高可用 eureka.client.serviceUrl.defaultZone=http://admin:123456@${eureka.instance.hostname}:1112/eureka/ #security模块的用户名和密码,连接这个注册中心需要此用户名,密码如上行中的(admin:123456@)

    spring.security.user.name=admin spring.security.user.password=123456 #自我保护模式关闭 eureka.server.enableSelfPreservation=false #IP进行注册 eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} eureka.instance.preferIpAddress=true # 指定不同的环境 spring.profiles.active=master

     

    7、EurekaSeverApplication.java,加上注解EnableEurekaServer,自动化配置为注册中心

    1 @EnableEurekaServer
    2 @SpringBootApplication
    3 public class EurekaSeverApplication {
    4 
    5     public static void main(String[] args) {
    6         SpringApplication.run(EurekaSeverApplication.class, args);
    7     }
    8 
    9 }

    8、config包下WebSecurityConfig 安全配置

     1 @Configuration
     2 @EnableWebSecurity
     3 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
     4 
     5     @Override
     6     protected void configure(HttpSecurity http) throws Exception {
     7         http.csrf().disable();
     8         super.configure(http);
     9     }
    10 }

    9、若引入安全模块和安全配置,访问localhost:1111页面如下

     

     输入用户名,密码admin:123456为自己配置的用户名密码,登录后页面如下,注册中心无instances,还未有服务注册在此注册中心上

     

     

     

    三、高可用集群

        在生产环境中必须搭建一个集群来保证高可用。集群的搭建需要在Eureka 在配置中指定其他多个 Eureka 的地址。

    eureka.client.serviceUrl.defaultZone=http://用户名1:密码1 @localhost:1111/eureka/,http://用户名2:密码2 @localhost:1112/eureka/,http://用户名3:密码3 @localhost:1113/eureka/

         至此,高可用注册中心就搭建完了。

      比学习更重要的是培养学习的能力,比勤奋更重要的是找对方法,分析问题,解决问题。

    <<  返回页首  >>

     

     

  • 相关阅读:
    webserver服务器
    轮子哥的网站地址
    构建javaweb服务的docker镜像和容器化启动服务
    spring-cloud-alibaba-gateway
    markdown-记笔记常用语法
    java-生成公私钥命令
    阿里云oss文件服务器
    mybatis-mapper.xml一个标签支持执行多个sql语句
    docker安装nacos
    spring-cloud-alibaba-nacos
  • 原文地址:https://www.cnblogs.com/flyPenguinblog/p/13261124.html
Copyright © 2011-2022 走看看