Eureka项目搭建、数据请求
Eureka简介
Eureka采用的是Server/Client的模式进行设计。
Server扮演了服务注册中心的角色,为Client提供服务注册和发现的功能,维护着注册到自身的Client的相关信息,同时提供接口给Client获取到注册表中其他服务的信息。
Client将有关自己的服务的信息通过一定的方式登记到Server上,并在正常范围内维护自己信息的一致性,方便其他服务发现自己,同时可以通过Server获取到自己的依赖的其他服务信息,从而完成服务调用。
Eureka功能主要包括:
服务注册
、
服务续约
、
服务剔除
、
服务下线
、
获取注册表信息
、
远程调用
等。
- 以下是Eureka几个角色的解释: -
Eureka服务端
:负责服务注册、发现并管理每项服务的中心。-Eureka实例
:服务(如订单系统)部署多个服务器,每个服务器上提供的服务都是实例。-Eureka服务
:指提供特定服务功能的服务,例如:订单系统,同一服务可以提供多个实例;-Eureka客户端
:主要向服务中心注册自己成为服务。但它既可以是服务提供者,也可以是消费者。它与Eureka实例感觉相似,但实际上意义不同。
Eureka项目创建
1、新建Maven项目
2、只保留Maven项目的依赖文件
pom.xml
文件作为后续创建的子模块依赖的父依赖文件
3、创建子模块(Eureka服务模块)
4、修改
pom.xml
demo/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>demo-eureka</module></modules><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version><spring-cloud.version>2021.0.5</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
demo/demo-eureka/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-eureka</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-eureka</name><description>demo-eureka</description></project>
5、创建并修改配置文件
demo/demo-eureka/src/main/resources/application.yml
server:port:8000spring:application:name: demo-eureka
eureka:instance:hostname: 127.0.0.1
client:register-with-eureka:false# 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry:false# 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心地址
6、添加Eureka注解
demo/demo-eureka/src/main/java/com/example/demo/demoeureka/DemoEurekaApplication.java
packagecom.example.demo.demoeureka;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassDemoEurekaApplication{publicstaticvoidmain(String[] args){SpringApplication.run(DemoEurekaApplication.class, args);}}
7、运行
8、创建其他服务
9、修改
pom.xml
demo/pom.xml
<packaging>pom</packaging><modules><module>demo-eureka</module><module>demo-one</module><module>demo-two</module></modules>
demo/demo-one/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-one</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-one</name><description>demo-one</description></project>
demo/demo-two/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-two</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-two</name><description>demo-two</description></project>
10、创建并修改配置文件
把服务注册到Eureka中
demo/demo-one/src/main/resources/application.yml
server:port:8001spring:application:name: demo-one
eureka:client:service-url:defaultZone: http://127.0.0.1:8000/eureka/
demo/demo-two/src/main/resources/application.yml
server:port:8002spring:application:name: demo-two
eureka:client:service-url:defaultZone: http://127.0.0.1:8000/eureka/
11、添加Eureka注解
demo/demo-one/src/main/java/com/example/demo/demoone/DemoOneApplication.java
packagecom.example.demo.demoone;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplicationpublicclassDemoOneApplication{publicstaticvoidmain(String[] args){SpringApplication.run(DemoOneApplication.class, args);}}
demo/demo-two/src/main/java/com/example/demo/demotwo/DemoTwoApplication.java
packagecom.example.demo.demotwo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplicationpublicclassDemoTwoApplication{publicstaticvoidmain(String[] args){SpringApplication.run(DemoTwoApplication.class, args);}}
12、添加项目服务
13、运行
14、访问404解决
***开启监控服务配置
demo/demo-one/src/main/resources/application.yml
***开启监控服务配置
demo/demo-two/src/main/resources/application.yml
management:endpoints:web:exposure:include:"*"
15、项目目录结构
搭建Eureka集群
应对服务挂了找不到服务的问题
1、准备
SwitchHosts
:用于修改 IP 和 域名 的映射
2. 创建 Eureka Server 模块
demo/pom.xml
<packaging>pom</packaging><modules><module>eureka-server-01</module><module>eureka-server-02</module><module>eureka-server-03</module><module>eureka-client-01</module></modules>
demo/eureka-server-01
、
demo/eureka-server-02
、
demo/eureka-server-03
- 启动类 -
demo/eureka-server-01/src/main/java/com/example/demo/eurekaserver01/EurekaServer01Application.java``````packagecom.example.demo.eurekaserver01;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServer01Application{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServer01Application.class, args);}}
-demo/eu¬reka-server-02/src/main/java/com/example/demo/eurekaserver01/EurekaServer02Application.java``````packagecom.example.demo.eurekaserver02;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServer02Application{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServer02Application.class, args);}}
-demo/eureka-server-03/src/main/java/com/example/demo/eurekaserver01/EurekaServer03Application.java``````packagecom.example.demo.eurekaserver03;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServer03Application{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServer03Application.class, args);}}
- 配置项 -
demo/eureka-server-01/src/main/resources/application.yml``````server:port:8001spring:application:name: eureka-server-01eureka:instance:hostname: www.eureka-server-01.comclient:register-with-eureka:false# 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry:false# 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
-demo/eureka-server-02/src/main/resources/application.yml``````server:port:8002spring:application:name: eureka-server-02eureka:instance:hostname: www.eureka-server-02.com client:register-with-eureka:false# 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry:false# 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
-demo/eureka-server-03/src/main/resources/application.yml``````server:port:8003spring:application:name: eureka-server-03eureka:instance:hostname: www.eureka-server-03.com client:register-with-eureka:false# 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry:false# 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/ # 注册中心地址
3、运行
4、创建 Eureka Client 模块并注册到Eureka服务中
- 启动类 -
demo/eureka-client-01/src/main/java/com/example/demo/eurekaclient01/EurekaClient01Application.java``````packagecom.example.demo.eurekaclient01;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@SpringBootApplicationpublicclassEurekaClient01Application{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaClient01Application.class, args);}}
- 配置项 -
demo/eureka-client-01/src/main/resources/application.yml``````server:port:8081spring:application:name: eureka-client-01eureka:instance:hostname: www.eureka-client-01.com instance-id: eureka-client-01client:service-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/management:endpoints:web:exposure:include:"*"
5、运行
6、项目目录结构
网络请求获取数据
1、 创建项目
demo/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>service-01</module><module>service-02</module></modules><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
demo/service-01/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-01</artifactId><version>0.0.1-SNAPSHOT</version><name>service-01</name><description>service-01</description></project>
demo/service-02/pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-02</artifactId><version>0.0.1-SNAPSHOT</version><name>service-02</name><description>service-02</description></project>
demo/service-01/src/main/resources/application.yml
server:port:8001
demo/service-02/src/main/resources/application.yml
server:port:8002
2、 创建请求
demo/service-01/src/main/java/com/example/demo/service01/TestController.java
packagecom.example.demo.service01;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;@RestController@RequestMappingpublicclassTestController{privatefinalRestTemplate restTemplate =newRestTemplate();@GetMapping("msg")publicStringmsg(){return"service-01 服务中请求 service-02";}@GetMapping("say")publicStringsay(){String html = restTemplate.getForObject("https://www.example.com/",String.class);String msg = restTemplate.getForObject("http://127.0.0.1:8002/msg",String.class);returnString.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);}}
demo/service-01/src/main/java/com/example/demo/service01/TestController.java
packagecom.example.demo.service02;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;@RestController@RequestMappingpublicclassTestController{privatefinalRestTemplate restTemplate =newRestTemplate();@GetMapping("msg")publicStringmsg(){return"service-02 服务中请求 service-01";}@GetMapping("say")publicStringsay(){String html = restTemplate.getForObject("https://www.example.com/",String.class);String msg = restTemplate.getForObject("http://127.0.0.1:8001/msg",String.class);returnString.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);}}
3、 运行
4、 目录结构
利用Eureka实现服务间数据请求
1、 创建项目
demo/pom.xml
<packaging>pom</packaging><modules><module>eureka-server</module><module>service-01</module><module>service-02</module></modules>
2、 添加依赖
demo/pom.xml
<!-- 用于服务间请求的依赖 --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
3、
demo/eureka-server
demo/eureka-server/src/main/resources/application.yml
server:port:8001spring:application:name: eureka-server
eureka:instance:hostname: www.eureka-server.com
client:register-with-eureka:false# 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry:false# 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心地址
demo/eureka-server/src/main/java/com/example/demo/eurekaserver/EurekaServerApplication.java
packagecom.example.demo.eurekaserver;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServerApplication.class, args);}}
4、
demo/service-01
demo/service-01/src/main/resources/application.yml
server:port:8081spring:application:name: service-01eureka:instance:hostname: www.eureka-server.com
instance-id: service-01client:service-url:defaultZone: http://www.eureka-server.com:8001/eureka/
management:endpoints:web:exposure:include:"*"
demo/service-01/src/main/java/com/example/demo/service01/Service01Application.java
packagecom.example.demo.service01;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublicclassService01Application{publicstaticvoidmain(String[] args){SpringApplication.run(Service01Application.class, args);}}
demo/service-01/src/main/java/com/example/demo/service01/api/IService02.java
packagecom.example.demo.service01.api;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;@FeignClient(value ="SERVICE-02")publicinterfaceIService02{@GetMapping("/getMsg")publicStringgetMsg();}
demo/service-01/src/main/java/com/example/demo/service01/Service01Controller.java
packagecom.example.demo.service01;importcom.example.demo.service01.api.IService02;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjavax.annotation.Resource;@RestController@RequestMappingpublicclassService01Controller{@ResourceprivateIService02 service02;@GetMapping("/say")publicStringsay(){return service02.getMsg();}@GetMapping("getMsg")publicStringgetMsg(){return"SERVICE-02 调用 SERVICE-01";}}
5、
demo/service-02
demo/service-02/src/main/resources/application.yml
server:port:8082spring:application:name: service-02eureka:instance:hostname: www.eureka-server.com
instance-id: service-02client:service-url:defaultZone: http://www.eureka-server.com:8001/eureka/
management:endpoints:web:exposure:include:"*"
demo/service-02/src/main/java/com/example/demo/service02/Service02Application.java
packagecom.example.demo.service02;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublicclassService02Application{publicstaticvoidmain(String[] args){SpringApplication.run(Service02Application.class, args);}}
demo/service-02/src/main/java/com/example/demo/service02/api/IService01.java
packagecom.example.demo.service02.api;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;@FeignClient(value ="SERVICE-01")publicinterfaceIService01{@GetMapping("/getMsg")publicStringgetMsg();}
demo/service-02/src/main/java/com/example/demo/service02/Service02Controller.java
packagecom.example.demo.service02;importcom.example.demo.service02.api.IService01;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjavax.annotation.Resource;@RestController@RequestMappingpublicclassService02Controller{@ResourceprivateIService01 service01;@GetMapping("/say")publicStringsay(){return service01.getMsg();}@GetMapping("getMsg")publicStringgetMsg(){return"SERVICE-01 调用 SERVICE-02";}}
6、 运行
优先运行
EurekaServerApplication
启动类
7、 目录结构
版权归原作者 Prosper Lee 所有, 如有侵权,请联系我们删除。