当前位置: 首页 > news >正文

Eureka微服务实战-服务提供者

1.启动主java文件 DiscoveryApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //发现注册中心的服务
public class DiscoveryApplication {

    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }
}

复制代码

2.服务提供控制器 HelloController.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class HelloController {

    private final Logger log = LoggerFactory.getLogger(HelloController.class);


    @Autowired
    private DiscoveryClient client;//eureka服务发现接口

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){

        List<ServiceInstance> instances = client.getInstances("hello-server");
        for (ServiceInstance instance:instances  ) {
            log.info("/hello"+instance.getHost() + "---server---"+instance.getServiceId());
        }
        return "DiscoveryApplication";
    }

}
复制代码

3.服务提供核心配置文件

spring:
    application:
        name: hello-server

server:
    port: 9000
eureka:
    instance:
        lease-expiration-duration-in-seconds: 30 #参数用于定于服务时效时间
        lease-renewal-interval-in-seconds: 10  #用户定义与注册中心续约时间
        hostname: localhost
    client:
        service-url:
            defaultZone: http://peer1:8761/eureka/ #注册中心地址
        enabled: true
        register-with-eureka: true #向注册中心注册此服务
复制代码

4.依赖pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hnair</groupId>
    <artifactId>discovery</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>discovery</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

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

复制代码

访问地址:http://localhost:9000/hello

相关文章:

  • Python入门教程之安装MyEclipse插件和安装Python环境
  • Windows系统下类UNIX环境---Cygwin安装
  • MySQL Desc指令相关
  • 一个优秀的软件测试工程师需具备的技能
  • Mozilla正在SpiderMonkey中测试JavaScript并行计算
  • [剑指offer] 二叉树的镜像
  • PHP下用B/S编程模式去实现C/S软件编程模式下的插件引擎功能!
  • Vue创建五:导出及配置文件解析
  • 监测谁用了SQL Server的Tempdb空间
  • JavaScript面向对象程序设计(7): 闭包
  • Java NIO系列教程(十二) Java NIO与IO
  • Session,cookie,localStorage
  • Android拓展系列(12)--使用Gradle发布aar项目到JCenter仓库
  • VsCode编辑器
  • jQuery中的Ajax----05
  • angular学习第一篇-----环境搭建
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Debian下无root权限使用Python访问Oracle
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • js数组之filter
  • Protobuf3语言指南
  • Python_OOP
  • redis学习笔记(三):列表、集合、有序集合
  • Vue.js 移动端适配之 vw 解决方案
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 观察者模式实现非直接耦合
  • 快速构建spring-cloud+sleuth+rabbit+ zipkin+es+kibana+grafana日志跟踪平台
  • 学习笔记TF060:图像语音结合,看图说话
  • 一起参Ember.js讨论、问答社区。
  • Python 之网络式编程
  • UI设计初学者应该如何入门?
  • ​低代码平台的核心价值与优势
  • !!Dom4j 学习笔记
  • (145)光线追踪距离场柔和阴影
  • (3)(3.5) 遥测无线电区域条例
  • (NSDate) 时间 (time )比较
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (离散数学)逻辑连接词
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (七)Knockout 创建自定义绑定
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)jQuery 基础
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .“空心村”成因分析及解决对策122344
  • .net core 连接数据库,通过数据库生成Modell
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .net访问oracle数据库性能问题
  • .NET开源项目介绍及资源推荐:数据持久层
  • .Net面试题4
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复