美文网首页
spring cloud eureka 服务注册中心

spring cloud eureka 服务注册中心

作者: SoLix | 来源:发表于2018-07-18 21:45 被阅读0次

    eureka server 注册中心 demo

    搭建 maven 工程,删除 src 等文件,只留 pom文件,再在该项目中 创建 module ,选择 initializr 创建 spring boot 工程 ,选择 web  中web组件、cloud discovery 中Eureka  server 组件 :

    pom 中 maven 声明如下:

    <dependency>

        <groupId> org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

      <groupId> org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

    </dependency>

    根据创建好的项目修改eureka-server 配置文件类型 application.yml 后缀(官网都是yml 配置样例),拷贝样例修改端口,设置application name:

    server:

      port: 6000

    eureka:

      instance:

        hostname: eureka-server

      client:

        registerWithEureka: false

        fetchRegistry: false

        serviceUrl:

          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    spring:

      application:

        name: eureka-server

    在 EurekaServerApplication 中添加eureka server 注解来启动eureka 服务:

    @SpringBootApplication

    @EnableEurekaServer

    public class EurekaServerApplication {

        public static void main(String[] args) {

            SpringApplication.run(EurekaServerApplication.class, args);

        }

    }

    启动服务查看浏览器页面效果:

    完成第一步操作。

    第二步: 创建 provider-server 服务提供者,去 eureka-server 中注册服务,同样选择spring boot 创建工程 provider-server ,这次选择web 中web 组件和cloud discovery 中Eureka  Discovery:

    pom 中maven 声明如下:

      <dependency>

        <groupId> org.springframework.boot</groupId>

      <artifactId> spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

        <groupId> org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

    </dependency>

    修改 provider-server 配置文件后缀 改成yml 并配置如下:

    server:

      port: 7700

    spring:

      application:

        name: provider-server

    eureka:

      instance:

        hostname: provider-server

        preferIpAddress: true

      client:

        serviceUrl:

          defaultZone: http://localhost:6000/eureka/

    配置 ProviderServerApplication 中注解:

    @SpringBootApplication

    @EnableEurekaClient

    @EnableDiscoveryClient

    public class ProviderServerApplication {

        public static void main(String[] args) {

            SpringApplication.run(ProviderServerApplication.class, args);

        }

    }

    在yml 的配置中添加了 preferIpAddress :true 这个的作用是提供本地服务的的ip 给eureka-server,这个在feign 调用中起到作用,这个在之后的feign调用服务中没有添加导致调用失败,服务调用过程中调用http://provider-server/hello 这样,是根据服务名调用,这样导致找不到具体服务提供地址,导致调用失败。加上这个配置我们在点击eureka-server页面上的:

    时,在浏览器上出现的是地址,否则出现的是服务名(这个地址会报错,因为没有加actuator 组件,但是eureka-server 访问actuator/info 显示正常,在eureka中包括了actuator ):

    启动项目 provider-server ,刷新浏览器页面:

    注册成功。

    其中在 provider-server 中yml配置的application name 就是服务名称,就是eureka-server 页面中的Application ,启动两个服务效果:

    设置 provider-server 7710 ,main class 为 provider-server application全路径,parogram arguments 为 --server.port=7710 ,启动7710 端口服务,查看eurek-server 页面效果:

    这就注册上两个服务提供者。

    相关文章

      网友评论

          本文标题:spring cloud eureka 服务注册中心

          本文链接:https://www.haomeiwen.com/subject/lolapftx.html