eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理
服務(wù)治理可以說是微服務(wù)架構(gòu)中最為核心和基礎(chǔ)的模塊,它主要用來實現(xiàn)各個微服務(wù)實例的自動化注冊和發(fā)現(xiàn)。
Spring Cloud Eureka是Spring Cloud Netflix 微服務(wù)套件的一部分,主要負(fù)責(zé)完成微服務(wù)架構(gòu)中的服務(wù)治理功能。
本文通過簡單的小例子來分享下如何通過Eureka進(jìn)行服務(wù)治理:
搭建服務(wù)注冊中心
注冊服務(wù)提供者
服務(wù)發(fā)現(xiàn)和消費
==========我是華麗的分割線========================
一、搭建服務(wù)注冊中心
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
創(chuàng)建maven工程:eureka(具體實現(xiàn)略)
修改pom文件,引入依賴
4.0.0
com.sam
eureka
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
1.8
org.springframework.cloud
spring-cloud-dependencies
Camden.SR6
pom
import
org.springframework.cloud
spring-cloud-starter-eureka-server
創(chuàng)建啟動類
/***
* @EnableEurekaServer
* 用來指定該項目為Eureka的服務(wù)注冊中心*/@EnableEurekaServer
@SpringBootApplicationpublic classEurekaApp {public static voidmain(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
配置application.properties文件
#設(shè)置tomcat服務(wù)端口號
server.port=1111#設(shè)置服務(wù)名稱
spring.application.name=eureka-service
eureka.instance.hostname=localhost
#注冊中心不需要注冊自己
eureka.client.register-with-eureka=false#注冊中心不需要去發(fā)現(xiàn)服務(wù)
eureka.client.fetch-registry=false#設(shè)置服務(wù)注冊中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
啟動服務(wù)并訪問,我們會看到這樣的畫面:
二、注冊服務(wù)提供者
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
創(chuàng)建maven工程:hello-service(具體實現(xiàn)略)
修改pom文件,引入依賴
4.0.0
com.sam
hello-service
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
1.8
org.springframework.cloud
spring-cloud-dependencies
Camden.SR6
pom
import
org.springframework.cloud
spring-cloud-starter-eureka
創(chuàng)建啟動類
/***
*
* @EnableDiscoveryClient
* 讓服務(wù)使用eureka服務(wù)器
* 實現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
**/@EnableDiscoveryClient
@SpringBootApplicationpublic classHelloApp {public static voidmain(String[] args) {
SpringApplication.run(HelloApp.class, args);
}
}
創(chuàng)建controller
@RestControllerpublic classHelloController {
Logger logger= LoggerFactory.getLogger(HelloController.class);
@Autowired
DiscoveryClient discoveryClient;
@RequestMapping("/hello")publicString hello() {
ServiceInstance instance=discoveryClient.getLocalServiceInstance();//打印服務(wù)的服務(wù)id
logger.info("*********" +instance.getServiceId());return "hello,this is hello-service";
}
}
配置application.properties文件
server.port=9090#設(shè)置服務(wù)名
spring.application.name=hello-service
#設(shè)置服務(wù)注冊中心的URL,本服務(wù)要向該服務(wù)注冊中心注冊自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
啟動并測試)啟動后再hello-service的控制臺會有這種字樣(xxx代表你的PC名)
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
eureka的控制臺會打印出如下字樣(xxx代表你的PC名)
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
)再次訪問localhost:1111,會發(fā)現(xiàn)有服務(wù)注冊到注冊中心了
三、服務(wù)發(fā)現(xiàn)和消費
完整目錄結(jié)構(gòu)如下:
搭建過程:
創(chuàng)建maven工程(具體實現(xiàn)略)
修改pom文件,引入依賴
4.0.0
com.sam
hello-consumer
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
1.8
org.springframework.cloud
spring-cloud-dependencies
Camden.SR6
pom
import
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-ribbon
這里比hello-service服務(wù)提供者,多了ribbon的依賴
創(chuàng)建啟動類
@EnableDiscoveryClient
@SpringBootApplicationpublic classConsumerApp {//@Bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean
@Bean
@LoadBalanced//@LoadBalanced實現(xiàn)負(fù)載均衡
publicRestTemplate restTemplate() {return newRestTemplate();
}public static voidmain(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
這里也要用到@EnableDiscoveryClient, 讓服務(wù)使用eureka服務(wù)器, 實現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
創(chuàng)建controller
@RestControllerpublic classConsumerController {//這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實例
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hello-consumer")publicString helloConsumer() {//調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port
restTemplate.getForObject("http://hello-service/hello", String.class);return "hello consumer finish !!!";
}
}
配置application.properties文件
server.port=9999spring.application.name=hello-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
#這里的配置項目和服務(wù)提供者h(yuǎn)ello-service一樣
啟動,測試)啟動eureka。為了展示負(fù)責(zé)均衡的效果,我們的hello-service啟動兩個服務(wù),啟動兩個服務(wù)的具體步驟如下
以上是hello-service1的啟動步驟,端口號為9090;同樣方法設(shè)置hello-service2,端口號為9091(具體實現(xiàn)略)。
)啟動hello-consumer
)再次訪問http://localhost:1111/,會發(fā)現(xiàn)有2個hello-service服務(wù)(端口號一個是9090,一個是9091),1個hello-consume服務(wù)
)? ?多次訪問http://localhost:9999/hello-consumer,會發(fā)現(xiàn)hello-service1和hello-service2會輪流被調(diào)用(已經(jīng)實現(xiàn)了負(fù)責(zé)均衡),可以通過兩者的控制臺打印內(nèi)容確認(rèn)(還記得我們在hello-service的controller中有個loggerlogger.info("*********" + instance.getServiceId());嗎?對,就是這個打印)
四、總結(jié)
以上實例實現(xiàn)了基本的服務(wù)治理:
通過spring-cloud-starter-eureka-server和@EnableEurekaServer實現(xiàn)服務(wù)注冊中心
通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注冊到服務(wù)注冊中心
通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用注冊中心并發(fā)現(xiàn)服務(wù),通過spring-cloud-starter-ribbon來實現(xiàn)負(fù)載均衡消費服務(wù)
PS:這里說明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我們使用spring進(jìn)行開發(fā),有興趣的朋友可以自行百度了解下。
總結(jié)
以上是生活随笔為你收集整理的eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java jtable 监听事件_【Ja
- 下一篇: java 格式化解析_java日期格式化