亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

一窺Spring Cloud Eureka

標簽:
Java

在Spring Cloud中Eureka负责服务发现功能。服务发现需要解决如何找到服务提供者在网络中位置的问题。

服务端

在Spring Tool Suite的文件菜单中,点击新建Spring Starter Project。
5ba67fe500016e0d05530740.jpg

然后在入口方法加上@EnableEurekaServer标记。

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class SpringcloudEurekaServerApplication {    public static void main(String[] args) {
        SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
    }
}

接着,在application.properties文件中增加如下配置:

server.port=8765eureka.instance.hostname: localhost
eureka.client.registerWithEureka: falseeureka.client.fetchRegistry: falseeureka.client.serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

如果能够顺利启动此程序的话,可以在http://localhost:8765地址中看到下列页面:
5ba67fe60001aa9314710959.jpg

客户端

新建另一个Spring Starter Project。
5ba67fe800011d9405530740.jpg

在入口方法加上@EnableDiscoveryClient标记。

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient@RestController@SpringBootApplicationpublic class SpringcloudEurekaClientApplication {    public static void main(String[] args) {
        SpringApplication.run(SpringcloudEurekaClientApplication.class, args);
    }    
    @RequestMapping("/hello")    public String home() {        return "Hello world";
    }
}

配置文件里添加服务端的地址

server.port=8760spring.application.name: springcloud-eureka-client
eureka.client.serviceUrl.defaultZone: http://localhost:8765/eureka/

启动客户端后,刷新服务端页面,可以看到该客户端服务已被服务端发现并注册。
5ba67fe80001bdd814670952.jpg

原文出处:https://www.cnblogs.com/kenwoo/p/9689019.html  

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消