首页
关于这个博客
Search
1
Java 实现Google 账号单点登录(OAuth 2.0)全流程解析
231 阅读
2
Spring AI 无法获取大模型深度思考内容?解决方案来了
202 阅读
3
微信小程序实现页面返回前确认弹窗:兼容左上角返回与右滑返回
91 阅读
4
服务器遭遇 XMRig 挖矿程序入侵排查与清理全记录
66 阅读
5
解决 Mac 版 PicGo 无法打开问题:“已损坏,无法打开” 报错处理指南
37 阅读
Java 核心
框架与中间件
数据库技术
开发工具与效率
问题排查与踩坑记录
程序员成长与思考
前端
登录
Search
标签搜索
java虚拟机
JVM
保姆级教程
Java
Spring AI
SpringBoot
Nginx
WebFlux
Spring
cdn
https
dcdn
网站加速
Tool
图片导出
服务部署
源码解析
单点登录
google
sso
Luca Ju
累计撰写
35
篇文章
累计收到
1
条评论
首页
栏目
Java 核心
框架与中间件
数据库技术
开发工具与效率
问题排查与踩坑记录
程序员成长与思考
前端
页面
关于这个博客
搜索到
6
篇与
的结果
2025-08-18
从零开始:Java 服务部署与 HTTPS 访问全攻略
前言在现代 Web 应用开发中,将 Java 服务部署到服务器并通过 HTTPS 安全访问是一项基础且重要的技能。本文将详细记录从环境准备到最终实现 HTTPS 访问的完整过程,适合新手开发者参考学习。一、JDK 环境安装Java 服务运行依赖 JDK 环境,我们选择安装开源免费的 OpenJDK 17 版本。1. 更新系统索引包首先需要更新 Ubuntu 系统的软件包索引,确保能获取到最新的软件版本信息:sudo apt update2. 安装 OpenJDK 17执行以下命令安装 OpenJDK 17:sudo apt install openjdk-17-jdk安装过程中系统会自动处理依赖关系,无需额外操作。3. 验证 JDK 安装结果安装完成后,通过以下命令验证是否安装成功:java -version如果安装成功,会显示类似如下的版本信息:openjdk version "17.0.x" 20xx-xx-xx OpenJDK Runtime Environment (build 17.0.x+xx-Ubuntu-1ubuntux) OpenJDK 64-Bit Server VM (build 17.0.x+xx-Ubuntu-1ubuntux, mixed mode, sharing)4. 配置 JAVA_HOME 环境变量为了让系统和其他工具能正确识别 JDK 位置,需要配置 JAVA_HOME 环境变量:echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ~/.bashrc source ~/.bashrc执行完成后,JAVA_HOME 环境变量将在每次登录时自动生效。二、Java 服务打包与部署完成 JDK 环境配置后,接下来需要将开发好的 Java 服务打包并上传到服务器。1. 服务打包使用 Maven 工具对 Java 项目进行打包,推荐通过 IDEA 的 Maven 插件操作:在 IDEA 右侧的 Maven 面板中找到项目展开 Lifecycle 选项双击 package 命令执行打包操作打包成功后,会在项目的 target 目录下生成对应的 JAR 文件2. 上传服务到服务器将打包好的 JAR 文件通过 SFTP 工具(如 Termius)或 scp 命令上传到服务器的指定目录,例如/opt/java/services/目录。3. 启动 Java 服务通过以下命令启动 Java 服务:nohup java -jar /opt/java/services/your-service.jar &其中&符号表示让服务在后台运行。如果需要更完善的服务管理,建议配置 systemd 服务。三、Nginx 配置实现 HTTPS 访问为了实现 HTTPS 访问并优化请求转发,我们需要配置 Nginx 作为反向代理服务器。1. 创建或编辑 Nginx 配置文件执行以下命令创建新的 Nginx 配置文件:sudo nano /etc/nginx/sites-available/your_domain.conf如果你已有配置文件,直接编辑对应文件即可。2. 配置 HTTPS 与反向代理在配置文件中添加以下内容,实现 HTTP 到 HTTPS 的跳转以及请求转发:server { listen 80; server_name yourdomain.com; # 替换为你的实际域名 # 将所有HTTP请求强制跳转至HTTPS return 301 https://$host$request_uri; } server { listen 443 ssl; server_name yourdomain.com; # 替换为你的实际域名 # SSL证书配置(请替换为你的证书实际路径) ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # SSL安全优化配置 ssl_protocols TLSv1.2 TLSv1.3; # 支持的TLS协议版本 ssl_prefer_server_ciphers on; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"; ssl_session_cache shared:SSL:10m; # SSL会话缓存设置 ssl_session_timeout 10m; # SSL会话超时时间 # 将所有 /app 开头的请求转发到Java服务的9999端口 location /app/ { proxy_pass http://127.0.0.1:9999; # 传递原始请求信息(HTTPS环境关键配置) proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 重要:传递HTTPS协议信息 # 超时设置(根据实际服务需求调整) proxy_connect_timeout 300s; proxy_read_timeout 300s; } # 其他路径处理 location / { # 可以根据需求配置为返回404或指向其他服务 return 404; } }3. 启用 Nginx 配置创建符号链接将配置文件添加到 sites-enabled 目录,使 Nginx 能够识别该配置:sudo ln -s /etc/nginx/sites-available/your_domain.conf /etc/nginx/sites-enabled/4. 检查并重启 Nginx配置完成后,先检查配置文件是否有语法错误:sudo nginx -t如果显示 "nginx: configuration file /etc/nginx/nginx.conf test is successful",说明配置无误,可以重启 Nginx 使配置生效:sudo systemctl restart nginx四、验证 HTTPS 访问完成以上所有步骤后,通过浏览器访问https://yourdomain.com/app/user/login(替换为你的实际域名和接口路径),如果能正常访问并获得服务响应,说明整个部署过程成功完成。常见问题排查如果无法访问 HTTPS 服务,先检查服务器防火墙是否开放 443 端口证书路径错误会导致 Nginx 启动失败,需确保证书文件存在且权限正确Java 服务未启动或端口被占用会导致 502 错误,可通过netstat -tlnp查看端口占用情况配置文件修改后未重启 Nginx 会导致配置不生效,记得每次修改后重启服务通过本文的步骤,你已经成功实现了 Java 服务的部署和 HTTPS 访问配置。在实际生产环境中,还可以根据需求添加服务监控、日志管理等功能,进一步提升服务的稳定性和可维护性。
2025年08月18日
16 阅读
0 评论
0 点赞
2023-05-25
java使用POI导出Excel单元格为数字类型
第一版开发的时候,所有的单元格都是文本类型,由于需求方需要导出Excel可以直接使用函数计算,所以需要改动一下,将导出的Excel设置为数值类型。创建XSSFCellStyle// 此处设置数据格式 XSSFDataFormat df = wb.createDataFormat(); // 创建单元格样式 XSSFCellStyle numberStyle = wb.createCellStyle(); numberStyle.setFillForegroundColor((short) 1); //设置要添加表背景颜色 numberStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //solid 填充 numberStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //文字水平居中 numberStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//文字垂直居中 numberStyle.setBorderBottom(BorderStyle.THIN); //底边框加黑 numberStyle.setBorderLeft(BorderStyle.THIN); //左边框加黑 numberStyle.setBorderRight(BorderStyle.THIN); // 有边框加黑 numberStyle.setBorderTop(BorderStyle.THIN); //上边框加黑 // 注意#,##0.00_ 后面有一个空格 numberStyle.setDataFormat(df.getFormat("#,##0.00_ ")); //为单元格添加背景样式 sheet=setTitleStyle(sheet,rowNum,colNum,style1);创建单元格,并且设置XSSFCellStyle。private static XSSFSheet setTitleStyle(XSSFSheet sheet,int rowNum,int colNum,XSSFCellStyle style){ for (int i = 0; i < rowNum; i++) { //需要行表格 Row row = sheet.createRow(i); //创建行 row.setHeight((short)600); for (int j = 0; j < colNum; j++) {//需要列 row.createCell(j).setCellStyle(style); } } return sheet; }在为单元格赋值的时候,将数据转换为Double类型。
2023年05月25日
8 阅读
0 评论
0 点赞
2023-04-14
使用Apache POI 设置单元格中文字方向
前几天遇到了一个需求,需要使用Apache POI导出Excel,并且还需要实现单元格合并和文字竖向展示的功能。最终结果是这个样子介绍一下合并单元格和文字竖向展示的实现方法。1、合并单元格只要知道需要合并单元格的行号和列号就可以//创建工作簿 XSSFWorkbook workBook = new XSSFWorkbook(); //创建一个sheet XSSFSheet sheet = workBook.createSheet(); CellRangeAddress balanceCell = new CellRangeAddress(0, 4, 1, 1); sheet.addMergedRegion(balanceCell);2、文字竖向显示获取到需要竖向显示的单元格,然后设置单元格样式,设置Rotation属性 //创建工作簿 XSSFWorkbook workBook = new XSSFWorkbook(); //创建一个sheet XSSFSheet sheet = workBook.createSheet(); CellStyle directionStyle = workBook.createCellStyle(); directionStyle.setRotation((short)255); XSSFRow row = sheet.getRow(0); XSSFCell cell = row.getCell(4); cell.setCellStyle(directionStyle);
2023年04月14日
12 阅读
0 评论
0 点赞
2020-05-25
MyBatis分页助手报错java.util.ArrayList cannot be cast to com.github.pagehelper.Page
最近在使用MyBatis时遇到了这问题原本可以正常使用分页,但是当我添加了一个查询条件时突然报出了这个错误,后来终于找到了原因。因为PageHelper.startPage(pageNum,pageSize)方法 只会对靠近的第一个查询语句进行分页。结果我新添加的查询条件又进行了一个select查询,所以会产生这个错误如:PageHelper.startPage(pageNum, pageSize, true);XXXMapper.xxxxPage<> page= (Page<>)TestMapper.query( );改为:XXXMapper.xxxxPageHelper.startPage(pageNum, pageSize, true);Page<> page= (Page<>)TestMapper.query( ); 或者:PageHelper.startPage(pageNum, pageSize, true);Page<> page= (Page<>)TestMapper.query( );XXXMapper.xxxx这样就不会报这个错了。 还是因为自己对这个分页助手不够了解呀。。。
2020年05月25日
1 阅读
0 评论
0 点赞
2020-04-11
写给新同学的SpringBoot教程 — 高级篇
写给新同学的SpringBoot教程 — 高级篇一、添加缓存1、使用SpringBoot自带缓存功能1、基本使用在主类上添加注解@EnableCaching在业务方法上添加@Cacheable(cacheNames = "xxx")注解这样即可实现基础的缓存功能基本流程:@Cacheable:方法执行之前,先去查询Cache(缓存组件),按照CacheNames指定的名字获取,(CacheManager先获取相应的缓存),第一次获取缓存如果没有Cache组件会自行创建去Cache中查找缓存的内容,使用一个key,默认就是方法的参数key是按照某种策略生成的,默认hi使用keyGenerator生成的。默认使用SimplekeyGenerator生成key: 如果有一个参数,key=参数的值 如果没有参数,key=new SimpleKey(); 如果有多个参数,key=new SimpleKey(params);没有查到缓存就去调用目标方法;方法查询结束后,将目标方法返回的结果放入到缓存中。总结: @Cacheable标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,如果缓存中没有,就执行方法并将结果放入到缓存中;以后再来调用就可以直接将缓存中的数据进行返回。核心:使用CaCheManager【ConCurrentMapCacheManager】按照名字得到Cache【ConcurrentMapCache】组件key是使用keyGenerator生成的,默认是SimpleKeyGenerator主类:@SpringBootApplication @MapperScan(value = "com.jcq.cache.mapper") @EnableCaching //这个注解不要忘了 public class Springboot09CacheApplication { public static void main(String[] args) { SpringApplication.run(Springboot09CacheApplication.class, args); } }业务方法@Service public class EmployeeService { @Resource private EmployeeMapper employeeMapper; @Cacheable(cacheNames = "emp",key = "#root.methodName+'['+#id+']'") public Employee getEmp(Integer id){ return employeeMapper.selEmpByid(id); } }2、其他属性keyGenerator:key的生成器,可以自己指定key的生成器组件的id ,key/keyGenerator二选一使用。cacheManager:指定缓存管理器,或者cacheResolver指定获取解析器。condition:满足条件是放入缓存。unless:满足条件是不放入缓存,与condition正好相反。sync:默认为false,true为开启异步缓存。若开启异步,unless注解无效3、其他注解@CachePut注解:方法运行完后,将数据放入缓存中@CachePut与@Cacheable的区别: 后者在方法运行前先到缓存总查询,前者在方法执行后,将数据放入缓存中。注意: 只有保持数据的key相同,才能实现数据的同步更新。 @CachePut(cacheNames = "emp",key = "#employee.id") public Employee update(Employee employee){ employeeMapper.updEmp(employee); return employee; }@CacheEvict:清除缓存数据allEntries:清空所有缓存,默认为falsebeforeInvocation:在方法执行前清空缓存,默认为false注意:保持数据key同一,才可以保持数据同步。 //测试清除缓存中的数据 @CacheEvict(cacheNames = "emp",key = "#id") public void delEmpCache(Integer id){ System.out.println("清除缓存"); }@Caching:可以将多个注解组合//测试Caching的使用 @Caching(cacheable = { @Cacheable(value = "emp", key = "#lastName") }, put = {@CachePut(value = "emp", key = "#result.id"), @CachePut(value = "emp",key = "#result.email") } ) public Employee selBylastName(String lastName){ return employeeMapper.selBylastName(lastName); } 2、整合redis1、redis命令常用命令网址2、步骤在pom.xml中引入下列依赖<!--引入redis启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>配置文件如下#配置redis spring.redis.host=192.168.91.128SpringBoot把对redis的操作封装在RedisTemplate 和 StringRedisTemplate中@SpringBootTest class Springboot09CacheApplicationTests { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private EmployeeService employeeService; @Test public void testRedis(){ //添加字符串 // stringRedisTemplate.opsForValue().append("msg","helloworld"); //添加list // stringRedisTemplate.opsForList().leftPushAll("mylist","1","2","3"); //取出list // String str = stringRedisTemplate.opsForList().leftPop("mylist"); // System.out.println(str); Employee emp = employeeService.getEmp(1); //添加对象 redisTemplate.opsForValue().set("emp",emp); }3、添加自定义组件,将对象转化为json字符串存储@Configuration public class MyRedisTemplate { @Bean public RedisTemplate<Object, Employee> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Employee> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); template.setDefaultSerializer(new Jackson2JsonRedisSerializer(Employee.class)); return template; } }而在服务启动过程中,使用的CacheManager依旧默认按照序列化的方式进行对象存储,所以还需要添加组件来达到将对象用json存储的目的 /** * 缓存管理器 */ @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { //初始化一个RedisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); //设置CacheManager的值序列化方式为json序列化 RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer(); RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair .fromSerializer(jsonSerializer); RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(pair); //设置默认超过期时间是30秒 defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); //初始化RedisCacheManager return new RedisCacheManager(redisCacheWriter, defaultCacheConfig); }二、SpringBoot与消息1、使用docker安装rabbitmq//下载rabbitmq docker pull rabbitmq:3-management //运行 docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq id默认用户名密码均为:guest转换器类型:direct:只有key相同的队列才可以收到消息fanout:任意队列都能收到消息atguigu.# : #表示0或多个词.news : 表示0或一个单词2、SpringBoot整合Rabbitmq1、基础使用配置文件:**spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.host=192.168.91.128 #默认为5672,不需要配置 #spring.rabbitmq.port=5672测试代码: /** * 单播 */ @Test void contextLoads() { Map<String, Object> map = new HashMap<>(); map.put("name", "张三"); map.put("age", 18); rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map); }但是这样发送的消息,会以默认的序列化方式进行存储。效果接收消息: /** * 接收消息 */ @Test void recieve(){ Object o = rabbitTemplate.receiveAndConvert("atguigu.news"); System.out.println(o.getClass()); System.out.println(o); }自定义组件,使用json格式进行对象转化@Configuration public class MyAMQPConfig { @Bean public MessageConverter messageConverter(){ return new Jackson2JsonMessageConverter(); } }广播测试代码 /** * 广播 */ @Test void sendMsg(){ rabbitTemplate.convertAndSend("exchange.fanout","",new Book("SpringMVC","张佳明")); }2、消息监听主类上添加@EnableRabbit注解@EnableRabbit @SpringBootApplication public class SpringBoot10AmqpApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot10AmqpApplication.class, args); } }业务代码如下:@Service public class BookService { @RabbitListener(queues = "atguigu.news") public void bookLinstener(Book book){ System.out.println("接收到消息"+book); } }3、使用AmqpAdmin管理工具 @Autowired private AmqpAdmin amqpAdmin; @Test void createRabbit(){ //创建转换器 // amqpAdmin.declareExchange(new DirectExchange("AmqpAdmin.exchange")); // System.out.println("转换器创建成功"); //创建消息队列 // amqpAdmin.declareQueue(new Queue("AmqpAdmin.news")); // System.out.println("消息队列创建成功"); //创建绑定规则 amqpAdmin.declareBinding(new Binding("AmqpAdmin.news", Binding.DestinationType.QUEUE, "AmqpAdmin.exchange", "AmqpAdmin.haha", null)); System.out.println("创建绑定成功"); //删除队列 amqpAdmin.deleteQueue("AmqpAdmin.news"); System.out.println("删除队列"); amqpAdmin.deleteExchange("AmqpAdmin.exchange"); System.out.println("删除转换器"); }三、SpringBoot与检索ElasticSearch1、搭建环境1、下载es docker pull elasticsearch 2、启动命令 [root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 5acf0e8da90b 7d33661ebabc32621173c4c9648047016e6ede9fc625c51340692736b7fdf170 其中: ES_JAVA_OPTS="-Xms256m -Xmx256m" :-Xms 初始堆内存大小 -Xmx 最大使用内存 9200:es默认web通信使用9200端口 9300:当分布式情况下es多个节点之间通信使用9300端口 文档地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index-doc.htmlSpringBoot默认支持两种技术和ElasticSearch交互一、Jest(默认不生效)需要导入jest的工具包(io.serachbox.chlient.JestClient)需要将默认的SpringBoot-data-Elasticsearch注释掉导入jest依赖但是jest已经过时,不演示了二、SpringDataClient节点信息clusterNodes,clusterNameElasticsearchTemplate来操作es编写一个ElasticsearchRepository子接口来操作es因版本问题,暂时没有解决SpringBoot2.X版本整合es的方法,使用会报错四、SpringBoot与任务1、异步任务1、在主类添加@EnableAsync注解2、在业务层添加@Async注解即可实现异步任务@Async @Service public class AsyncService { public void testAsync(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("方法执行完毕"); } }2、定时任务cron表达式:Cron表达式的格式:秒 分 时 日 月 周 年(可选)。字段名 允许的值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日 1-31 , - * ? / L W C月 1-12 or JAN-DEC , - * / 周几 1-7 or SUN-SAT , - * ? / L C # 年(可选字段) empty 1970-2099 , - * /具体的用法:https://www.cnblogs.com/lazyInsects/p/8075487.html @Scheduled(cron = "0 * * * * MON-FRI") 周一到周五,没整分运行一次 // @Scheduled(cron = "0/4 * * * * MON-FRI") 没整分,间隔4秒执行 @Scheduled(cron = ) public void testAsync(){ System.out.println("方法执行完毕"); } }在方法上加上@Scheduled注解在主类添加@EnableSchelding注解主类代码如下//@EnableAsync @EnableScheduling @SpringBootApplication public class SpringBoot12TaskApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot12TaskApplication.class, args); } } 3、邮件任务配置文件spring.mail.username=1036658425@qq.com spring.mail.password=kersbnhzzuhfbdih spring.mail.host=smtp.qq.com@Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { //创建简单邮件对象 SimpleMailMessage massage = new SimpleMailMessage(); //设置主题 massage.setSubject("简单测试"); //设置内容 massage.setText("测试简单邮件发送"); //设置收件方 massage.setTo("594082079@qq.com"); //设置发送方 massage.setFrom("1036658425@qq.com"); mailSender.send(massage); } @Test public void test02() throws MessagingException { //测试复杂邮件发送 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setSubject("复杂发送"); helper.setText("<b style='color:red'>测试复杂邮件发送</b>",true); helper.addAttachment("a.jpg",new File("E:\\笔记\\SpringBoot\\springboot高级\\SpringBoot高级.assets\\image-20200310195943079.png")); helper.addAttachment("b.jpg",new File("E:\\笔记\\SpringBoot\\springboot高级\\SpringBoot高级.assets\\image-20200310201323308.png")); helper.setTo("594082079@qq.com"); helper.setFrom("1036658425@qq.com"); mailSender.send(mimeMessage); } 五、SpringBoot与安全1、业务部分代码编写配置类@EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { //添加认证功能 @Override protected void configure(HttpSecurity http) throws Exception { // super.configure(http); http.authorizeRequests().mvcMatchers("/").permitAll() .mvcMatchers("/level1/**").hasRole("VIP1") .mvcMatchers("/level2/**").hasRole("VIP2") .mvcMatchers("/level3/**").hasRole("VIP3"); //开启登录认证页面 http.formLogin(); //开启自动配置注销功能 http.logout().logoutSuccessUrl("/"); //访问/logout 清除session //开启自动配置记住我功能 http.rememberMe(); } //添加登录授权功能 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // super.configure(auth); auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2") .and().withUser("lisi").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3") .and().withUser("wangwu").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP3","VIP1"); }整合页面部分,因版本问题,暂时没有效果,先不放图片了六、分布式1、Dubbo下载镜像[root@localhost ~]# docker pull zookeeper [root@localhost ~]# docker run --name zk01 -p 2181:2181 --restart always -d bbebb888169c 1、提供者将服务提供者注册到注册中心导入相关依赖Dubbo和Zkclient<!--引入Dubbo服务启动器--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>2. 在主类添加启用Dubbo的注解 @EnableDubbo @SpringBootApplication public class ProviderTicketApplication { public static void main(String[] args) { SpringApplication.run(ProviderTicketApplication.class, args); } }如果不加此注解,消费者会报空指针异常 2. 配置application配置文件 dubbo.application.name=provider dubbo.registry.address=zookeeper://192.168.91.128:2181 dubbo.scan.base-packages=com.jcq.ticket.service使用@service发布服务@Component @Service public class TickerServiceImpl implements TickerService { @Override public String getTicker() { return "《厉害了,我的国》"; } }2、消费者导入相关依赖<!--引入Dubbo服务启动器--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>配置文件dubbo.application.name=consumer dubbo.registry.address=zookeeper://192.168.91.128:2181 server.port=8081引用服务需要把提供者的包个接口复制到项目中,且保持路径同一加下来就是编写业务代码了import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; @Service public class UserServiceImpl { @Reference private TickerService tickerService; public String getTicker(){ return tickerService.getTicker(); } }注意不要到错包了,会导致空指针异常。2、SpringCloud1、注册中心1、首先在主类添加@EnableEurekaServer注解@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }2、添加配置文件server: port: 8761 #修改服务端口 eureka: client: fetch-registry: false #不从注册中心获取服务 register-with-eureka: false #不在服务中心注册服务 service-url: defaultZone: http://localhost:8761/eureka/ #访问路径 instance: hostname: eureka-server #主机名称3、登录页面查看是否启动成功网址:http://localhost:8761/2、提供者1、编写配置文件server: port: 8001 spring: application: name: provider-ticket eureka: instance: prefer-ip-address: true #注册服务时,使用服务的ip地址 service-url: defaultZone: http://localhost:8761/eureka/2、正常编写业务代码及控制器代码一个项目注册多个服务直接将不同端口好的项目进行打包,使用cmd命令分别执行即可项目打包后的图片管理页面显示内容入下3、消费者1、编写配置文件server: port: 8200 eureka: instance: prefer-ip-address: true service-url: defaultZone: http://localhost:8761/eureka/ spring: application: name: consumer-user2、主类代码:@EnableDiscoveryClient //开启到注册中心查找服务 @SpringBootApplication public class ConsumerUserApplication { public static void main(String[] args) { SpringApplication.run(ConsumerUserApplication.class, args); } /** * 给容器添加组件 * 可以发送远程http请求 * @return */ @LoadBalanced //开启负载均衡功能 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }3、业务代码@RestController public class UserController { @Autowired private RestTemplate restTemplate; @GetMapping("/buy") public String buyTicket(String name){ //发送远程请求,请求注册中心的业务方法。地址直接写注册中心中的服务名,不需写端口号 String ticket = restTemplate.getForObject("http://PROVIDER-TICKET/get", String.class); return name+"购买了"+ticket; } }七、热部署只需要到如SpringBoot提供的依赖即可<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>更改代码后,手动CTRL+F9 重新编译,即可实现热部署
2020年04月11日
4 阅读
0 评论
0 点赞
1
2