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>

最新文章

  1. [AHOI 2009] 维护序列(线段树模板题)
  2. Oracle定时查询结果输出到指定的log文件
  3. GPU深度发掘(一)::GPGPU数学基础教程
  4. 十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众
  5. Apache虚拟主机配置
  6. apache日志文件太大的问题
  7. 基于jQuery的简易瀑布流实现
  8. Oracle 查询字段在什么表
  9. Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误
  10. css实现居中
  11. 学渣也要搞 laravel(3)—— HTTP控制器
  12. Ajax简单应用-购物车
  13. css3技巧属性之text-overflow
  14. 10317 Fans of Footbal Teams(并查集)
  15. SQLite学习手册(数据表和视图)
  16. 【翻译】了解ASP.NET MVC中的Ajax助手
  17. Epson 微型打印机打印 LOGO C#
  18. NPOI导出Excel帮助类
  19. python 条件与循环
  20. SQLyog简介及其功能(附百度云盘下载地址)

热门文章

  1. printk函数
  2. mac brew nginx php php-fpm xdebug
  3. tensorboard在Mac OS X系统环境下如何启动
  4. 【默默努力】react-drag-grid
  5. css---4表单相关伪类
  6. loj6005 [网络流24题]最长递增子序列
  7. String相加解析
  8. Linux虚拟机ip为127.0.0.1的处理
  9. 在scrapy中过滤重复的数据
  10. 如何使用Pig集成分词器来统计新闻词频?