服务治理
服务治理是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动话注册和发现。
搭建服务注册中
首先,创建一个基础SpringBoot工程,命名为lemon-eurkea(命名自己随意),并在pom.xml中引入需要的依赖内容,代码如下:
<parent>
<groupId>com.lemon</groupId>
<artifactId>lemon</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>lemon-eureka</artifactId>
<packaging>jar</packaging>
<description>springcloud注册中心</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这里我是依赖了一个父工程,父工程的pom配置为:
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lemon-version>0.0.1</lemon-version>
<springboot.version>1.5.2.RELEASE</springboot.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<druid.version>1.0.26</druid.version>
<fastjson.version>1.2.24</fastjson.version>
<pagehelper.spring.boot.version>1.2.0</pagehelper.spring.boot.version>
<joda.time.version>2.9.9</joda.time.version>
<shiro.version>1.3.2</shiro.version>
<jsch.version>0.1.54</jsch.version>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Springboot 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.spring.boot.version}</version>
</dependency>
<!-- druid阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>${jsch.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
剩下的例子也会在现在的工程基础上进行添加,所有的例子可以在github中查看, 点击这里。
通过@EnableEurekaServer
注解启动一个服务注册中心提供给其他应用进行对话。
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaApplication.class)
.web(true).run(args);
}
}
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties
配置文件中增加如下信息:
spring.application.name=lemon-eureka
server.port=1001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.register-with-eureka:由于应用为注册中心,因此设置为false,代表不向注册中心注册自己。
eureka.client.fetch-registry: 由于注册中心的职责就是维护服务实例,他并需要去检索服务,所以设置为false。
简单的注册中心完成了,启动应用并访问http://localhost:1001/,可以看到如下图所示的Eureka的信息面板