帮别做网站,较好网站建设公司,苏州网站设计公司兴田德润i网址多少,石家庄做网站价格这是本人学习的总结#xff0c;主要学习资料如下 - 马士兵教育
[TOC](目录)1、Eureka
1.1、架构
Eureka是SpringCloud Nexflix的核心子模块#xff0c;其中包含Server和Client。
Server提供服务注册#xff0c;存储所有可用服务节点。
Client用于简化和Server的通讯复杂…这是本人学习的总结主要学习资料如下 - 马士兵教育
[TOC](目录)1、Eureka
1.1、架构
Eureka是SpringCloud Nexflix的核心子模块其中包含Server和Client。
Server提供服务注册存储所有可用服务节点。
Client用于简化和Server的通讯复杂度。
下面是Eureka的简单架构图 每一个服务节点需要在Eureka Server中注册如果需要其他节点的服务则需要远程调用Service ProviderProvider会访问Server由Server找到一个合适的节点提供服务给cumsumer 1.2、核心特性
服务注册这是最核心的功能其余的特性都是对这个功能的加强。服务续约Client每隔30s就会向Server发送一次心跳来续约超过90s没有续约就会被Server删除这个服务节点。服务下线Client可以主动向Server发送cancel命令优雅下线。缓存注册列表Client会缓存从Server获取的注册列表并且每30s更新一次。 2、建立Spring Cloud项目
2.1、项目结构和父项目依赖
接下来就是代码展示如何配置启动server和client以及client之间获取信息。
这是项目结构两个子module分别是server和order-client和user-client。server提供注册服务另外两个作为client则是到server注册然后互相调用对方的服务。 这是根目录的dependency
propertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetjava.version1.8/java.versionspring.cloud.versionHoxton.SR12/spring.cloud.versionspring.boot.version2.3.12.RELEASE/spring.boot.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion${spring.boot.version}/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion${spring.boot.version}/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactIdversion${spring.boot.version}/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-netflix-eureka-server/artifactId/dependency/dependencies2.2、启动Server
2.2.1、dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId
/dependency2.2.2、配置文件
resources/application.yml
spring:application:name: msb-eureka-server
server:port: 8761eureka:instance:#注册实例名称hostname: localhost#是否将自己的ip注册到eureka中默认false 注册 主机名prefer-ip-address: true# Eureka客户端需要多长时间发送心跳给Eureka表明他仍然或者默认是30# 通过下面方式我们可以设置默认单位是秒lease-renewal-interval-in-seconds: 10# Eurkea服务器在接受到实例最后一次发送的心跳后需要等待多久可以将次实例删除# 默认值是90# 通过下面方式我们可以设置默认单位是秒lease-expiration-duration-in-seconds: 30client:#是否注册到eureka服务中register-with-eureka: false#是否拉取其他服务fetch-registry: false
2.2.3、Server端启动代码
EnableEurekaServer
// 因为一直报Gson包冲突所以加上了exclude
SpringBootApplication(exclude {GsonAutoConfiguration.class})
public class EureakServerApplication {public static void main(String[] args) {SpringApplication.run(EureakServerApplication.class);}
}启动以后打开网页检查。localhost:8761 2.3、启动Client
2.3.1、dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId
/dependency2.3.2、配置文件
order-client和user-client都一样因为是单机模拟所以监听的端口号不同。
# 节点在server中注册的名字
spring:application:# user-client则用user-clientname: order-client
server:
# order-client 监听9002, user-client监听9003port: 9002eureka:client:# 这个一定要配对server地址后面默认要加一个上下文eurekaservice-url:defaultZone: http://localhost:8761/eurekamanagement:endpoints:web:exposure:include: shutdown #暴露shutdown端点endpoint:shutdown:enabled: true #再次确认暴露shutdown端点feign:tokenId: 111111111111111111112.3.3、Client端启动代码
注意有两个注解可以将其标注为Client分别是EnableDiscoveryClient和EnableEurekaClient。
这里推荐使用EnableDiscoveryClient因为后者是netfliex提供的如果使用后者后期要更换其它注册中心就需要更换注解比较麻烦。
这是order-client的代码
EnableDiscoveryClient // 这是官方提供的 ,我们以后可能切换其他的注册中心比如说nacos那我们就直接切换就行了
//EnableEurekaClient // 是netflix提供的如果用这个注解就只能服务于eureka
SpringBootApplication
public class EurekaOrderClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaOrderClientApplication.class);}
}这时user-client的代码
EnableDiscoveryClient // 这是官方提供的 ,我们以后可能切换其他的注册中心比如说nacos那我们就直接切换就行了
//EnableEurekaClient // 是netflix提供的如果用这个注解就只能服务于eureka
SpringBootApplication(exclude {GsonAutoConfiguration.class})
public class EurekaUserClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaUserClientApplication.class);}
}到Server的页面查看两个Client都注册成功。
2.3.4、提供RPC服务
因为RPC是基于HTTP实现的协议所以我们提供RPC服务时就像写一个controller的服务一样。
这里设定order模块会调用user提供的服务。
Slf4j
RestController
public class UserController {RequestMapping(/getUserInfo)public String getUser(String userId) {log.info(userId); return userInfo: {userId: userId };;}
}接下来就看order模块如何通过Eureka调用user提供的服务。
2.4、服务之间获取信息
引入LoadBalancerClient从这个bean中可以获得其他注册的client元数据比如地址端口号等。
获取到这些信息后就可以组成请求地址然后获取数据。
下面这个例子展示了如何获取其他client的元信息并且调用其它client的服务。
Service
public class OrderService {Autowiredprivate LoadBalancerClient eurekaClient;Autowiredprivate RestTemplate restTemplate;public void getUser() {ServiceInstance instance eurekaClient.choose(msb-user);String hostname instance.getHost();int port instance.getPort();String uri /getUserInfo?userId userId;String url http:// hostname : port uri;return restTemplate.getForObject(url, String.class);}}