Spring Cloud配置Eureka 服务器

Posted Q_Y_X

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud配置Eureka 服务器相关的知识,希望对你有一定的参考价值。

基础架构:

Eureka的基本功能:

eueka按逻辑上可以划分为3个模块,eureka-server,service-provider,service-consumer
eureka-server:服务端,提供服务注册和发现
eureka-client-service-provider:服务端,服务提供者,通过http rest告知服务端注册,更新,取消服务
eureka-client-service-consumer:客户端,服务消费者,通过http rest从服务端获取需要服务的地址列表,然后配合一些负载均衡策略(ribbon)来调用服务端服务。
值得注意的一点,不同于其他服务注册与发现(zookeeper需要单独以中间件的形式部署集群server),以上3个角色都是逻辑角色,甚至可以在相同的jvm进程上

注意:在该默认配置下,注册中心也会将自己作为客户端尝试注册自己,因此要禁用客户端注册行为
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

项目结构:

 

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>micronservice</groupId>
  <artifactId>micronservice</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <modules>
    <module>provider-user</module>
    <module>consumer-order</module>
  </modules>
  <!--将当前项目声明成一个spring boot项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
  </parent>
  <name>micronservice</name>
  <!-- FIXME change it to the project\'s website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.M9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <!--spring boot的日志依赖-->
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Eureka服务器的配置:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.qyx</groupId>
  <artifactId>eureka01</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>eureka01</name>
  <!-- FIXME change it to the project\'s website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <!--eurekaserver依赖-->
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      <version>1.4.3.RELEASE</version>
    </dependency>
    <!--安全依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

application.yml文件的配置

server:
  port: 10000
#因为当前的eureka是单机的,所以我们需要做一些配置
#注意:在该默认配置下,注册中心也会将自己作为客户端尝试注册自己,因此要禁用客户端注册行为
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
eureka:
  client:
    register-with-eureka: false #禁止自己当做服务注册
    fetch-registry: false   #屏蔽注册信息
    service-url:
      defaultZone: http://user:123@localhost:10000/eureka
#security:
#  basic:
#    enabled: true #开启安全配置,也就是需要密码,如果不需要设置为fasle即可,注意这个参数必须放在application.yml文件中,不允许放在bootstrap.yml
#  user:
#    password: 123
#    name: user #在配置了用户名和密码后我们可以修改地址的访问风格为 curl风格
package com.qyx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import javax.swing.*;

@EnableEurekaServer //通过@EnableEurekaServer注解启动一个服务注册中心简言之将当前项目标注为eurekaServer
@SpringBootApplication
public class EurekaApp {
    public static void  main(String[] args)
    {
        SpringApplication.run(EurekaApp.class);
    }
}

登录service-url.defaultZone: http://user:123@localhost:10000/eureka 里面配置的Eureka信息面板的地址


 

 

以上是关于Spring Cloud配置Eureka 服务器的主要内容,如果未能解决你的问题,请参考以下文章

spring-cloud: eureka之:ribbon负载均衡配置

Spring Cloud配置Eureka 服务器

spring-cloud配置eureka服务端

spring cloud 之 Eureka

spring-cloud: eureka之:ribbon负载均衡自定义配置

Spring Cloud微服务实战-服务治理(Spring Cloud Eureka)