【前面的话】SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它配置简单,上手快,而且生态成熟,便于应用。但是它对SpringBoot有很强的依赖,需要有一定基础,但是SpringBoot俩小时就可以入门。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。另外这是SpringCloud的版本为Greenwich.SR2,JDK版本为1.8,SpringBoot的版本为2.1.7.RELEASE。
壹、新建父工程
- 新建一个Maven父工程lovincloud,便于版本管理,然后删除src文件夹
- 添加pom依赖和SpringCloud和SpringBoot的版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<groupId>com.eelve.lovincloud</groupId> <artifactId>lovincloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging>
<name>lovincloud</name> <url>http://maven.apache.org</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
贰、添加一个注册中心
在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。
- 新建一个子工程lovin-eureka-server作为服务的注册中心
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <parent> <artifactId>lovincloud</artifactId> <groupId>com.eelve.lovincloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-server</artifactId> <packaging>jar</packaging> <name>eurekaserver</name> <version>0.0.1</version> <description>eureka服务端</description>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
- 然后在启动类上添加@EnableEurekaServer注解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.eelve.lovin;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer @SpringBootApplication public class LovinEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(LovinEurekaServerApplication.class,args); } }
|
- eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring: application: naem: lovineurkaserver server: port: 8881 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
|
叁、添加一个服务消费端
- 新建一个子工程lovin-eureka-server作为服务的注册中心
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <parent> <artifactId>lovincloud</artifactId> <groupId>com.eelve.lovincloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-client</artifactId> <packaging>jar</packaging> <name>eurekaclient</name> <version>0.0.1</version> <description>eureka的一个消费端</description>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
- 然后在启动类上添加@EnableEurekaClient注解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.eelve.lovin;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication @EnableEurekaClient public class LovinEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(LovinEurekaClientApplication.class,args); } }
|
1 2 3 4 5 6 7 8 9
| server: port: 8801 spring: application: name: lovineurkaclient eureka: client: serviceUrl: defaultZone: http://localhost:8881/eureka/
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.eelve.lovin.controller;
import com.eelve.lovin.config.ServerConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@Autowired ServerConfig serverConfig;
@RequestMapping("hello") public String hello(){ return serverConfig.getUrl()+"###"+ HelloController.class.getName(); } }
|
肆、分别启动注册中心的服务端和客户端
访问localhost:8881查看结果
到这里我们可以已经看到已经成功将客户端注册到服务端了,然后我们访问测试接口
可以看到已经访问成功,至此Eureka的搭建已经完成。
伍、加入安全配置
在互联网中我们一般都会考虑安全性,尤其是管理服务的注册中心,所以我们可以用spring-boot-starter-security来做安全限制
- 给lovin-eureka-server添加spring-boot-starter-security的pom依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| spring: application: naem: lovineurkaserver security: basic: enabled: true user: name: lovin password: ${REGISTRY_SERVER_PASSWORD:lovin} server: port: 8881 eureka: instance: hostname: localhost metadata-map: user.name: ${security.user.name} user.password: ${security.user.password} client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration; 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;
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http.authorizeRequests() .antMatchers("/css/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); super.configure(http); } }
|
- 给lovin-eureka-client添加spring-boot-starter-security的pom依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 8801 spring: application: name: lovineurkaclient security: basic: enabled: true user: name: lovin password: ${REGISTRY_SERVER_PASSWORD:lovin} eureka: client: serviceUrl: defaultZone: http://lovin:lovin@localhost:8881/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health metadata-map: user.name: lovin user.password: lovin
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); } }
|
- 另外为了测试多客服端注册,我们可以修改再给客户端新建一个配置文件,然后开启IDEA的多节点运行,如下图所示勾选Allow parallel run
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.eelve.lovin.config;
import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component;
import java.net.InetAddress; import java.net.UnknownHostException;
@Component public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> { private int serverPort;
public String getUrl() { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return "http://"+address.getHostAddress() +":"+this.serverPort; }
@Override public void onApplicationEvent(WebServerInitializedEvent event) { this.serverPort = event.getWebServer().getPort(); }
}
|
可以看到,这里已经让我们输入用户名和密码了,说明spring-boot-starter-security已经配置成功,这时我们输入配置的用户名:lovin和密码:lovin
这里我们可以看到已经成功了,那么到这里Eureka的配置已经全部成功了。