• Spring Cloud Eureka作为注册中心


                             spring cloud 入门系列一:使用Eureka作为注册中心
    

    服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。

    Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分,主要负责完成微服务架构中的服务治理功能。

    本文通过简单的小例子来分享下如何通过Eureka进行服务治理
    一、搭建服务注册中心
    使用idea是New->Projiect->Spring Initializr->
    next
    在这里插入图片描述
    文件名及文件路径最后点击完成
    列出目录结构
    搭建过程如下:

    1.创建maven工程:eureka(具体实现略)
    2.修改pom文件,引入依赖`

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.yd.cloud</groupId>
        <artifactId>eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- 引入eureka server依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <!-- 使用dependencyManagement进行版本管理 -->
        <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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
        </repositories>
    
    </project>
    

    3.创建启动类

    package com.yd.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    /**
     *
     * @EnableEurekaServer
     * 用来指定该项目为Eureka的服务注册中心
     */
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
    
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }
    

    4.配置application.properties文件

    #设置tomcat服务端口号
    server.port=9901
    #设置服务名称
    spring.application.name=eureka-service
    
    eureka.instance.hostname=localhost
    #注册中心不需要注册自己
    eureka.client.register-with-eureka=false
    #注册中心不需要去发现服务
    eureka.client.fetch-registry=false
    #设置服务注册中心的URL
    eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
    

    5.或者使用application.yml文件

    server:
      port: 9901
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    

    6.启动服务并访问,我们会看到这样的画面
    在这里插入图片描述
    GitHub地址

  • 相关阅读:
    poj1321(棋盘问题)
    poj3009(Curling 2.0)
    站点 1访问非本站点下面的web.config文件需要的权限
    VS2010 Web网站发布详解
    服务禁止旁注,安全设置
    在Windows2012下安装SQL Server 2005无法启动服务的解决办法
    Server-U与IIS端口占用问题解决
    Server-U_详细配置
    创建维护计划时,提示“代理XP”组件已作为此服务器安全配置的一部分被关闭
    SQL SERVER 2005如何建立自动备份的维护计划
  • 原文地址:https://www.cnblogs.com/szls-666/p/12494225.html
Copyright © 2020-2023  润新知