TZ_13_微服务场景Eureka
2024-09-04 02:16:27
1.搭建Eureka的注册中心
1.1Eureka几个时间间隔配置详解
1 >客户端信息上报到eureka服务的时间周期,配置的值越小,上报越频繁,eureka服务器应用
状态管理一致性越高
#客户端信息上报到服务的周期
eureka.client.instance-info-replication-interval-seconds=30
2> 客户端拉取Eureka服务器配置信息的时间周期
#客户端拉取服务器配置信息的周期,默认30s
eureka.client.registry-fetch-interval-seconds=30
1.2Eureka服务中心
application.yaml文件的配置
#配置自己的端口号
server:
port: 10086
#配置注册中心让自己也能注册到自己 Client就不会因为注册不到自己而报错了
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
# 将实例的地址配死 访问的而不是自己的主机名
instance:
prefer-ip-address: true
ip-address: 127.0.0.1 #配置自己的服务名
spring:
application:
name: eureka-service
1.3服务提供者
application.yaml的配置
server:
port: 9090 #配置别名
mybatis:
type-aliases-package: com.hdh.pojo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1 spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/shop
username: root
password: 12345
application:
name: user-servic
1.4服务调用者
application.yaml的配置
#配置自己的端口号
server:
port: 10086 eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
# 将实例的地址配死 访问的而不是自己的主机名
instance:
prefer-ip-address: true
ip-address: 127.0.0.1 #配置自己的服务名
spring:
application:
name: eureka-service
1.5调用者发起调用Cousmer 也就是Controller
application.class起步类
//拉取在注册中心注册过的服务
@EnableDiscoveryClient
@SpringBootApplication
public class ApplicationService { @Bean
//对json数据自动处理
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ApplicationService.class);
} }
获取指定服务 通过discoveryClient.getInstances("user-service");
@RestController
@RequestMapping("User")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("{id}")
public User selectById(@PathVariable(name = "id") Integer id) {
//获取注册过的服务 通过name获取实例
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
ServiceInstance insrance = instances.get(0);
//获取ip和端口
String host = insrance.getHost();
int port = insrance.getPort();
String url = "http://"+host+":"+port+"/User/" + id;
User user = restTemplate.getForObject(url, User.class);
return user; }
}
1.6起步类
@EnableEurekaServer
@SpringBootApplication
public class EurekaService {
public static void main(String[] args) {
SpringApplication.run(EurekaService.class); }
}
1.7Eureka客户端和服务端的起步坐标
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.7.RELEASE</version>
</dependency> <!-- Eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
最新文章
- [AHOI 2009] 维护序列(线段树模板题)
- Oracle定时查询结果输出到指定的log文件
- GPU深度发掘(一)::GPGPU数学基础教程
- 十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众
- Apache虚拟主机配置
- apache日志文件太大的问题
- 基于jQuery的简易瀑布流实现
- Oracle 查询字段在什么表
- Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误
- css实现居中
- 学渣也要搞 laravel(3)—— HTTP控制器
- Ajax简单应用-购物车
- css3技巧属性之text-overflow
- 10317 Fans of Footbal Teams(并查集)
- SQLite学习手册(数据表和视图)
- 【翻译】了解ASP.NET MVC中的Ajax助手
- Epson 微型打印机打印 LOGO C#
- NPOI导出Excel帮助类
- python 条件与循环
- SQLyog简介及其功能(附百度云盘下载地址)