Eureka:就是服务注册中心(可以是一个集群),
对外暴露自己的地址提供者:启动后向Eureka注册自己信息(地址,提供什么服务)
消费者:
向Eureka订阅服务,Eureka会将对应服务的所有提供者地址列表发送给消费者,
并且定期更新心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态
1、编写EurekaServer
接下来我们创建一个项目,启动一个EurekaServer
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.0.1.RELEASE
2、编写启动器
package com.itzheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class);
}
}
3、完善配置文件:在resources:application.yml
server:
port: 10086
4、完善EurekaServer
@EnableEurekaServer
5、启动EurekaServer
6、点击访问EurekaServer:http://localhost:10086/
发现控制台报错
7、需要设置服务地址:后会自动注册
server:
port: 10086
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
重新运行
运行没有异常
访问:http://localhost:10086/
发现服务没有名称
8、设置访问名称:完善application.yml
server:
port: 10086
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
重新运行项目
http://localhost:10086/
三、user-service使用Eureka :服务的提供方
1、在user-service当中的pom.xml当中引入Eureka
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.1.RELEASE
2、完善UserApplication,设置注册中心
package com.itzheng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan(“com.itzheng.user.mapper”)
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}
}
3、在user-service当中的resources配置Eureka
server:
port: 8082
spring:
application:
name: user-service
datasource:
url: jdbc:mysql://localhost:3306/itzheng
username: root
password: root
mybatis:
type-aliases-package: com.itzheng.user.pojo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
从新运行项目
4、Eureka :http://localhost:10086/ 发现多注册一个服务
三、consumer-demo注册Eureka :服务的使用方,consumer动态拉去服务列表
1、在consumer-demo的pom.xml当中
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.1.RELEASE
2、完善ConsumerApplication添加注解
@EnableDiscoveryClient
3、添加配置
server:
port: 8088
spring:
application:
name: consumer-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka