本篇博客是承接上一篇《手把手教你用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. 真香的flex弹性布局

    如何实现一个左中右的布局 在flex出现之前 #box{ color: white; } #left{ float: left; width: 30%; background-color: red; ...

  2. 01.swoole学习笔记--TCP服务器

    1.安装swoole扩展 2.网络调试助手进行调试 <?php //创建服务器 $host='192.168.10.31'; $port=; //$model='SWOOLE_PROCESS'; ...

  3. mysql使用的坑

    一: mysql默认是安装记录的物理顺序取数据的,如果不加order by 排序,可能得不到预期的结果. (1) 获取 两个时间点的 id  (很快) $sql = ‘select id from a ...

  4. python中groupby函数详解(非常容易懂)

    一.groupby 能做什么? python中groupby函数主要的作用是进行数据的分组以及分组后地组内运算! 对于数据的分组和分组运算主要是指groupby函数的应用,具体函数的规则如下: df[ ...

  5. poj 1027 Ignatius and the Princess II全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  6. js数据类型 判断

    1. js数据类型(两种数据类型) 基本数据类型:null undefined number boolean symbol string 引用数据类型: array object null: 空对象 ...

  7. 小米手机收到升级鸿蒙OS提示?官方回应

    虽然尚未得到官方确认,但华为“鸿蒙”OS已经成为网络热门话题,在机圈引发热议. 本周,互联网上出现了显示为MIUI 10手机被锁定,屏幕上出现“小米将于2020年9月15日全面停止服务,届时您所有设备 ...

  8. SYSTEMTIME 获取日期之差

    #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  9. SpringBoot+Jpa测试自增时报错Springboot-jpa Table 'sell.hibernate_sequence' doesn't exist

    解决办法: @GeneratedValue(strategy = GenerationType.IDENTITY) 如图所示:

  10. 五十六、SAP中LVC表格的常用布局属性LVC_S_LAYO

    一.LVC_S_LAYO为表格常用的布局属性,包括网格线,宽度自适应,隐藏主键等 二.我们来对比使用前和使用后的表格,这个原始布局风格的表格 三.这个是设置了相关属性的表格