SpringCloud学习之手把手教你用IDEA搭建入门项目(三)
本篇博客是承接上一篇《手把手教你用IDEA搭建SpringCloud入门项目(二)》,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目,本篇博客主要是贴出项目的代码部分,方便读者更好的实战操作搭建一个属于自己的SpringCloud项目
1)项目框架大体如下

2)编写eureka-server模块的启动类
package com.xu.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3)把properties文件改成yml类型文件,并且编辑配置文件如下

application.yml文件编辑如下:
server:
port: 8761
eureka:
instance:
hostname: localhost #表示这个注册中心在本地
client:
registerWithEureka: false #注册中心不注册自己,默认是注册自己
fetchRegistry: false
serviceUrl:
#拿到上面的主机地址和端口地址,配置一个注册中心
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4)启动注册中心服务,看到控制台输出这些信息,则表示注册中心服务启动成功

5)注册中心服务启动后,在浏览器输入地址:http://localhost:8761/,应该可以看到这个界面,这里可以看到在注册中心注册的一些服务

6)服务提供者模块service-provider其他代码如下
ServiceProviderApplication.java
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}

修改application.properties文件为application.yml,并编辑如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #这里是告诉他注册中心地址
server:
port: 8763
spring:
application:
name: service-hello #这里给服务取一个帅气的名字
这里只提供了一个RestApi接口服务,后面服务消费者会调用这个测试API服务:
HelloWorldController.java
package com.xu.serviceprovider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi " + name + ", i am from port: " + port;
}
}

启动这个模块

然后刷新注册中心地址http://localhost:8761/,就可以看到他已经被注册到注册中心了

7)服务消费者模块service-consumer其他代码如下:
ServiceConsumerApplication.java
package com.xu.serviceconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}

service-consumer模块的application.yml如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
其他代码如下:
HelloService.java
package com.xu.serviceconsumer.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name)
{
return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name, String.class);
}
}
HelloControler.java
package com.xu.serviceconsumer.controller;
import com.xu.serviceconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name) {
return helloService.hiService(name);
}
}
启动这个模块,成功后如下:

刷新注册中心地址:http://localhost:8761/,可以看到一个新的服务又被注册到注册中心了

打开浏览器,在一个新的窗口中输入地址:http://localhost:8764/hi?name=admin,如果成功则返回如下信息则表示成功调用了服务提供者的服务接口(根据端口可以知道,8763端口是服务提供者端口)

本篇博客到此为止,如有问题请留言大家一起讨论学习,诸君共勉!
===============================================================================
如果您觉得此文有帮助,可以小小打赏一下,持续更新更有动力哟!


SpringCloud学习之手把手教你用IDEA搭建入门项目(三)的更多相关文章
- SpringCloud学习之手把手教你用IDEA搭建入门项目(二)
本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(一)>,不清楚的请到我的博客空间查看后再看本篇博客 1)先创建一个Eureka服务注册中心模块,用来作为服务的 ...
- SpringCloud学习之手把手教你用IDEA搭建入门项目(一)
SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 2)创建一个空模板的maven项目,用 ...
- SpringCloud学习之手把手教你用IDEA搭建入门项目【番外篇】(一)
之前的文章里,我曾经搭建了一个Springcloud项目,但是那个时候我对于SpringCloud架构的很多组件不甚清楚,只是通过查找资料然后动手稀里糊涂的把一个项目成功搭建起来了,其中有很多不合理和 ...
- 手把手教你用vue-cli搭建vue项目
手把手教你用vue-cli搭建vue项目 本篇主要是利用vue-cli来搭建vue项目,其中前提是node和npm已经安装好,文章结尾将会简单提到一个简单的例子.使用vue-cli搭建项目最开始我也是 ...
- 手把手教你用webpack3搭建react项目(开发环境和生产环境)(一)
开发环境和生产环境整个配置源码在github上,源码地址:github-webpack-react 如果觉得有帮助,点个Star谢谢!! (一)是开发环境,(二)是生产环境. 一.首先创建packag ...
- 手把手教你用vue-clic3搭建vue-element-admin项目
下载element-admin框架 点击该地址:https://github.com/PanJiaChen/vue-element-admin 用git clone https://github.co ...
- 沉淀,再出发——手把手教你使用VirtualBox搭建含有三个虚拟节点的Hadoop集群
手把手教你使用VirtualBox搭建含有三个虚拟节点的Hadoop集群 一.准备,再出发 在项目启动之前,让我们看一下前面所做的工作.首先我们掌握了一些Linux的基本命令和重要的文件,其次我们学会 ...
- 手把手教你认识并搭建Nginx
手把手教你认识并搭建Nginx Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...
- 手把手教你使用 VuePress 搭建个人博客
手把手教你使用 VuePress 搭建个人博客 有阅读障碍的同学,可以跳过第一至四节,下载我写好的工具包: git clone https://github.com/zhangyunchencc/vu ...
随机推荐
- SqlServer查看锁表与解锁
某些情况下,sqlserver的表会被锁住,比如某个会话窗口有数据一直没提交,窗口又没关闭,这时表就会被锁住 其他任何连接查询表数据时都不会返回 这时需要手工杀掉产生死锁的会话ID,才能恢复正常 查看 ...
- jackson处理json
原文连接 工具下载: jackson-core-2.2.3.jar 核心jar包,下载地址 jackson-annotations-2.2.3.jar 该包提供Json注解支持,下载地址 jackso ...
- imput placeholder 移动端不居中问题
input{ height: 100%; } input::-webkit-input-placeholder { display: flex; align-items: center; line-h ...
- 0106 springMVC REST风格
markdown 印象笔记语法练习带快捷键的 加粗 快捷键 cmd+b 斜体 cmd+i 分割线 cmd+u 编号列表: cmd+shift+o 无编号列表 cmd+shift+u 待办事项 cmd+ ...
- NO11 SSH故障排查思路和netstat命令
本章知识相关考试:1.企业场景面试题:Linux系统如何优化?2.企业场景面试题:SSH服务连不上,如何排查?记住回答技巧: 1 ping 2 telnet 客户端ssh工具:SecureCRT,x ...
- HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)
题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数. 分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数. 1.将序列从头到尾扫一遍,用query求每个数字之前有多 ...
- 使用Kickstart+pxe自动化安装部署无人值守的linux服务器
Kickstart+pxe Kickstart无人职守安装RHEL5过程分享(详细图解版) 启动应用有:httpd.dhcpd.named.xinetd 无人职守自动批量安装linux系统超详细 参考 ...
- windows中git输错密码后不能修改问题
坑位 当使用git做代码管理的时候,如果仓库地址地选用的是https,在初始拉取代码时,需要输入账号和密码,如果不小心输错了,后续一直会验证失败,无法再重新更正账号信息 Why 因为git为了不让你每 ...
- 四、JavaScript之<script>标签的使用
一.代码如下 二.运行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 142-PHP trait的定义和使用
<?php trait info{ //定义trait static function getinfo(){ return '这是一个'.__CLASS__.'类.<br />'; ...