本篇博客是承接上一篇《手把手教你用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搭建入门项目(三)的更多相关文章

  1. SpringCloud学习之手把手教你用IDEA搭建入门项目(二)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(一)>,不清楚的请到我的博客空间查看后再看本篇博客 1)先创建一个Eureka服务注册中心模块,用来作为服务的 ...

  2. SpringCloud学习之手把手教你用IDEA搭建入门项目(一)

    SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 ​ 2)创建一个空模板的maven项目,用 ...

  3. SpringCloud学习之手把手教你用IDEA搭建入门项目【番外篇】(一)

    之前的文章里,我曾经搭建了一个Springcloud项目,但是那个时候我对于SpringCloud架构的很多组件不甚清楚,只是通过查找资料然后动手稀里糊涂的把一个项目成功搭建起来了,其中有很多不合理和 ...

  4. 手把手教你用vue-cli搭建vue项目

    手把手教你用vue-cli搭建vue项目 本篇主要是利用vue-cli来搭建vue项目,其中前提是node和npm已经安装好,文章结尾将会简单提到一个简单的例子.使用vue-cli搭建项目最开始我也是 ...

  5. 手把手教你用webpack3搭建react项目(开发环境和生产环境)(一)

    开发环境和生产环境整个配置源码在github上,源码地址:github-webpack-react 如果觉得有帮助,点个Star谢谢!! (一)是开发环境,(二)是生产环境. 一.首先创建packag ...

  6. 手把手教你用vue-clic3搭建vue-element-admin项目

    下载element-admin框架 点击该地址:https://github.com/PanJiaChen/vue-element-admin 用git clone https://github.co ...

  7. 沉淀,再出发——手把手教你使用VirtualBox搭建含有三个虚拟节点的Hadoop集群

    手把手教你使用VirtualBox搭建含有三个虚拟节点的Hadoop集群 一.准备,再出发 在项目启动之前,让我们看一下前面所做的工作.首先我们掌握了一些Linux的基本命令和重要的文件,其次我们学会 ...

  8. 手把手教你认识并搭建Nginx

    手把手教你认识并搭建Nginx Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor ...

  9. 手把手教你使用 VuePress 搭建个人博客

    手把手教你使用 VuePress 搭建个人博客 有阅读障碍的同学,可以跳过第一至四节,下载我写好的工具包: git clone https://github.com/zhangyunchencc/vu ...

随机推荐

  1. XV6源代码阅读-文件系统

    Exercise1 源代码阅读 文件系统部分 buf.h fcntl.h stat.h fs.h file.h ide.c bio.c log.c fs.c file.c sysfile.c exec ...

  2. Thymeleaf(一)---引入js/css文件

    th:href="@{/static/css/style.css}" th:src="@{/static/js/thymeleaf.js}" index.htm ...

  3. ios之开源

    a http://code.cocoachina.com b http://code4app.com c http://www.oschina.net/ios/codingList/ d github ...

  4. C# 控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  5. Google的搜索API的Delphi封装

    这个东西实现了已经有一段时间了,那个时候谷歌还没有退出中国内地呢!而现在呢,谷歌都退了有一些日子了!紧以此纪念一番! 话说谷歌API,我相信很多人应该都知道!不晓得在实际应用中,用的人多不多(我说的不 ...

  6. UVA - 10382 Watering Grass(几何)

    题意:有一个矩形,n个圆.已知矩形的长宽和圆的半径,问最少需多少个圆将矩形完全覆盖. 分析: 1.首先求圆与矩形的长的交点,若无交点,则一定不能对用最少的圆覆盖矩形有贡献. 2.如果两个圆与矩形相交所 ...

  7. pixi的图片处理

    pixi的图片处理   var texture = PIXI.Texture.fromImage('sprite.png');var sprite = new PIXI.Sprite(texture) ...

  8. web.xml的配置过程中也需要注意顺序问题

    配置WEB.XML的配置文件过程中发现: 直接红叉,鼠标放在红叉出信息如下: cvc-complex-type.2.4.a: Invalid content was found starting wi ...

  9. Node.js 加载静态资源css,js等不显示问题的解决方法

    一,原因 1,没有响应到css等文件 2,响应类型是由文件的后缀名决定 (1)html的请求头 Content-Type : text/html ; charset=utf-8 (2) CSS的请求头 ...

  10. 简单的js队列

    简单的js队列 /** * [Queue] * @param {[Int]} size [队列大小] */ function Queue(size) { var list = []; //向队列中添加 ...