美文网首页
《从搭建微服务框架到深入理解Spring Cloud---(一)

《从搭建微服务框架到深入理解Spring Cloud---(一)

作者: Dufresne_gogogo | 来源:发表于2018-11-26 14:52 被阅读0次

搭建注册中心

导入pom依赖

<?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>sakura</artifactId>
 <groupId>com.warm-sun</groupId>
 <version>1.0.0</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
​
 <artifactId>registry-center</artifactId>
 <packaging>jar</packaging>
 <name>registry-center</name>
 <description>微服务注册中心</description>
 <!-- 引入spring boot的依赖 -->
​
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.8</java.version>
 </properties>
​
 <dependencies>
 <!--注册中心选择eureka 也可以使用 zookeeper consul 这里自己扩展-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 <version>2.0.2.RELEASE</version>
 </dependency>
 <!--security 框架-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-security</artifactId>
 <version>2.0.1.RELEASE</version>
 </dependency>
 <!-- 注册中心-->
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
 <version>2.0.2.RELEASE</version>
 </dependency>
 <!--web 框架-->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <exclusions>
 <!--排除 tomcat容器-->
 <exclusion>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 </exclusion>
 </exclusions>
 </dependency>
 <!--undertow容器 基于内存 性能秒杀tomcat-->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-undertow</artifactId>
 <version>2.0.4.RELEASE</version>
 </dependency>
 </dependencies>
​
 <!-- 添加spring-boot的maven插件 -->
 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <configuration>
 <finalName>${project.name}</finalName>
 </configuration>
 </plugin>
 <plugin>
 <groupId>com.spotify</groupId>
 <artifactId>docker-maven-plugin</artifactId>
 <version>0.4.12</version>
 <configuration>
 <imageName>${registry.url}/${project.name}:0.0.1</imageName>
 <dockerHost>${docker.url}</dockerHost>
 <dockerDirectory>${project.basedir}</dockerDirectory>
 <resources>
 <resource>
 <targetPath>/</targetPath>
 <directory>${project.build.directory}</directory>
 <include>${project.build.finalName}.jar</include>
 </resource>
 </resources>
 <serverId>docker-hub</serverId>
 <registryUrl>https://index.docker.io/v1/</registryUrl>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

编写bootstrap.yml,配置eureka注册信息

#配置端口号
server:
 port: 2000
​
# 配置eureka 安全验证账户
spring:
 security:
 user:
 name: sakura
 password: Sakura*&^999
 application:
 name: registry-center
 cloud:
 config:
 enabled: false
​
# eureka 配置
eureka:
 instance:
 hostname: localhost
 prefer-ip-address: false
​
 client:
 #只有一个eureka注册中心 不需要自己向自己注册 当服务需要搭建集群的时候 registerwitheurka设置为true
 registerWithEureka: false
 # 表示从其他的配置中心同步数据,默认为true;当前只有一个配置中心,所以设置为false
 fetchRegistry: false
 serviceUrl:
 # 多个地址用,隔开 并相互注册
 defaultZone: http://sakura:Sakura*&^999@${eureka.instance.hostname}:${server.port}/eureka/

 #配置eureka心跳等
 server:
 eviction-interval-timer-in-ms: 4000
 enable-self-preservation: false
 renewal-percent-threshold: 0.9</pre>

配置启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
​
/** 注册中心服务端
 * @author tuxiaolei
 * @date 2018/11/23
 */
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {
 public static void main(String[] args) {
 SpringApplication.run(RegistryApplication.class,args);
 }
}

配置安全登录信息

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
​
/**
 * @author tuxiaolei
 * @date 2018/11/23
 */
@EnableWebSecurity
public class RegistrySecurityConfig extends WebSecurityConfigurerAdapter {
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 http.csrf().disable()
 .authorizeRequests()
 .antMatchers().permitAll()
 .anyRequest()
 //开启表单验证
 .authenticated().and().httpBasic();
 }
}

这样我们的Eureka配置中心就完成了

Eureka 架构

Eureka 官方架构图

首先我们要明白几个概念:

  1. Application Client(Service Consumer) : 服务消费者、应用客户端,在微服务架构中很多微服务既是服务的提供者也是服务的消费者,这种情况下,微服务是需要往注册中心注册的。从注册中心获取到服务提供者列表。

  2. Application Service(Service Provider): 服务的提供者,需要向注册中心注册、续约、下线 ,往注册中心发送心跳

  3. Eureka Server: 注册中心服务端,接收客户端的心跳,默认90秒没接收客户端的心跳下线这个客户端。

  4. Eureka Server: 注册中心客户端,注册客户端信息,方便服务消费者获取到提供者的地址、端口等的。

要理解eureka 首先我们得明白 eureka解决了什么:

我们知道:现在开发已经有传统的单系统开发转变成了实现流行的分布式开发和微服务开发,不同的业务被划分成不同的模块,部署到不同的服务器实例上,对于并发量大的模块(比如:订单模块、团购模块)往往来说要搭建集群,那么我们在服务调用的时候怎么知道我部署的服务在哪个服务器上,同个服务的多个实例我应该调取哪个,哪个服务是健康的,哪个服务宕机了,我们的eureka就很好的解决了这些问题。

Eureka 就像一个智能版的微信,我想跟A聊天的时候,我就去找A的通讯信息,新结识了B,将B的信息存到通讯录里,当B改了头像,那么第一时间我也能知道。只不过我们的Eureka更为强大!

根据上续场景,我们来考虑一下如何实现:

1.服务注册后如何及时发现注册信息

2.服务宕机后怎,如何及时下线服务

3.服务如何做水平扩展,解决性能瓶颈

4服务注册发现后,调用微服务怎么做路由

5.服务发生异常怎么做降级处理

6.注册中心如何实现自身高可用

这里我们先留下悬念,我会在后续的博客中,针对eureka的源码进行解析。

相关文章

网友评论

      本文标题:《从搭建微服务框架到深入理解Spring Cloud---(一)

      本文链接:https://www.haomeiwen.com/subject/itjbqqtx.html