zoukankan      html  css  js  c++  java
  • Spring Cloud入门程序

    本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用

    1、创建Spring Starter Project工程

    点击next,添加项目名

    2、引入Spring Cloud 的 Eureka

    点击next

    点击 finish

    3、配置项目的 application.properties

    #设置tomcat服务端口号
    server.port=1111
    #设置服务名称
    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

    注意:

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

    禁止方式如下:在application.properties配置文件中增加以下内容

    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
     
    否则会出现:
    com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

    或者com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

     4、创建启动类

    用Spring Boot创建一个服务类Springmvc011Application ,需要一个注解@EnableEurekaServer加在springboot工程的启动类上

    package com.hello;
    
    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 Springmvc011Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Springmvc011Application.class, args);
        }
    }

    5、启动服务并访问,我们会看到这样的画面:

    6、总结:

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

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

    本文通过简单的小例子来分享下如何通过Eureka进行服务治理:

    • 搭建服务注册中心(本文)
    • 注册服务提供者
    • 服务发现和消费

    Donate捐赠

    如果我的文章帮助了你,可以赞赏我 1 元给我支持,让我继续写出更好的内容)

       

      (微信)                                        (支付宝)

    微信/支付宝 扫一扫

  • 相关阅读:
    DAY9学习笔记
    DAY8课下作业
    DAY8学习笔记
    DAY7课下作业
    github clone 开源代码报Permission denied (publickey)
    grandle构建工程报:javax.net.ssl.SSLException
    centos安装redash
    centos安装nodejs
    CURL执行请求
    flume定制拦截器Interceptor
  • 原文地址:https://www.cnblogs.com/moonsoft/p/9306827.html
Copyright © 2011-2022 走看看