• SpringCloud 项目搭建(四):服务提供方搭建


    四、服务提供方搭建 
    1.在父项目上面新建模块myservice
    
     
    2.选择Spring Cloud Discovery—>Eureka Discovery Client
    
    
    3.Module Name一般不做修改,和项目名称Artifact一样
     
     
    4.将srcmain
    esources下面的application.properties改名为application.yml,修改文件编码方式为UTF-8,内容如下
    
    server:
      port: 1001
    spring:
      application:
        name: myservice
      profiles:
        active: default
      zipkin:
         enabled: true
         base-url: http://localhost:9411/
         locator:
           discovery:
             enabled: true
         sender:
           type: WEB
      sleuth:
         web:
           client:
             enabled: true
         sampler:
           # 默认的采样比率为0.1,不能看到所有请求数据
           # 更改采样比率为1,就能看到所有的请求数据了,但是这样会增加接口调用延迟
           probability: 1.0
    eureka:
      instance:
        lease-renewal-interval-in-seconds: 5      # 心跳时间,即服务续约间隔时间 (缺省为30s)
        lease-expiration-duration-in-seconds: 10  # 没有心跳的淘汰时间,10秒,即服务续约到期时间(缺省为90s)
        prefer-ip-address: true                   # 将IP注册到服务注册中心
      client:
        service-url:
          defaultZone: http://localhost:1024/eureka/
        fetch-registry: true # 向注册中心注册
        registry-fetch-interval-seconds: 5 # 服务清单的缓存更新时间,默认30秒一次
    
    
    5.打开srcmainjavacomlimyservice下面的MyserviceApplication.java
    
    在启动类上加入@EnableDiscoveryClient注解,声明该微服务注册到服务注册中心。
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class MyserviceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyserviceApplication.class, args);
        }
    
    }
    
    6.pom文件中增加依赖,并在Maven Projects里面刷新
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    7.srcmainjavacomlimyservice下面新建包controller,新建MyController.java
    
    @RestController
    public class MyController {
    
        @GetMapping("/GetTime")
        public String getTime(){
            return new Date().toString();
        }
    }
    
    8.打开浏览器,访问http://localhost:1001/getTime
    
    
  • 相关阅读:
    vs2010装虚拟机后启动慢
    LINUX FIFO(读书笔记,LINUX程序设计第四版)
    WPF控件 ProgressBar
    C# SQLite操作示列
    win10下使用Linux命令
    类初始化与实例化过程
    结构与类 Delphi
    多线程应用 Delphi
    (珍藏)SQL Server中常用的SQL语句
    获取当前用户本地应用程序设置文件夹 Delphi
  • 原文地址:https://www.cnblogs.com/liw66/p/12289988.html
Copyright © 2020-2023  润新知