下面写一个简单的demo验证下eureka,实现服务注册、服务发现。
一、单节点:
1、api:
封装其他组件需要共用的dto
2、eureka-service服务注册中心:
(1)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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.cloud</groupId>
<artifactId>myspringcloud-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- springBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
(2)application.properties:在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以需要禁用它的客户端注册行为
server.port=1111
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
(3)启动类:
package com.demo.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MyEurekaServerApplication {
public static void main(String args[]){
SpringApplication.run(MyEurekaServerApplication.class,args);
}
}
启动后,访问http://localhost:1111/:
就可以从后台监控服务了,(是不是比dubbo搭建zk注册中心方便多了) ,此时还没有服务注册过来,可以看到application下是空的。
3、eureka-client注册服务提供者:将原有的springboot工程改造,注册到eureka注册中心
(1)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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo.cloud</groupId>
<artifactId>myspringcloud-eureka-client</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- springBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.demo.cloud.api</groupId>
<artifactId>myspringcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
(2)application.properties:
server.port=2222
server.context-path=/myService
spring.application.name=my-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#mybatis
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy
mybatis.mapper-locations=classpath*:Mapper/*Mapper.xml
(3)dao、service、Mapper:mybatis持久化部分,此处省略
(4)controller:
package com.demo.cloud.controller;
import com.demo.cloud.dto.AuthorityDTO;
import com.demo.cloud.dto.UserDTO;
import com.demo.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/user")
@RestController
public class UserApiController {
@Autowired
private UserService userService;
@Value("${server.port}")
private String port;
@RequestMapping("/findByUserName")
public UserDTO findByUserName(String username){
return userService.findByUserName(username);
}
@RequestMapping("/getAllUsers")
public List<UserDTO> getAllUsers(){
System.out.println(port);
return userService.getAllUsers();
}
@RequestMapping("/getAuthortiesByUserId")
public List<AuthorityDTO> getAuthortiesByUserId(Integer userId){
return userService.getAuthortiesByUserId(userId);
}
@RequestMapping("/addUser")
public void addUser1(@RequestBody UserDTO userDTO){
userService.addUser(userDTO);
}
}
(5)启动类:
package com.demo.cloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.demo.cloud.dao")
public class MyEurekaClientApplication {
public static void main(String args[]){
SpringApplication.run(MyEurekaClientApplication.class,args);
}
}
注意:这里使用的是@EnableEurekaClient注解,也可以使用@EnableDiscoveryClient注解,
@EnableEurekaClient是Eureka特有的注解,用于启动Eureka客户端。当使用Eureka作为注册中心时,就推荐使用@EnableEurekaClient注解,应用启动后会自动注册到Eureka Server,并完成服务治理。
@EnableDiscoveryClient是Spring Cloud通用的注解,可以与Eureka、Consul等多种注册中心对接。当我们的微服务同时需要与多个注册中心集成时,就需要使用@EnableDiscoveryClient注解。
可以说,@EnableEurekaClient是@EnableDiscoveryClient的一个具体实现,如果项目中注册中心只使用Eureka,那么使用@EnableEurekaClient更加方便和简单。但如果要切换到其他的注册中心,改动较大。
启动项目,再浏览下http://localhost:1111/:
可以看到注册成功了。
二、高可用部署:
1、先部署eukera-service的高可用
2、再部署eureka-client的高可用:连接注册中心的配置项改为多个,以逗号分隔,然后将该组件部署多个节点即可
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://xxx.xx.xxx:1111/eureka/