本篇博客是承接上一篇《手把手教你用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. XmlBeanDefinitionReader

  2. pandas包 —— drop()、sort_values()、drop_duplicates()

    一.drop() 函数 当你要删除某一行或者某一列时,用drop函数,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. 1.命令: df.drop() 删除行:df.d ...

  3. Java虚拟机05.2(内存分配)

    jdk1.7中堆内存分为:年轻代+老年代+永久代.但是永久代有作为非堆内存存在,也就是说堆内存的大小应该为年轻代+老年代.在tomcat容器中,如果jsp页面过多可能出现永久代溢出.通常栈溢出都是程序 ...

  4. java 接口 2.19

    接口中所有的方法都是抽象的和public的,所有的属性都是public,static,final的.

  5. angular.js开发 将多页面开发成单页面

    用angulara.js做单页面开发时,由于不能跨页面取数据,又需要传参,可以采用:$scope.step=0/1来解决这个问题,设置初始值为想要的页面即可.

  6. 05.swoole学习笔记--定时器

    <?php //循环执行的定时器 swoole_timer_tick(,function($timer_id){ echo "执行 $timer_id \n"; }); sw ...

  7. CSS样式表——格式与选择器

    1.分类 1)内联(写在标签内部) style="样式" 控制精确,代码重用性差 2)内嵌(在<head></head>中) <style type= ...

  8. 学会C#

    一.字符串插值 (String Interpolation) C# 6之前我们拼接字符串时需要这样 var Name = "Jack"; var results = "H ...

  9. android studio3.1 添加闪屏页面(启动欢迎界面)(例子简单无BUG)

    截图 启动页的 activity_splash.xml  我用了一张图片自己添加吧 <?xml version="1.0" encoding="utf-8" ...

  10. LeetCode160 相交链表(双指针)

    题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...