目录
一站式微服务架构SpringCloud-Netflix注册中心Eureka
创建父项目springcloud-dome-parent
父项目向子项目提供统一的依赖
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-parent</artifactId>
<version>0.0.1</version>
<description>springcloud父项目</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<!--聚合项目-->
<modules>
<module>../springcloud-dome-commons</module>
<module>../springcloud-dome-eureka</module>
<module>../springcloud-dome-producer</module>
<module>../springcloud-dome-consumer</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
创建通用项目springcloud-dome-commons
创建通用项目来向其他项目提供工具类或实体类
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父项目-->
<parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-parent</artifactId>
<version>0.0.1</version>
<relativePath>../springcloud-dome-parent/pom.xml</relativePath>
</parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-commons</artifactId>
<version>0.0.1</version>
<name>springcloud-dome-commons</name>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
创建实体类(略)
创建注册中心项目springcloud-dome-eureka
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父项目-->
<parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-parent</artifactId>
<version>0.0.1</version>
<relativePath>../springcloud-dome-parent/pom.xml</relativePath>
</parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-eureka</artifactId>
<version>0.0.1</version>
<name>springcloud-dome-eureka</name>
<description>springcloud注册中心</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件
# 设置Eureka Server WEB控制台端口,自定义
server.port=8761
# 设置spring应用命名,可以自定义,非必要
eureka.instance.hostname=localhost
#是否将自己注册到Eureka-Server中,默认的为true
eureka.client.register-with-eureka=false
#是否从Eureka-Server中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
#指定注册服务地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
#禁用自我保护机制
#eureka.server.enable-self-preservation=false
启动类添加@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudDomeEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDomeEurekaApplication.class, args);
}
}
创建生产者项目springcloud-dome-producer
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父项目-->
<parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-parent</artifactId>
<version>0.0.1</version>
<relativePath>../springcloud-dome-parent/pom.xml</relativePath>
</parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-producer</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>springcloud-dome-producer</name>
<description>springcloud消费者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--添加通用项目依赖-->
<dependency>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-commons</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<!--包含了src/main/java目录下的所有xml资源配置文件-->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<!--包含了src/main/resources目录下的所有资源配置文件,有别的格式的配置文件时,不要写成**/*.xml-->
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
配置文件
server.port=8090
#数据库连接配置
spring.datasource.url=jdbc:mysql://47.110.157.82:3307/dome
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#打开所有web访问端点
management.endpoints.web.exposure.include=*
spring.application.name=springcloud-producer-01
#每2秒向服务端发送一次心跳,证明自己存活
eureka.instance.lease-renewal-interval-in-seconds=2
#告诉服务端,10秒之内没有发送心跳,就代表故障了
eureka.instance.lease-expiration-duration-in-seconds=10
#以IP作为链接
eureka.instance.prefer-ip-address=true
#实例名称
eureka.instance.instance-id=springcloud-producer-01
#Eureka链接地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
创建DAO层service层(略)
创建controller
@RestController
public class ServiceController {
@Autowired
UserInfoService userInfoService;
@RequestMapping("get")
@ResponseBody
public List<UserInfo> producer(){
UserInfoExample userInfoExample = new UserInfoExample();
UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
criteria.andIdEqualTo(3);
List<UserInfo> userAll = userInfoService.findUserAll(userInfoExample);
return userAll;
}
}
启动类添加@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudDomeProducerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDomeProducerApplication.class, args);
}
}
创建消费者项目springcloud-dome-consumer
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父项目-->
<parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-parent</artifactId>
<version>0.0.1</version>
<relativePath>../springcloud-dome-parent/pom.xml</relativePath>
</parent>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-consumer</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>springcloud-dome-consumer</name>
<description>springcloud消费者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>springcloud-dome</groupId>
<artifactId>springcloud-dome-commons</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件
server.port=8081
spring.application.name=springcloud-consumer-01
#每2秒向服务端发送一次心跳,证明自己存活
eureka.instance.lease-renewal-interval-in-seconds=2
#告诉服务端,10秒之内没有发送心跳,就代表故障了
eureka.instance.lease-expiration-duration-in-seconds=10
#以IP作为链接
eureka.instance.prefer-ip-address=true
#实例名称
eureka.instance.instance-id=springcloud-consumer-01
#Eureka链接地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
创建RestConfig提供restTemplate
@Configuration
public class RestConfig {
@Bean
@LoadBalanced //ribbon实现负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
创建远程调用Controller
@RestController
public class UserInfoController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("getUser")
@ResponseBody
public List<UserInfo> getUser(){
UserInfo[] users = restTemplate.getForObject("http://springcloud-producer-01/get", UserInfo[].class);
List<UserInfo> userList = Arrays.asList(users);
return userList;
}
}
启动类添加@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudDomeConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudDomeConsumerApplication.class, args);
}
}
测试
访问Eureka:http://localhost:8761/
通过消费者调用生产者
本地Eureka集群搭建
创建三个Eureka项目
eureka8761配置文件
# 设置Eureka Server WEB控制台端口,自定义
server.port=8761
# 设置spring应用命名,可以自定义,非必要
eureka.instance.hostname=localhost
#是否将自己注册到Eureka-Server中,默认的为true
eureka.client.register-with-eureka=false
#是否从Eureka-Server中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
#指定注册服务地址
eureka.client.service-url.defaultZone=http://eureka8762:8762/eureka,http://eureka8763:8763/eureka
eureka8762配置文件
# 设置Eureka Server WEB控制台端口,自定义
server.port=8762
# 设置spring应用命名,可以自定义,非必要
eureka.instance.hostname=localhost
#是否将自己注册到Eureka-Server中,默认的为true
eureka.client.register-with-eureka=false
#是否从Eureka-Server中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
#指定注册服务地址
eureka.client.service-url.defaultZone=http://eureka8761:8761/eureka,http://eureka8763:8763/eureka
eureka8763配置文件
# 设置Eureka Server WEB控制台端口,自定义
server.port=8763
# 设置spring应用命名,可以自定义,非必要
eureka.instance.hostname=localhost
#是否将自己注册到Eureka-Server中,默认的为true
eureka.client.register-with-eureka=false
#是否从Eureka-Server中获取服务注册信息,默认为true
eureka.client.fetch-registry=false
#指定注册服务地址
eureka.client.service-url.defaultZone=http://eureka8761:8761/eureka,http://eureka8762:8762/eureka
本地host文件添加本地映射
host文件路径C:\Windows\System32\drivers\etc
127.0.0.1 eureka8761
127.0.0.1 eureka8762
127.0.0.1 eureka8763
这里我使用的是一个项目,根据启动项目时的参数加载不同的配置文件启动三个项目
--spring.profiles.active=eureka8761
--spring.profiles.active=eureka8762
--spring.profiles.active=eureka8763
启动生产者和消费者项目后查看Eureka集群
测试
服务器Eureka集群部署
1.上传Eurekamu项目的jar包到服务器
2.在服务器hosts文件添加映射
hosts文件路径:/etc/hosts
127.0.0.1 eureka8761
127.0.0.1 eureka8762
127.0.0.1 eureka8763
3.创建一个文件夹用来存储日志
mkdir logs
4.启动项目
java -jar springcloud-dome-eureka-0.0.1.jar --spring.profiles.active=eureka8761 > ./logs/eureka8761 &
java -jar springcloud-dome-eureka-0.0.1.jar --spring.profiles.active=eureka8762 > ./logs/eureka8762 &
java -jar springcloud-dome-eureka-0.0.1.jar --spring.profiles.active=eureka8763 > ./logs/eureka8763 &
5.测试
Eureka 安全认证
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置文件设置访问用户和密码
spring.security.user.name=liu
spring.security.user.password=123456
新建Security配置类,csrf设置为不可用
@EnableWebSecurity
@Configuration
public class EurekaSecurityConfig extends WebSecurityConfigurerAdapter {
//认证
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
启动注册中心,访问Eureka,这是需要登录
客户端服务需要在连接Eureka地址中加入登录Security的用户和密码
eureka.client.service-url.defaultZone=http://liu:123456@localhost:8761/eureka