怎么在spring cloud中使用Eureka 實(shí)現(xiàn)服務(wù)治理?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)專注骨干網(wǎng)絡(luò)服務(wù)器租用10余年,服務(wù)更有保障!服務(wù)器租用,服務(wù)器托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。
一、搭建服務(wù)注冊中心
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
1.創(chuàng)建maven工程:eureka(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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.sam</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <!-- 使用dependencyManagement進(jìn)行版本管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka server依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
3.創(chuàng)建啟動(dòng)類
/** * * @EnableEurekaServer * 用來指定該項(xiàng)目為Eureka的服務(wù)注冊中心 */ @EnableEurekaServer @SpringBootApplication public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
4.配置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
5.啟動(dòng)服務(wù)并訪問,我們會(huì)看到這樣的畫面:
二、注冊服務(wù)提供者
先列出完整目錄結(jié)構(gòu):
搭建過程如下:
1.創(chuàng)建maven工程:hello-service(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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.sam</groupId> <artifactId>hello-service</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
3.創(chuàng)建啟動(dòng)類
/*** * * @EnableDiscoveryClient * 讓服務(wù)使用eureka服務(wù)器 * 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn) * */ @EnableDiscoveryClient @SpringBootApplication public class HelloApp { public static void main(String[] args) { SpringApplication.run(HelloApp.class, args); } }
4.創(chuàng)建controller
@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); //打印服務(wù)的服務(wù)id logger.info("*********" + instance.getServiceId()); return "hello,this is hello-service"; } }
5.配置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
6.啟動(dòng)并測試
1.)啟動(dòng)后再hello-service的控制臺(tái)會(huì)有這種字樣(xxx代表你的PC名)
復(fù)制代碼 代碼如下:
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
eureka的控制臺(tái)會(huì)打印出如下字樣(xxx代表你的PC名)
復(fù)制代碼 代碼如下:
Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
2.)再次訪問localhost:1111,會(huì)發(fā)現(xiàn)有服務(wù)注冊到注冊中心了
三、服務(wù)發(fā)現(xiàn)和消費(fèi)
完整目錄結(jié)構(gòu)如下:
搭建過程:
1.創(chuàng)建maven工程(具體實(shí)現(xiàn)略)
2.修改pom文件,引入依賴
<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.sam</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 引入ribbon 依賴 ,用來實(shí)現(xiàn)負(fù)載均衡,我們這里只是使用,先不作其他介紹--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>
這里比hello-service服務(wù)提供者,多了ribbon的依賴
3.創(chuàng)建啟動(dòng)類
@EnableDiscoveryClient @SpringBootApplication public class ConsumerApp { //@Bean 應(yīng)用在方法上,用來將方法返回值設(shè)為為bean @Bean @LoadBalanced //@LoadBalanced實(shí)現(xiàn)負(fù)載均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
這里也要用到@EnableDiscoveryClient, 讓服務(wù)使用eureka服務(wù)器, 實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
4.創(chuàng)建controller
@RestController public class ConsumerController { //這里注入的restTemplate就是在com.sam.ConsumerApp中通過@Bean配置的實(shí)例 @Autowired RestTemplate restTemplate; @RequestMapping("/hello-consumer") public String helloConsumer() { //調(diào)用hello-service服務(wù),注意這里用的是服務(wù)名,而不是具體的ip+port restTemplate.getForObject("http://hello-service/hello", String.class); return "hello consumer finish !!!"; } }
5.配置application.properties文件
server.port=9999 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka #這里的配置項(xiàng)目和服務(wù)提供者h(yuǎn)ello-service一樣
6.啟動(dòng),測試1.)啟動(dòng)eureka。為了展示負(fù)責(zé)均衡的效果,我們的hello-service啟動(dòng)兩個(gè)服務(wù),啟動(dòng)兩個(gè)服務(wù)的具體步驟如下
以上是hello-service1的啟動(dòng)步驟,端口號為9090;同樣方法設(shè)置hello-service2,端口號為9091(具體實(shí)現(xiàn)略)。
2.)啟動(dòng)hello-consumer
3.)再次訪問http://localhost:1111/,會(huì)發(fā)現(xiàn)有2個(gè)hello-service服務(wù)(端口號一個(gè)是9090,一個(gè)是9091),1個(gè)hello-consume服務(wù)
4.) 多次訪問http://localhost:9999/hello-consumer,會(huì)發(fā)現(xiàn)hello-service1和hello-service2會(huì)輪流被調(diào)用(已經(jīng)實(shí)現(xiàn)了負(fù)責(zé)均衡),可以通過兩者的控制臺(tái)打印內(nèi)容確認(rèn)(還記得我們在hello-service的controller中有個(gè)loggerlogger.info("*********" + instance.getServiceId());嗎?對,就是這個(gè)打?。?/p>
關(guān)于怎么在spring cloud中使用Eureka 實(shí)現(xiàn)服務(wù)治理問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)站題目:怎么在springcloud中使用Eureka實(shí)現(xiàn)服務(wù)治理
URL鏈接:http://jinyejixie.com/article40/pocoeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站排名、全網(wǎng)營銷推廣、ChatGPT、小程序開發(fā)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)