consumer ---------Eureka-----------------provider
Euraka提供的是和Zookeeper类似的功能,服务注册组件。
pom
导入eureka server启动器。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.wang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-7001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
配置文件
服务注册地址,端口号等信息配置
eureka:
instance:
hostname: localhost #服务实例名称
client:
healthcheck:
enabled: true #这个配置必须配置才能启动成功
register-with-eureka: false #表示是否向eureka注册中心注册自己
fetch-registry: false #false 表示自己是注册中心
service-url: #表示监控注册地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 7001
启动类
注解启动eureka---》 @EnableEurekaServer
package com.wang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class,args);
}
}
测试:
启动成功,
服务注册
服务端提供服务,需要先注册到eureka ;
1.导入eureka client 依赖
<!-- eureka配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept-8001
info:
app.name: com.wang.springcloud
company.name: zhipanwang
3 EnableEurekaClient
启动类注解,eureka组件会自动注册服务
package com.wang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //自动注册服务到eureka服务器
public class DeptProvider {
public static void main(String[] args) {
SpringApplication.run(DeptProvider.class,args);
}
}
问题
搭建eureka之后启动报错,找不到method
09:10:12.136 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
查了下,spring boot版本问题 换到1.5.3之后没问题
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
eureka.client.healthcheck.enabled 配置问题
配置eureka.client.healthcheck.enabled为true解决
Parameter 3 of method eurekaClient in org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration required a bean of type 'com.netflix.appinfo.HealthCheckHandler' that could not be found.
- Bean method 'eurekaHealthCheckHandler' not loaded because @ConditionalOnProperty (eureka.client.healthcheck.enabled) did not find property 'eureka.client.healthcheck.enabled'