博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一篇: 服务的注册与发现Eureka(Greenwich版)
阅读量:4180 次
发布时间:2019-05-26

本文共 6324 字,大约阅读时间需要 21 分钟。

一、创建服务注册中心

新建个主工程(这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom)并删除掉src目录

父pom.xml代码如下

4.0.0
com.example
chapter1
0.0.1-SNAPSHOT
jar
chapter1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
eureka-server
eureka-client
UTF-8
UTF-8
1.8
Greenwich.M1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

 

 

二、创建eureka-server子工程(右键父工程 new module)

eureka-server pom.xml代码如下

4.0.0
eureka-server
0.0.1-SNAPSHOT
jar
eureka-server
Demo project for Spring Boot
com.example
chapter2
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

application.properties配置文件:

#服务启动端口server.port=8001#主机地址eureka.instance.hostname=localhost#这两个false表明这个服务为eureka servereureka.client.fetch-registry=falseeureka.client.register-with-eureka=false#服务地址eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/#服务名称spring.application.name=eureka-server

EurekaServerApplication 启动类:

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/** * @author xiaobu */@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}

输入界面如下所示:

三、在父工程右键新建一个eureka-client模块 选择 eureka-discovery

对应的pom.xml如下所示

4.0.0
eureka-client
0.0.1-SNAPSHOT
jar
eureka-client
eureka-client for Spring Boot
com.example
chapter1
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

application.properties配置如下:

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/server.port=8002spring.application.name=eureka-client
启动类EurekaClientApplication配置如下:
package com.example;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu */@EnableEurekaClient@RestController@SpringBootApplicationpublic class EurekaClientApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaClientApplication.class, args);    }    @Value("${server.port}")    String port;    @RequestMapping("/test")    public String test(@RequestParam(value = "name", defaultValue = "admin") String name) {        return name +"test ,i am from port:" + port;    }}

启动eureka-client刷新http://localhost:8001 效果如下所示:

输入 效果图如下:

启动多个实例以及自动编译(修改端口,取消热编译)

转载地址:http://ougai.baihongyu.com/

你可能感兴趣的文章
明明程序员很累,为什么还有这么多人想入行?
查看>>
国家电网是“围城”?辞职吗?
查看>>
长沙,牛逼啊!
查看>>
卧槽!这 TM 才是真正的老司机看片神器!!!
查看>>
我差点信了......
查看>>
输入百度网址后,我发现了一个秘密...
查看>>
卧槽,被盗号了!!!
查看>>
TCP ,丫的终于来了!!
查看>>
你删除过的所有小黄片,它都能轻易找到
查看>>
小米低调上线的良心APP,永久免费真心好用!
查看>>
最新款笔记本真香,包邮送一台!
查看>>
微信小程序商城项目(Java版),拿去毕设又节省2千块
查看>>
学计算机的女生都怎么样了?
查看>>
我去蔚来试车了。。。
查看>>
福利,PyTorch中文版官方教程来啦(附下载)
查看>>
硬核分享!靠这个技术过了阿里二面
查看>>
一把小刀,直插 class 文件的小心脏
查看>>
进大厂,稳了!
查看>>
暴跌30%,心态崩了
查看>>
卧槽,我的小破站爆了!
查看>>