![在这里插入图片描述](https://img-blog.csdnimg.cn/c386a4c7ae214caf87b51fd1ea63aeff.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5q2l5bCU5pav54m5,size_19,color_FFFFFF,t_70,g_se,x_16#pic_center)
微服务核心技术
一、微服务核心技术
1、服务注册和发现
1.1 技术选型
1.2 Eureka实现
1.2.1 搭建Erueka Server
1、创建工程
2、导入坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3、配置application.yml
spring:
application:
name: eureka-server
server:
port: 9000 #端口
#配置eureka server
eureka:
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:9000/eureka/
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #剔除服务间隔
4、配置启动类
@SpringBootApplication
//激活eureakaserver
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
5、启动
1.2.2 将服务提供者注册到EurekaServer
1、引入EurekaClient坐标
<!--引入EurekaClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、application.yml
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/ #多个eurekaserver之间用,隔开
instance:
prefer-ip-address: true #使用ip地址注册
3、启动类配置
@SpringBootApplication
@EntityScan("com.uncle.product.entity")
//激活eurekaClient
//@EnableEurekaClient
//@EnableDiscoveryClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}
1.2.3 服务消费者通过Eureka获取服务列表,并调用
1、引入EurekaClient坐标
<!--引入EurekaClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、application.yml
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/ #多个eurekaserver之间用,隔开
instance:
prefer-ip-address: true #使用ip地址注册
3、启动类配置
@SpringBootApplication
@EntityScan("com.uncle.order.entity")
//激活eurekaClient
//@EnableEurekaClient
//@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
4、DiscoveryClient的使用
@Autowired
private DiscoveryClient discoveryClient;
...
discoveryClient.getInstances("service-product");
### 1.3 EruekaServer高可用
-- 多个EruekaServer,各个微服务都注册到各个EruekaServer
### 1.4 EruekaServer高可用