Spring Cloud+Eureka 微服务

微服务

微服务是以缩短交付周期为核心、基于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中提供服务接口
例如:
服务提供者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对象来调用服务提供者的接口
例如:
RestTemplate服务消费者Controller

方式二: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”)//字符串为调用的提供者服务名
例如:
feign调用的service接口
controller:
feign调用的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/

相同方法将服务消费者客户端注册到高可用注册中心。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值