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