lagou-service-resume 8080-----
lagou-service-autodeliver 8090----
lagou-cloud-eureka-server 8761----
基于Maven构建SpringBoot⼯程,在SpringBoot⼯程之上搭建EurekaServer服务(lagou-cloud-eureka-server-8761)
Spring Cloud 是一个综合的项目,下面有很多子项目,比如eureka子项目(版本号 1.x.x)
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-dependenciesartifactId>
- <version>Greenwich.RELEASEversion>
- <type>pomtype>
- <scope>importscope>
- dependency>
- dependencies>
- dependencyManagement>
- "1.0" encoding="UTF-8"?>
- <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">
- <parent>
- <artifactId>lagou-parentartifactId>
- <groupId>com.lagougroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>lagou-cloud-eureka-server-8761artifactId>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
- dependencies>
-
- <properties>
- <maven.compiler.source>11maven.compiler.source>
- <maven.compiler.target>11maven.compiler.target>
- properties>
-
- project>
注意:在父工程的pom文件中手动引入jaxb的jar,因为Jdk9之后默认没有加载该模块,EurekaServer使用到,所以需要手动导入,否则EurekaServer服务无法启动。
父工程pom.xml
-
- <dependency>
- <groupId>com.sun.xml.bindgroupId>
- <artifactId>jaxb-coreartifactId>
- <version>2.2.11version>
- dependency>
- <dependency>
- <groupId>javax.xml.bindgroupId>
- <artifactId>jaxb-apiartifactId>
- dependency>
- <dependency>
- <groupId>com.sun.xml.bindgroupId>
- <artifactId>jaxb-implartifactId>
- <version>2.2.11version>
- dependency>
- <dependency>
- <groupId>org.glassfish.jaxbgroupId>
- <artifactId>jaxb-runtimeartifactId>
- <version>2.2.10-b140310.1920version>
- dependency>
- <dependency>
- <groupId>javax.activationgroupId>
- <artifactId>activationartifactId>
- <version>1.1.1version>
- dependency>
-
- # Eureka server 端口号
- server:
- port: 8761
-
- spring:
- application:
- name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
-
- # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
- eureka:
- instance:
- hostname: localhost # 当前Eureka实例的主机名
- client:
- service-url: # 配置客户端所交互的Eureka Server的地址
- defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
- register-with-eureka: false # 当前自己就是Server,不需要注册自己
- fetch-registry: false # 获取注册中心的服务信息,自己就是Server,不需要从Eureka Server获取服务信息,默认为true
- package com.lagou.edu;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- @SpringBootApplication
- // 声明当前项目为Eureka服务
- @EnableEurekaServer
- public class LagouEurekaServerApp8761 {
- public static void main(String[] args) {
- SpringApplication.run(LagouEurekaServerApp8761.class, args);
- }
- }
在互联网应用中,服务实例很少有单个的。
即使微服务消费者会缓存服务列表,但是如果EurekaServer只有一个实例,该实例挂掉,正好微服务消费者本地缓存列表中的服务实例也不可用,那么这个时候整个系统都受影响。
在生产环境中,我们会配置Eureka Server集群实现高可用。Eureka Server集群之中的节点通过点对点(P2P)通信的方式共享服务注册表。我们开启两台 EurekaServer 以搭建集群。
由于是在个人计算机中进行测试很难模拟多主机的情况,Eureka配置server集群时需要执行host地址。 所以需要修改个人电脑中host地址
127.0.0.1 LagouCloudEurekaServerA
127.0.0.1 LagouCloudEurekaServerB
8761模块配置文件
- # Eureka server 端口号
- server:
- port: 8761
-
- spring:
- application:
- name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
-
- # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
- eureka:
- instance:
- hostname: LagouCloudEurekaServerA # 当前Eureka实例的主机名
- client:
- service-url:
- # 配置客户端所交互的Eureka Server的地址 (Eureka Server集群中每一个Server其实相对于其他Server来说都是Client)
- # 集群模式下,defaultZone应该指向其他Eureka Server,如果有更多其他Server实例,逗号拼接即可
- defaultZone: http://LagouCloudEurekaServerB:8762/eureka
- register-with-eureka: true # 集群模式下改为true
- fetch-registry: true # 获取注册中心的服务信息,默认为true
8762配置文件
- # Eureka server 端口号
- server:
- port: 8762
-
- spring:
- application:
- name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
-
- # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
- eureka:
- instance:
- hostname: localhost # 当前Eureka实例的主机名
- client:
- service-url: # 配置客户端所交互的Eureka Server的地址
- defaultZone: http://LagouCloudEurekaServerA:8761/eureka
- register-with-eureka: true
- fetch-registry: true # 获取注册中心的服务信息,默认为true
http://lagoucloudeurekaservera:8761/和http://lagoucloudeurekaserverb:8762/会发现注册中心LAGOU-CLOUDEUREKA-SERVER 已经有两个节点,并且 registered-replicas (相邻集群复制节点)中已经包含对方
注册简历微服务
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-commonsartifactId>
- dependency>
- "1.0" encoding="UTF-8"?>
- <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">
- <parent>
- <artifactId>lagou-parentartifactId>
- <groupId>com.lagougroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>lagou-service-resumeartifactId>
-
-
- <dependencies>
- <dependency>
- <groupId>com.lagougroupId>
- <artifactId>lagou-service-commonartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- dependencies>
-
-
- <properties>
- <maven.compiler.source>11maven.compiler.source>
- <maven.compiler.target>11maven.compiler.target>
- properties>
-
- project>
在application.yml 中添加Eureka Server高可用集群的地址及相关配置
- server:
- port: 8080
-
- spring:
- application:
- name: lagou-service-resume
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://8.142.8.105:3306/lagou?useUnicode=true&characterEncoding=utf8&useSSL=false
- username: root
- password: g5201314yj
- jpa:
- database: MySQL
- show-sql: true
- hibernate:
- naming:
- physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
-
- # 注册到Eureka服务中心
- eureka:
- client:
- service-url: # eureka server的路径
- # 注册到集群,就把多个EurekaServer地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个
- defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka
-
- instance:
- prefer-ip-address: true # 服务实例中显示ip,而不是显示主机名(兼容老的Eureka版本)
- # 自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
- instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
经验:自定义实例显示格式,加上版本号,便于多版本管理
注意:
说明:其他微服务注册可参照执行
instance-id添加上版本号的效果
此处自动投递微服务是消费者
(1)pom文件引入坐标,添加eureka client的相关坐标
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- server:
- port: 8090
-
- # 注册到Eureka服务中心
- eureka:
- client:
- service-url:
- # 注册到集群,就把多个EurekaServer地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个
- defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka
-
- instance:
- prefer-ip-address: true # 服务实例中显示ip,而不是显示主机名(兼容老的Eureka版本)
- # 自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
- instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
-
- spring:
- application:
- name: lagou-service-autodeliver
- package com.lagou.edu;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.client.RestTemplate;
-
- @SpringBootApplication
- @EnableDiscoveryClient
- public class AutoDeliverApplication {
- public static void main(String[] args) {
- SpringApplication.run(AutoDeliverApplication.class, args);
-
- }
-
- // 使用RestTemplate模板对象远程调用
- @Bean
- public RestTemplate gerRestTemplate() {
- return new RestTemplate();
- }
- }
- package com.lagou.edu.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/autodeliver")
- public class AutoDeliverController {
- @Autowired
- private RestTemplate restTemplate;
-
- // /autodeliver/checkState/
-
- /* @GetMapping("/checkState/{userId}")
- public Integer findResumeOpenState(@PathVariable Long userId) {
- // 调用远程服务->简历微服务接口 RestTemplate
- Integer forObject = restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId, Integer.class);
- return forObject;
- } */
-
- @Autowired
- private DiscoveryClient discoveryClient;
-
- /**
- * 改造:服务注册到Eureka之后的改造
- *
- * @param userId
- * @return
- */
- @GetMapping("/checkState/{userId}")
- public Integer findResumeOpenState(@PathVariable Long userId) {
- // 从Eureka Server中获取我们关注的那个服务的实例信息以及接口信息
- // 1、从Eureka Server中获取lagou-service-resume服务的实例信息(使用客户端对象做这件事情)
- List
instances = discoveryClient.getInstances("lagou-service-resume"); - // 2、如果有多个实例,选择第一个使用(负载均衡的过程)
- ServiceInstance serviceInstance = instances.get(0);
- // 3、从元数据信息中获取host、port
- String host = serviceInstance.getHost();
- int port = serviceInstance.getPort();
- String url = "http://" + host + ":" + port + "/resume/openstate/" + userId;
- System.out.println("===========>从Eureka Server集群获取服务私立拼接的url:" + url);
- // 调用远程服务->简历微服务接口 RestTemplate
- Integer forObject = restTemplate.getForObject(url, Integer.class);
- return forObject;
- }
- }
(5)访问:http://localhost:8080/resume/openstate/1545132
发现调用成功
注:
项目模块
详细代码:
链接:https://pan.baidu.com/s/15nAeIuJNTj1JXKW2CcVD_A?pwd=9tjz
提取码:9tjz