1、注册中心用来管理每个服务与服务之间的依赖关系(服务治理),存放服务地址相关信息(接口地址)
2、服务提供者:提供服务接口
3、服务消费者:调用服务接口
4、服务注册:将信息注册到注册中心上
5、服务发现:从注册中心上获取服务信息
SpringCloud支持三种注册中心: Eureka、Consul、Zookeeper
Dubbo支持两种注册中心:Redis、Zookeeper
本文基于SpringCloud实现Eureka的服务注册与发现
代码实现
1.创建springboot项目
引入如下jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring_cloud_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_cloud_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<modules>
<module>register-server</module>
<module>producer</module>
<module>consumer</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.搭建服务注册中心
(1)引入jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
(2)添加application.properties文件
#注册中心服务ID
spring.application.name=register-server
#端口号
server.port=8800
# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。
# 由于当前这个应用就是Eureka Server,故而设为false
eureka.client.register-with-eureka=false
# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,
# 不需要同步其他的Eureka Server节点的数据,故而设为false。
eureka.client.fetch-registry=false
# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
# 默认是http://localhost:8800/eureka;多个地址间可使用,分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
(3)启动类添加注解
package com.example.registerserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class RegisterServerApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterServerApplication.class, args);
}
}
@EnableEurekaServer的原理:
(1)@EnableEurekaServer的作用是向容器中加入一个标识类Marker 。
(2)EurekaServerAutoConfiguration类上的@ConditionalOnBean({Marker.class})注解通过对容器中有无Marker类,实现了开关控制效果同时这个类装载了多个bean,这些bean是实现eureka服务端的主要功能。
(3)EurekaServerInitializerConfiguration是初始化eureka的实现类,这个类实现了SmartLifeCycle接口,通过生命周期回调方法初始化eureka。
(4)启动服务访问http://localhost:8800/
3.搭建服务提供者
(1)引入jar包
<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>
</dependency>
(2)添加application.properties文件
#服务名称
spring.application.name=producer
#端口号
server.port=8700
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
#启动服务发现的功能,开启了才能调用其它服务
spring.cloud.config.discovery.enabled=true
#发现服务的名字--对应注册中心的服务名字
spring.cloud.config.discovery.serviceId=register-server
(3)启动类添加注解
package com.example.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
(4)启动服务访问http://localhost:8800/
4.搭建服务消费者
(1)引入jar包
<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>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
(2)添加application.yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
server:
port: 8600
spring:
application:
name: consumer
(3)启动类添加注解
package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced //使用负载均衡机制
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
(4)启动服务访问http://localhost:8800/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)