zoukankan      html  css  js  c++  java
  • SpringCloud 微服务 (服务治理 Spring Cloud Eureka)

     服务治理只要负责微服务架构中最为核心的基础的模块,它主要用来实现各个微服务实例的自动注册与发现。

     搭建服务注册中心

     1.首先创建一个基础的Spring boot 工程,命名为eureka-server,并在pom.xml中引入必要依赖

           <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.4.3.RELEASE</version>
    		<relativePath />
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<java.version>1.7</java.version>
    	</properties>
    	
    	<!-- 引入spring boot依赖 -->
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
           	 <!--增加eureka-server的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
          
    	</dependencies>
    	
    	<!-- 引入spring cloud 依赖 -->
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>Brixton.SR5</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    

      

     2.@EnableEurekaServer注解启动一个微服务注册中心

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaApplication {
    	
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaApplication.class, args);
    	}
    }
    

    3.在默认配置下,服务注册中心也会将自己作为客户端来注册它自己,所以我们需要禁用它的客户端注册行为

    server.port=1111
    #是否将自己注册到eureka server
    eureka.client.registerWithEureka=false 
    #是否从eureka server获取注册信息
    eureka.client.fetchRegistry=false	 
    #设置与enreka server交互地址,查询服务和注册服务 多个地址,分割    
    eureka.client.serviceUrl.defaultZone=http://192.168.30.1:8761/eureka
    

      

    服务提供者     

     1.首先创建一个基础的Spring boot 工程,命名为eureka-provider,并在pom.xml中引入必要依赖

    <!-- 引入spring boot依赖 -->
    <dependencies>
      <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
      </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
            
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
       </dependency>
            
    
    </dependencies>
    	
    <!-- 引入spring cloud 依赖 -->
    <dependencyManagement>
      <dependencies>
    	<dependency>
    	  <groupId>org.springframework.cloud</groupId>
    	  <artifactId>spring-cloud-dependencies</artifactId>
    	  <version>Brixton.SR5</version>
    	  <type>pom</type>
    	  <scope>import</scope>
        </dependency>
    </dependencyManagement>
    

      

    2.创建Controller 

    @GetMapping("/{id}")
    public User findByid(@PathVariable Long id){	
    	User user = userRepository.findOne(id);
    	return user;	
    }

    3.@EnableDiscoveryClient  激活@EnableDiscoveryClient实现

    @EnableDiscoveryClient
    @SpringBootApplication
    public class UserApplication {
    	
    	public static void main(String[] args) {
    		SpringApplication.run(UserApplication.class, args);
    	}
    }
    

    4.注定配置服务名,指定服务中心地址

    server.port=9999
    server.ssl.enabled=false
    
    spring.application.name=microservice-provider-user
    #这可以配置多个, 逗号分割 eureka.client.serviceUrl.defaultZone=http://192.168.137.100:1111/eureka/

    高可用eureka

    修改我们之前创建的eureka工程 ,新建两个配置application-peer1.properties,application-peer1.properties

    server.port=1111
    spring.application.name=eureka-server
    eureka.instance.hostname=peer1
    eureka.client.serviceUrl.defaultZone=http://192.168.137.101:1112/eureka
    eureka.server.enable-self-preservation=false
    
    server.port=1112
    spring.application.name=eureka-server
    eureka.instance.hostname=peer2
    eureka.client.serviceUrl.defaultZone=http://192.168.137.100:1111/eureka
    eureka.server.enable-self-preservation=false
    

    启动时指定配置文件启动

    nohup java -jar discovery-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 &

    nohup java -jar discovery-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 &

    这样两个eureka就能互相注册,形成一个双节点集群

    eureka自我保护模式

    当eureka Server 在一段时间内没有接受到某个微服务实例的心跳,Eureka Server 将会注销掉该实例(默认90秒)

    当网络分区发生故障时,丢失过多客户端,那么这个节点就会进入自我保护模式,一旦进入保护模式,Eureka Server 就会保护服务注册中心的信息,不在注销

    使用eureka.server.enable-self-preservation=false 禁用自我保护模式

  • 相关阅读:
    Day12:前端代码的合并和登录过程的完善
    Day11:基本界面的完成
    Day10:界面的跳转
    Day9:尝试连接绑定数据与API
    Day8:转战前端开发
    Day7:熟悉搭建服务器工具及配置
    Day6:完成API的列举设计
    day14
    day13
    day13
  • 原文地址:https://www.cnblogs.com/zls1218/p/8991106.html
Copyright © 2011-2022 走看看