解决办法有的很多种,下面的都是我亲测的,有时候发现第二次使用另一种方式也可以解决,所以内容仅供参考
1- jpa解决org.hibernate.lazyinitializationexception无法初始化代理 - 没有会话
#配置文件添加懒加载
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
2- PageRequest类使用的时候利用新的一个对象的时候报错,所这个方法过时了,解决方法
PageRequest pageRequest = new PageRequest(int,int); //提示方法过时,就调用静态方法,不用新的对象了,看源码就知道了
PageRequest pageRequest = PageRequest.of(0, 5);
3-无法找到@EnableEurekaClient注解依赖,请参考
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入依赖,注意不是服务端-->
<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-netflix-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4-java.net.ConnectException:连接被拒绝:连接解决办法
它可能不是错误,就是尤里卡不知道自己此时作为服务器呢还是服务的发布或者调用者,所以就抛异常,此时如果明确是作为服务器为替他请求提供调用或者注册,那么可以在YML或者特性文件说明,比如
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
port: 8761
5-If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
启动类修改为:@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
6-maven一个模块调用另一个的service模块如果打包或者添加了依赖仍然不能跟自动导包或者注入,
启动类修改为
@SpringBootApplication(scanBasePackages="com.demo.cloud.palyerinfo")
7.使用原生ribbon API ConfigurationManager 在ribbon单独使用时找不到这个类,或者使用ConfigurationManager报错Cannot access org.apache.commons.configuration.event.EventSource请添加依赖
//package com.example.first.ribbon.client;
import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//一个原生ribbon请求的负载均衡的简单例子
public class ClientApp {
public static void main(String[] args) throws Exception {
/**
* 设置请求服务器
*/
// 1、设置请求的服务器
ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
// 2、 配置规则处理类
//本示例略,先默认使用其默认负载均衡策略规则
//通过ClientFactory获取指定名称client
AbstractLoadBalancerAwareClient client = (AbstractLoadBalancerAwareClient) ClientFactory.getNamedClient("my-client");
System.out.println("client is " + client.getClass());
// 3、 创建实例请求
HttpRequest request = HttpRequest.newBuilder().uri("/findPerson/666").build();
// 4、发 送 10 次请求到服务器中
for (int i = 0; i < 6; i++) {
HttpResponse response = (HttpResponse) client.executeWithLoadBalancer(request);
String result = response.getEntity(String.class);
System.out.println(result);
}
}
}
<!--单独使用ribbon原生PAI(注意,这是配套的)-->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-httpclient</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
<version>2.3.0</version>
</dependency>
调用restletmaven报错,是因为maven没有提供,所以需要单独配置地址
<!--单独使用restlet(注意仓库地址)-->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.3.10</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>2.3.10</version>
</dependency>
<!--restlet调用rest依赖地址仓库-->
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>
报错Exception in thread "main" java.lang.IllegalStateException: Method reHello not annotated with HTTP method type (ex. GET, POST)
就是接口方法的注解不要混用在一个接口里面
/**
* feign自带注解
* hello表示将要被代理对象的请求
* @return
*/
@RequestLine("GET /hello")
String sayHello();
/**
* 第三方注解
* 和原生的一样,但是不能混用
* @return
*/
@GET
@Path("/hello")
String reHello();
@EnableFeignClients无法解析
<!--配置feign(注意版本Greenwich.RELEASE的版本写法)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Feign调用报错The bean 'XXX.FeignClientSpecification', defined in null, could not be registered....的解决办法
spring:
main:
allow-bean-definition-overriding: true
不行的话就在配置文件加上
spring.main.allow-bean-definition-overriding=true
spring cloud2 hystrix没有/actuator/hystrix.stream路径解决
在启动类上加入
@EnableHystrix
@EnableCircuitBreaker
@EnableHystrixDashboard
在启动类中加入
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
本文提供了Spring Cloud开发中常见的问题及解决办法,包括JPA、PageRequest类使用、Eureka客户端配置、ConnectException异常处理、嵌入式数据库配置、Maven模块间服务调用、Ribbon API使用、Restlet调用配置、Feign注解冲突、Hystrix流监控等,覆盖了从微服务架构搭建到具体技术实现的多个方面。
1187

被折叠的 条评论
为什么被折叠?



