1、POM配置

  和普通Spring Boot工程相比,仅仅添加了Eureka、Spring Boot Starter Actuator依赖和Spring Cloud依赖管理

<dependencies>
  <!--添加Eureka Server依赖-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <!--服务健康检测,务必添加,不然负载均衡消费此服务时,断路器会打开-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <!--Spring Cloud依赖版本管理-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

02、使能Eureka Client

@SpringBootApplication
@EnableEurekaClient//使能Eureka Server服务
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

03、src/main/resources工程配置文件application.yml

server:
  port: 2001  #默认启动端口
spring:
  application:
    name: hello-service-provider #应用名

eureka:   instance:     hostname: hello-service-provider  #主机名
  client:
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/ #服务注册中心地址。若部署在其它IP主机,请将localhost改为主机IP

04、提供服务

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        System.out.println("Time" + System.currentTimeMillis());
        return "Hello Spring Cloud";
    }
}

  服务消费者通过http://hello-service-provider/hello即可消费此服务。

posted on 2017-08-02 12:55  TBBS  阅读(151)  评论(0编辑  收藏  举报