SpringCloud Eureka服务治理

springCloud有很多个组件
**

服务治理:springcloud Eureka

**
构建服务注册中心
服务注册与发现服务
Eureka的基础架构
Eureka的服务治理机制
Eureka的配置

服务治理
服务治理是微服务的基础和核心。
服务注册:创建一个注册中心,每个服务向注册中心提供服务,将主机与端口号,版本号,通信协议给注册中心
服务发现:服务调用不用直接调用具体的实例,可以通过服务名发起调用。
搭建服务注册中心

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置Eureka服务器注册中心

server.port=1111

eureka.instance.hostname=localhost
#作为服务器不需要向自己注册
eureka.client.register-with-eureka=false
#服务器不需要检索服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

@EnableEurekaServer
@SpringBootApplication
public class SpringcloudApplication {

    public static void main(String[] args) {

//        SpringApplication.run(SpringcloudApplication.class, args);
        new SpringApplicationBuilder(SpringcloudApplication.class).web(true).run(args);
    }

}

登录localhost:1111
在这里插入图片描述
注册服务提供者
导入依赖

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

application.properties

spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动类

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceproducttestApplication {

    public static void main(String[] args) {

        SpringApplication.run(ServiceproducttestApplication.class, args);
    }
}

controller

@RestController
public class HelloController {
    private final Logger logger=Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host"+instance.getHost()+",service_id"+instance.getServiceId());
        return "Hello World";
    }
}

注册成功
在这里插入图片描述
服务的高可用
每一个服务即使提供者也是消费者,注册中心也不例外,
高可用的注册中心的集群
创建application-peer1.properties

spring.application.name=eureka-server
server.port=1111

eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/

创建application-peer2.properties

spring.application.name=eureka-server
server.port=1112

eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

修改hosts文件配置peer1和peer2
通过spring.profies.active属性来分别启动peer1和peer2
启动成功
在这里插入图片描述
在这里插入图片描述
都有互相的副本。
再把服务注册到两个注册中心上
application.properties

spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

这样两个注册中心上就都有hello-service的服务了
如果通过ip来定义注册中心,需要再配置文件的参数上加eureka.instance.perfer-ip-address=true

服务的消费任务由Ribbon完成,Ribbon是一个基于http和ycp的客户端负载均衡器。
通过java-jar不同的端口号创建两个hello-service服务
创建ribbon-consumer
导入依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

启动类

@EnableDiscoveryClient
//该应用注册为Eureka客户端
@SpringBootApplication
public class RibbonConsumerApplication {
    @Bean
    @LoadBalanced
    //开启客户端负载均衡
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }

}

配置文件

spring.application.name=ribbon-consumer
server.port=9000

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

controller

@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        return restTemplate.getForEntity("htt://HELLO-SERVICE/hello",String.class).getBody();
    }
}

基础架构:
服务注册中心:提供服务注册与发现功能,也就是eureka-server
服务提供者:HELLO-SERVICE
服务消费者:消费者应用从服务注册中心获取服务列表,从而调用所需要的服务
服务治理机制:
服务注册中心-1和服务注册中心-2,特们互相注册组成了高可用集群
服务提供者启动了两个实例,一个注册到服务注册中心1上,另一个注册到服务注册中心2上
还有两个服务消费者,他们呢分别指向了一个注册中心

服务提供者
服务提供者再启动的时候REST请求的方式将自己注册到EurekaServer上,将元数据存储在一个双层结构map上,第一层key是服务名,第二层key是服务的实例名,
服务同步
由于服务注册中心之间因为是互相注册为服务,当服务提供者发送请求到一个注册中心的时候,他会将请求转发到集群中相连的其他注册中心,从而实现中心之间的服务同步。
服务续约
读物提供者回维护一个心跳来高速Eureka Server还或者,防止EurekaServer的剔除任务,我们将该操作服务续约。
有两个重要属性:
eureka.instance.lease-renewal-interval-in-seconds=30
用于定义服务续约任务的调用时间间隔
eureka.instance.lease-expiration-duration-in-seconds=90
用于定义服务失效的时间,默认为90秒
服务消费者
Eureka Server会维护一份只读的服务清单返回客户端,同时清单会每隔30s进行更新一次。
eureka.client.registry-fetch-inteerval-seconds=30修改清单更新时间
对于方法实例的选择,Eureka中有Region和Zone的概念,一个Region可以包含多个Zone,每个客户端需要被注册到一个Zone中,所以客户端对应一个Region和一个Zone
服务下线
当服务非正常关闭的时候,会触发服务下线的请求给Eureka Server告诉服务注册中心我要下线了。
失效剔除
Eureka Server再启动的时候会创建一个定时任务,默认每个一段时间(默认60秒)将当前清单中超时(默认90秒)的没有续约的服务剔除出去。
自我保护
服务注册到Eureka Server 会维护一个心跳连接。服务器必须有容错机制,比如请求重试、断路器。
eureka.server.enable-self-preservation=false参数关闭保护机制

原理分析
Eureka有服务注册中心,服务提供者,服务消费者三个元素,后两者属于Eureka客户端。
我们将一个springboot应用注册到Eureka Server中主要做两件事
1.再主类中配置了@EnableDiscoveryClient注解
2再application.properties中用eureka.client.serviceUrl.defaultZone指定服务注册中心的位置。
DiscoveryClient类与Eureka Server相互协作
Eureka Client负责下面的任务
-向Eureka Server注册服务实例
-向Eureka Server服务续约
-当服务关闭期间,向Eureka Server取消租约
-查询Eureka Server中的服务实例列表
Eureka Client需要配置一个Eureka Server的URL列表
通过配置属性eureka.client.serviceUrl.defaultZone
Region 、Zone
客户端加载两个内容,第一个是Region第二个是Zone。一个Region对应多个Zone,再获取region和Zone信息之后,才真正开始加载Eureka Server的具体地址。
eureka.client.serviceUrl.defaultZone属性可以配置多个,Ribbon默认策略会优先访问同客户端下的一个Zone中的实例。

服务获取与服务续约
虚无注册到Eureka Server后,子然后需要一个心跳去续约,方法被剔除
参数:

eureka.instance.lease-renewal-interval-in-seconds=30每隔30秒续约一次
	eureka.instance.lease-expiration-duration-in-seconds=90 90秒不更新被剔除
eureka.client.fetch-registry=true定期更新服务清单,默认开启

Eureka客户端的配置只要分成两个方面
服务注册相关的配置:保活注册中心的地址,服务获取的时间间隔,可用区域等
服务实例相关的配置信息包括服务实例的名称,ip地址,端口号,健康路径检查。
端点配置
springcloud Eureka中默认使用了springboot-actuator模块提供的/info和/health端点

management.context-path=/hello
eureka.instance.statusPageUrlPath=${management.context-path}/info
eureka.instance.healthCheckUrlPath=${management.context-path}/health

或者配置绝对路径
eureka.instance.statusPageUrl=https:// e u r e k a . i n s t a n c e . h o s t n a m e / i n f o e u r e k a . i n s t a n c e . h e a l t h C h e c k U r l = h t t p s : / / {eureka.instance.hostname}/info eureka.instance.healthCheckUrl=https:// {eureka.instance.hostname}/health
eureka.instance.homePageUrl=https://${eureka.instance.hostname}/

原创文章 41 获赞 11 访问量 1498

猜你喜欢

转载自blog.csdn.net/weixin_44038332/article/details/105156887