日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【微服务架构】SpringCloud之Eureka入门篇

發(fā)布時(shí)間:2023/11/28 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【微服务架构】SpringCloud之Eureka入门篇 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

什么是Eureka

官方的介紹在這里Eureka wiki。Eureka是Netflix開源的一個(gè)RESTful服務(wù),主要用于服務(wù)的注冊(cè)發(fā)現(xiàn)。Eureka由兩個(gè)組件組成:Eureka服務(wù)器和Eureka客戶端。Eureka服務(wù)器用作服務(wù)注冊(cè)服務(wù)器。Eureka客戶端是一個(gè)java客戶端,用來簡(jiǎn)化與服務(wù)器的交互、作為輪詢負(fù)載均衡器,并提供服務(wù)的故障切換支持。Netflix在其生產(chǎn)環(huán)境中使用的是另外的客戶端,它提供基于流量、資源利用率以及出錯(cuò)狀態(tài)的加權(quán)負(fù)載均衡。

在我看來,Eureka的吸引力來源于以下幾點(diǎn):

  1. 開源:大家可以對(duì)實(shí)現(xiàn)一探究竟,甚至修改源碼。
  2. 可靠:經(jīng)過Netflix多年的生產(chǎn)環(huán)境考驗(yàn),使用應(yīng)該比較靠譜
  3. 省心 功能齊全:不但提供了完整的注冊(cè)發(fā)現(xiàn)服務(wù),還有Ribbon等可以配合使用的服務(wù)。
  4. 基于Java:對(duì)于Java程序員來說,使用起來,心里比較有底。
  5. Spring Cloud可以使用Spring Cloud, 與Eureka進(jìn)行了很好的集成,使用起來非常方便。

項(xiàng)目架構(gòu)

代碼實(shí)現(xiàn)

1、實(shí)現(xiàn)服務(wù)注冊(cè),創(chuàng)建EureKaserver 項(xiàng)目

項(xiàng)目結(jié)構(gòu)

pom.xml文件

<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.fit</groupId><artifactId>EureKaserver</artifactId><version>0.0.1-SNAPSHOT</version><!-- SpringBoot父類依賴引用 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

application.yml配置

server:port: 8888
eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

EurekaServer.java文件

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {public static void main(String[] args) {SpringApplication.run(EurekaServer.class, args);}
}

服務(wù)注冊(cè)搭建完成,運(yùn)行訪問:http://localhost:8888/

2、服務(wù)提供者,創(chuàng)建SpringCloud-Service

項(xiàng)目結(jié)構(gòu)

pom.xml文件

<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>comf.it</groupId><artifactId>SpringCloud-Service</artifactId><version>0.0.1-SNAPSHOT</version><!-- SpringBoot父類依賴引用 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

application.yml配置

eureka:client:serviceUrl:defaultZone: http://localhost:8888/eureka/
server:port: 8762
spring:application:name: SpringCloud-Service

UserController.java

@RestController
public class UserController {@RequestMapping(value ="getUser")public Map<String,Object> getUser(){Map<String,Object> user = new HashMap<String,Object>();user.put("name", "zlzhaoe");user.put("age", "26");return user;}
}

SpringCloudServiceApp.java

@EnableEurekaClient
@SpringBootApplication
public class SpringCloudServiceApp {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudServiceApp.class, args);}
}

服務(wù)提供者SpringCloud-Service創(chuàng)建完成

訪問:http://localhost:8762/getUser

訪問:http://localhost:8888/

3、消費(fèi)者,創(chuàng)建SpringCloud-Consumer

項(xiàng)目結(jié)構(gòu)

pom.xml

<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.fit</groupId><artifactId>SpringCloud-Consumer</artifactId><version>0.0.1-SNAPSHOT</version><!-- SpringBoot父類依賴引用 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

application.yml

eureka:client:serviceUrl:defaultZone: http://localhost:8888/eureka/
server:port: 8764
spring:application:name: SpringCloud-Consumer

ConsumerUserController.java

@Controller
public class ConsumerUserController {@Autowiredprivate RestTemplate restTemplate;@SuppressWarnings("unchecked")@ResponseBody@RequestMapping(value ="getServiceUser")public Map<String,Object> getServiceUser(){System.out.print("==========start");Map<String,Object> user = restTemplate.getForObject("http://SpringCloud-Service/getUser", Map.class);return user;}
}

SpringCloudConsumerApp.java

package com.fit;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConsumerApp {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApp.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}
}

*備注:在工程的啟動(dòng)類中,通過@EnableDiscoveryClient向服務(wù)中心注冊(cè);并且向程序的ioc注入一個(gè)bean: restTemplate;并通過@LoadBalanced注解表明這個(gè)restRemplate開啟負(fù)載均衡的功能*

消費(fèi)者,創(chuàng)建完成,運(yùn)行訪問http://localhost:8764/getServiceUser,返回如下圖

源碼下載

總結(jié)

以上是生活随笔為你收集整理的【微服务架构】SpringCloud之Eureka入门篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。