Solo  当前访客:3 登录 注册

喧哗博客-http://blog.xuahua.com

繁华过后的沉寂--技术经验分享
浏览次数: 95,037    文章总数: 91    评论总数: 3
存档:
2018 年 12 月 (6)

visual VM 监听远程内存 提示无法使用 service:jmx:rmi:///jndi/rmi:///jmxrmi

1,启动参数

  

java -Djava.rmi.server.hostname=xxx -Djava.security.policy=jstatd.all.policy -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8888
-Xmx256m -Xms=256m -jar xxx.jar

 

2,需要开启监听的二个随机端口,如果这两个端口访问不了,会提示 

VisualVM 无法使用 service:jmx:rmi:///jndi/rmi:///jmxrmi 连接到XX.xx.xx.xx 

 

如何查看,如下命令

netstat -tupln |grep {要监听java进程id}

3,命令查看内存占用情况分布

jstat -gcutil xxx 200 3

 

 

springcloud config配置中心 Cannot clone or checkout repository: git@git.example.com/xxx/xxx.git

出现此问题的原因有可能是

  1,私钥,公钥问题

比如,我遇到的就是之前私钥是以前生成的OPENSSH 私钥,与现在要求生成RSA 私钥公钥不符

所以,请认准私钥头部的 -----BEGIN RSA PRIVATE KEY-----

 

 2,gitlab仓库,未配置公钥

  可以在  gitlab 右上角,点开头像,settings --左侧栏 ssh-keys 

  把在服务器生成的公钥 id_rsa.pub 如果你另外生成的可能是xxx.pub复制过来

 

3,配置文件私钥配置

  一定要注意格式

  

以上为生产服务器配置使用,

 

另外,生成私钥命令为

ssh-keygen -t rsa -C "你的邮箱" 

 

如果已经存在id_rsa 可以指定另外一个

ssh-keygen -t rsa -C "你的邮箱" -f git

 

springcloud turbine 集群配置

1, 创建程序,修改pom.xml

   

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-hystrix-dashboard</artifactId>
        </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-turbine</artifactId>
        </dependency>

 

2,启动类

    

@EnableTurbine
@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableHystrixDashboard
public class ShineHystrixApplication {
     
    
    public static void main(String[] args) {
        SpringApplication.run(ShineHystrixApplication.class, args);
        System.out.print("++++++++++++++++++++++++熔断器中心启动成功!!++++++++++++++++++++++++");

    }
}

 

3,配置文件

  

server:
  port: 8768
spring:
  application:
    name: server-hystrix
  security:
    user:
      name: admin
      password: shine
eureka:
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8761/eureka/
    fetch-registry: true
    register-with-eureka: true
turbine:
  cluster-name-expression: new String("default")
  combine-host-port: true
##要监控的服务,各服务以,分隔 app-config: PlaceSerProFegin,ContentSerProFegin,MemberSerProFegin management: endpoints: web: exposure: include: hystrix.stream

 

 

 

 

springcloud eureka 开启密码配置,其他应用注册时,提示403

当其他应用注册eureka时,提示403,如下

failure with status code 403; retrying on another server if available

 

原因:

 springcloud2.0以后,密码校验开启了crf。所以把crf关闭即可。

增加配置类

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}

 

重启应用即可。

springcloud config 属性文件中文乱码 有更新!

问题,springcloud config-server服务配置git 存储文件中有包含中文,读取属性文件以后,出现乱码!!

git仓库中的属性文件名 locale_cn.properties

如下所示

shine.login.test=测试项目
shine.login.abc=终于来个Ok


请求 http://localhost:8769/locale/cn

出现乱码。




解决:

1,加载属性文件类,重写

OriginTrackedPropertiesLoader类。

2,自己定义加载属性文件

我采用,自己定义加载属性文件

具体如下:

  

public class MyPropertiesHandler implements PropertySourceLoader {
    @Override
    public String[] getFileExtensions() {
        return new String[]{"properties", "xml"};
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        Map<String, ?> properties = this.loadProperties(resource);
        return properties.isEmpty() ? Collections.emptyList() : Collections.singletonList(new OriginTrackedMapPropertySource(name, properties));
    }

    private Map<String, ?> loadProperties(Resource resource) throws IOException {
        String filename = resource.getFilename();
        return (Map) (filename != null && filename.endsWith(".xml") ? PropertiesLoaderUtils.loadProperties(resource) : loadPro(resource));
    }

    
    private Map<String, ?> loadPro(Resource resource) {
        Map<String, String> proMap = new HashMap<>();
        try {
            InputStream inputStream = resource.getInputStream();
            List<Byte> byteList = new LinkedList<Byte>();
            byte[] readByte = new byte[1024];
            int length;
            while ((length = inputStream.read(readByte)) > 0) {
                for (int i = 0; i < length; i++) {
                    byteList.add(readByte[i]);
                }
            }
            byte[] allBytes = new byte[byteList.size()];
            int index = 0;
            for (Byte soloByte : byteList) {
                allBytes[index] = soloByte;
                index += 1;
            }
            String str = new String(allBytes, "UTF-8");
            String[] strs = StringUtils.splitByWholeSeparator(str, "\n");
            for (String tmp_str : strs) {
                if (StringUtils.isNotBlank(tmp_str)) {
                    String[] tmpstr = StringUtils.splitByWholeSeparator(tmp_str, "=");
                    proMap.put(tmpstr[0], tmpstr[1]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proMap;

    }
}

 

同时,增加配置在 src/resources目录下增加文件夹

META-INF/spring.factories

添加一行代码 

org.springframework.boot.env.PropertySourceLoader=com.shine.configserver.MyPropertiesHandler

 

重启应用,再刷新 http://localhost:8769/locale/cn 即可看到正确的

 

 

 

 

 

 

 

 

 


 

 
















springcloud feign 多文件上传 有更新!

网上也有不同的上传,但是对于多文件上传的解决方案比较少,参考了https://blog.csdn.net/qq_32953079/article/details/8163081

1,导入引用 的文件 

   

<dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

 

2,定义上传接口

 

@FeignClient(value = "PLACESERVICEPROVIDER", configuration = FeignMultipartSupportConfig.class)
public interface FileService {

 @PostMapping(value = "/file/uploadMulti", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResult uploadMulti(@RequestPart(value = "files") MultipartFile[] files) throws ServiceException;
}

@Configuration
public class FeignMultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignEncoder() {
        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
    }
}

public class SpringMultipartEncoder extends FormEncoder {

    public SpringMultipartEncoder() {
        this(new Default());
    }


    /**
     * Constructor with specified delegate encoder.
     *
     * @param delegate delegate encoder, if this encoder couldn't encode object.
     */
    public SpringMultipartEncoder(Encoder delegate) {
        super(delegate);

        MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
        processor.addWriter(new SpringSingleMultipartFileWriter());
        processor.addWriter(new SpringManyMultipartFilesWriter());
    }


    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        // 单MultipartFile判断
        if (bodyType.equals(MultipartFile.class)) {
            MultipartFile file = (MultipartFile) object;
            Map data = Collections.singletonMap(file.getName(), object);
            super.encode(data, MAP_STRING_WILDCARD, template);
            return;
        } else if (bodyType.equals(MultipartFile[].class)) {
            // MultipartFile数组处理
            MultipartFile[] file = (MultipartFile[]) object;
            if (file != null) {
                Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                return;
            }
        }
        // 其他类型调用父类默认处理方法
        super.encode(object, bodyType, template);

    }
}


3,服务端实现接口

  

@RestController
public class FileServiceImpl implements FileService {
   @Override
    public BaseResult uploadMulti(MultipartFile[] files) throws ServiceException{
  //do Something
}

}

 

 

 一般默认的上传文件大小,会限制1m大小,可以增加配置

具体设置参见 这篇文章的设置

 

 

 

 

 

公告

喧哗博客--繁华过后的沉寂--技术经验分享^-^
Copyright (c) 2009-2019, b3log.org & hacpai.com