微服务
微服务是以缩短交付周期为核心、基于DevOps的演进式架构。具有独立部署、灵活扩展,有效隔离资源,利于团队组织架构调整的特点。
Eureka Server注册中心搭建
在项目中新建一个子模块
//在pom文件中导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//入口类上注解:
@EnableEurekaServer
@SpringBootApplication
//application.yml配置文件:
server:
port: 8761
eureka:
dashboard:
enabled: true # 表示控制面板是否启用
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka # 客户端注册地址
spring:
application:
name: eurekaserver # 服务名
Eureka Client服务提供者
新建子模块
//全部依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
//入口类添加注解:
@EnableDiscoveryClient //开启服务注册发现
//application.yml:
server:
port: 9001
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 客户端注册地址,可添加多个地址配置高可用
spring:
application:
name: compute-server # 服务名
profiles:
active: native
然后在Controller中提供服务接口
例如:
服务消费者
方式一:Eureka Client RestTemplate服务消费者
新建子模块
全部依赖:同服务提供者(见上方)
入口类添加注解:同服务提供者(见上方)
入口类中自动注入一个RestTemplate对象:
@Bean
@LoadBalanced
RestTemplate restTemplate(){
Return new RestTemplate();
}
//application.yml:
server:
port: 9002
eureka:
client:
enabled: true
service-url:
defaultZone: http://localhost:8761/eureka/ # 客户端注册地址,添加多个地址为高可用配置
spring:
application:
name: consumer-resttemplate
profiles:
active: native
//在Controller中通过RestTemplate对象来调用服务提供者的接口
例如:
方式二:Eureka Client Feign服务消费者
新建子模块
//全部依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
//application.yml:
server:
port: 9003
eureka:
client:
enabled: true
service-url:
defaultZone: http://localhost:8761/eureka/ # 客户端注册地址,添加多个地址为高可用配置
spring:
application:
name: consumer-feign
profiles:
active: native
入口类添加注解:
@EnableDiscoveryClient
@EnableFeignClient
service层:
interface上添加注解:@FeignClient(“compute-service”)//字符串为调用的提供者服务名
例如:
controller:
此时访问9003端口,需在浏览器地址栏传参,若不传参则报错。
Eureka Server高可用注册中心
在服务注册中心模块的resources目录下新建application-master.yml
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/ # 将8761的server注册到8762的注册中心上
register-with-eureka: true
fetch-registry: true
server:
enable-self-preservation: true
spring:
application:
name: eureka-server
在服务注册中心模块的resources目录下新建application-slave.yml
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ # 8762的server注册到8761的注册中心上,即互相注册
register-with-eureka: true
fetch-registry: true
server:
enable-self-preservation: true
spring:
application:
name: eureka-server
停止所有服务,然后打开master和slave的服务配置
启动master,再启动slave。此时master的控制台会报异常,是由于slave未启动,导致无法注册,当两边都启动成功时,就会互相注册。
将服务提供者客户端注册到高可用注册中心:
修改服务提供者的application.yml文件,defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
相同方法将服务消费者客户端注册到高可用注册中心。