springcloud(Greenwich.SR2版本)搭建遇到的问题
eureka注册中心
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>xyz.isee.edu</groupId>
<artifactId>edu-euraka</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
文件如下
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#表示是否将自己注册到Eureka Server,默认为true。
register-with-eureka: false
#表示是否从Eureka Server获取注册信息,默认为true。
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka
注意事项
-
eureka
依赖变更增加了netflix
公司的名称原来的依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
现在的依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
我在这里开启了
eureka
的安全验证。即如下security
依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
如果引入安全验证则需要配置访问
eureka
basic认证.以往配置是在application.xml
中增加如下配置:注意这是以往的配置: security: basic: enabled: true spring: profiles: peer1 security: user: name: test password: 123456 roles: USER
现在要在配置类
WebSecurityConfigurerAdapter
中配置@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { /** * @Description: 高版本的丢弃了 * security: * basic: * enabled: true 配置,应该使用以下方式开启 * @Param: [http] * @Return: void */ @Override protected void configure(HttpSecurity http) throws Exception { // Configure HttpSecurity as needed (e.g. enable http basic). http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER); //因为 Spring Security 默认开启了所有 CSRF 攻击防御,这里直接关闭csrf http.csrf().disable();// //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic, // 如果是form方式,不能使用url格式登录 http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); } /** * 配置认证的用户名、密码 * @param auth * @throws Exception */ @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) //admin .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("EUREKA-CLIENT").and() //eureka-security-client .withUser("eureka-security-client").password(new BCryptPasswordEncoder().encode("eureka-security-client")).roles("EUREKA-CLIENT") ; } }
注意这里的用户名和密码。在访问
eureka
是需要填写该用户名和密码。所有服务注册到该eureka
是也需要用到这个用户名和密码
配置中心
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>xyz.isee.edu.config</groupId>
<artifactId>edu-config-server</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- sping cloud 注册服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
文件如下
server:
port: 8888
eureka:
client:
service-url:
#默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
defaultZone: http://${edu.eureka.user}:${edu.eureka.password}@${edu.eureka.host}:${edu.eureka.port}/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
spring:
security:
basic:
enabled: true
user:
name: admin
password: admin
注意事项
-
eureka
依赖变更原来的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
现在的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
注册到
eureka
的配置原来的配置
server: port: 8888 eureka: client: service-url: #默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。 defaultZone: http://localhost:8761/eureka
现在的配置
server: port: 8888 eureka: client: service-url: #默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。 defaultZone: http://${edu.eureka.user}:${edu.eureka.password}@${edu.eureka.host}:${edu.eureka.port}/eureka/
注意这里的
defaultZone
因为eureka
使用了安全认证,所以我们需要在defaultZone
地址中增加${edu.eureka.user}:${edu.eureka.password}
来登录eureka,这里的用户名和密码就是刚才我们配置eureka
的用户名和密码。我们需要在项目的启动参数中配置这些参数:--edu.eureka.user=admin --edu.eureka.password=123456 --edu.eureka.host=localhost --edu.eureka.port=8761
-
实例id改动
原配置:instance: prefer-ip-address: true instance-id: ${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}
现在的配置
instance: prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
`ipAddress`改为了`ip-address`
服务客户端
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>xyz.isee.edu</groupId>
<artifactId>edu-base-domain</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- autoconfig-->
<dependency>
<groupId>xyz.isee.cloud</groupId>
<artifactId>edu-autoconfig</artifactId>
<version>1.0.0</version>
</dependency>
<!-- sdk-->
<dependency>
<groupId>xyz.isee.edu</groupId>
<artifactId>edu-sdk</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- sping cloud 注册服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- 不加redis配置会报如下异常
java.lang.ClassNotFoundException org.apache.commons.pool2.impl.GenericObjectPoolConfig
java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Greenwich默认使用io.lettuce 单独配置jedis来替代 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目启动参数如下
--spring.application.name=serverName
--spring.cloud.client.ip-address=127.0.0.1
--server.port=9002
--spring.profiles.active=development
--edu.eureka.user=admin
--edu.eureka.password=chenghf
--edu.eureka.host=localhost
--edu.eureka.port=8761
注意事项
-
hystrix
依赖变更原来依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
现在的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
-
ribbon
依赖变更原来依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
现在依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
-
eureka
依赖变更,这个刚才说过了原来依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
现在依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
feign
依赖变更原来依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
现在依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
-
另外我发现
Greewich.SR2
版本如果不添加redis
的依赖,会报如下异常:java.lang.ClassNotFoundException org.apache.commons.pool2.impl.GenericObjectPoolConfig
,java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig
,即使我的项目没有使用到redis
也会报这个异常。所以要引入spring-boot-starter-data-redis
并且还要单独引入redis.clients
依赖<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>