版本信息
Spring Cloud : Hoxton.SR1
Spring Boot : 2.2.2.RELEASE
Zookeeper : 3.5.6
Consul : 1.6.3
Nacos: 1.1.4
核心api
- org.springframework.cloud.client.serviceregistry.ServiceRegistry 服务注册
- org.springframework.cloud.client.serviceregistry.Registration 注册信息
- org.springframework.cloud.client.ServiceInstance 服务实例信息
服务注册只能单注册中心,不能多注册中心
- org.springframework.cloud.client.discovery.DiscoveryClient 服务发现
- org.springframework.cloud.client.ServiceInstance 服务实例信息
默认实现:org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient,可以多种服务发现并行
基于Euraka的注册中心以及服务发现
服务端
应用
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.配置文件application.yml
spring:
application:
name: euraka-server
server:
port: 9090
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:${
server.port}/eureka/
3.启动类EurekaDiscoveryServerApplication
/**
* Eureka 服务发现注册中心
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/1
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaDiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDiscoveryServerApplication.class, args);
}
}
Eureka 服务端访问地址: http://127.0.0.1:9090/
Spring Cloud Eureka
服务端配置类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBeanEureka 服务端端点:
https://github.com/Netflix/eureka/wiki/Eureka-REST-operationsSpring Cloud Eureka 服务端 端点去掉版本信息即可,比如/eureka/v2/apps改成/eureka/apps
源码分析
- 分析@EnableEurekaServer
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
}
@Configuration(proxyBeanMethods = false)
public class EurekaServerMarkerConfiguration {
@Bean
public Marker eurekaServerMarkerBean() {
return new Marker();
}
class Marker {
}
}
导入了EurekaServerMarkerConfiguration配置信息,该配置注册了Marker的Bean,由前面的@EnableConfigServer得知,这个Marker的Bean是一个类EurekaServerAutoConfiguration的自动装配条件
- 分析EurekaServerAutoConfiguration
@Configuration(proxyBeanMethods = false)
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({
EurekaDashboardProperties.class,
InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer{
...
}
导入了EurekaServerInitializerConfiguration配置信息
客户端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.配置文件application.yml(由于采用多个客户端,客户端采用profile进行区分)
spring:
application:
name: discovery-client
eureka:
client:
enabled: false
--- # eureka profile
spring:
profiles: eureka
server:
port: 8090
eureka:
client:
enabled: true
service-url:
defaultZone: http://127.0.0.1:9090/eureka/
3.启动类DiscoveryClientApplication
/**
* 服务发现客户端程序
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/1
*/
@SpringBootApplication
//@EnableDiscoveryClient // 这个注解可以去掉
public class DiscoveryClientApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryClientApplication.class, args)