zoukankan      html  css  js  c++  java
  • Spring Cloud(Dalston.SR5)--Eureka 服务提供者

    要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClient 注解来启动服务注册(必须在项目中实现了RESTful 服务)。

    • 创建项目

      创建名称为 service-provider 的 Spring Cloud 项目,修改 POM.xml 中增加以下依赖项:

      <?xmlversion="1.0"encoding="UTF-8"?>

      <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>

         

      <groupId>org.lixue.webservice</groupId>

      <artifactId>service-provider</artifactId>

      <version>1.0-SNAPSHOT</version>

      <parent>

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

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

      <version>1.5.4.RELEASE</version>

      <relativePath/><!--lookupparentfromrepository-->

      </parent>

         

      <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

      <java.version>1.8</java.version>

      <spring-cloud.version>Dalston.SR5</spring-cloud.version>

      </properties>

      <dependencies>

      <dependency>

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

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

      </dependency>

      <dependency>

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

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

      </dependency>

      <dependency>

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

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

      <scope>test</scope>

      </dependency>

      </dependencies>

      <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>

      </project>

         

    • 创建 REST 服务

      创建 HelloWorldController 类,用来提供 REST 服务器,代码如下:

      package org.lixue.webservices.services;

         

      import org.springframework.web.bind.annotation.RequestMapping;

      import org.springframework.web.bind.annotation.RequestMethod;

      import org.springframework.web.bind.annotation.RequestParam;

      impor torg.springframework.web.bind.annotation.RestController;

         

      @RestController

      publicclassHelloWorldController{

      @RequestMapping(method=RequestMethod.GET,name="speak")

      publicStringspeak(@RequestParam(value="body",required=false)Stringbody){

      if(body==null||body==""){

      return"helloworld";

      }

      return"speak"+body;

      }

      }

    • 启动类启用Eureka客户端

      在启动类中,增加 @EnableDiscoveryClient 注解,声明这是一个 Eureka 客户端,代码如下:

      package org.lixue.webservices.services;

         

      import org.springframework.boot.SpringApplication;

      import org.springframework.boot.autoconfigure.SpringBootApplication;

      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

         

      @SpringBootApplication

      @EnableDiscoveryClient

      publicclassServiceProviderApplication{

      publicstaticvoidmain(String[]args){

      SpringApplication.run(ServiceProviderApplication.class,args);

      }

      }

    • 基本配置

      在 src/main/resources 目录中,创建 application.yml 文件,该文件为 Spring Cloud 的配置文件,增加基本配置:

      #配置应用名称

      spring:

      application:

      name:helloworld-provider

      #服务端口

      server:

      port:8080

      #设置eureka服务注册中心的地址,如果多个以逗号分割

      eureka:

      client:

      service-url:

      #defaultZone表示默认的区域的eureka服务地址,多个使用逗号分割

      defaultZone:http://localhost:9000/eureka/

    • 启动项目

      运行项目,访问地址 http://localhost:9000 即可打开 Eureka 注册中心的服务控制台,在 instance currently registered with Eureka 中可以看到我们的服务提供者

  • 相关阅读:
    IOSUITextField类
    IOSUITableView设置背景图片,方式与众不同。
    IOS图标知识详情(转载自http://luoyl.info/blog/2012/03/iphoneipadicons/)
    IOSCreate UIActionSheet 'otherButtons' by passing in array
    Offset文件名(绝对偏移量)
    单例模式(Singleton)Holder
    在 spring 中一共定义了七种事务传播属性
    UML,各种关系合集
    Java,线程池,ThreadPoolExecutor
    EasyMock
  • 原文地址:https://www.cnblogs.com/li3807/p/8676363.html
Copyright © 2011-2022 走看看