SPRING INITIALIZR构建工程

spring boot 可通过SPRING INITIALIZR构建项目
访问SPRING INITIALIZR官网,填写项目相关信息后,生成项目。

将下载的项目解压,打开idea,file-->new-->project from existing sources。

import project选择maven项目,jdk选择1.8或以上,一直next即可。

spring cloud server 提供服务注册

1.修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.Eureka.server</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.Eureka.server</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- spring boot test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

2.通过SPRING INITIALIZR构建工程后,修改配置文件application.properties,我这里使用的是application.yml,两者只在格式上存在差别。

3.启动类application.java添加@EnableEurekaServer注解。

@EnableEurekaServer
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

4.访问http://localhost:8000/

spring cloud client 提供服务

1.同样创建项目后,修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.eureka.client</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.eureka.client</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

2.application.yml

eureka:client:serviceUrl:#server对应的地址defaultZone: http://localhost:8000/eureka/instance:hostname: localhost
server:port: 8089
spring:application:#应用名name: service-helloworld
ribbon:eureka:enabled: true

其中关于ribbon的学习来自于 https://www.jianshu.com/p/f86...
3.ApplicationClient.java

@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationClient {public static void main(String[] args) {SpringApplication.run(ApplicationClient.class, args);}}

4.HelloController.java

@RestController
public class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello "+name+",hahahaha!";}
}

5.访问http://localhost:8089/hello?name=w

此时再看服务端,多了service-helloworld这个应用。

spring cloud consumer 使用服务

1.新建项目,修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.eureka.consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.eureka.consumer</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- spring boot test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

2.application.propertites【这里特意没有用yml】

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3.ApplicationConsumer.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationConsumer {public static void main(String[] args) {SpringApplication.run(ApplicationConsumer.class, args);}}

4.创建接口类,HelloTest.java

//这里的name指定客户端的应用名
@FeignClient(name= "service-helloworld")
public interface HelloTest {//这里requestMapping与客户端相同@RequestMapping(value = "/hello")public String hello(@RequestParam(value = "name") String name);
}

5.ConsumerController.java

@RestController
public class ConsumerController {@AutowiredHelloTest helloTest;@RequestMapping("/hello/{name}")public String index(@PathVariable("name") String name) {return helloTest.hello(name);}}

6.访问http://localhost:9001/hello/w

此时,服务端存在两个应用

spring cloud eureka服务注册和调用相关推荐

  1. eureka集群只注册一个_一、Spring Cloud Eureka服务注册中心

    Sping boot Eureka是服务中心,管理各种服务功能包括服务的注册.发现.熔断.负载.降级等.Spring Cloud Eureka 是对Netflix公司的Eureka的二次封装. 在du ...

  2. Spring Cloud Eureka(二)注册一个服务的提供者

    Spring Cloud Eureka(二)注册一个服务的提供者 注册一个服务的提供者 在上一篇中,当启动项目并访问localhost:1111时,发现该注册中心还没有注册任何服务.所以现在来搞一个服 ...

  3. 如何优化Spring Cloud微服务注册中心架构?

    作者: 石杉的架构笔记 1.再回顾:什么是服务注册中心? 先回顾一下什么叫做服务注册中心? 顾名思义,假设你有一个分布式系统,里面包含了多个服务,部署在不同的机器上,然后这些不同机器上的服务之间要互相 ...

  4. Spring cloud实现服务注册及发现

    为什么80%的码农都做不了架构师?>>>    服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访 ...

  5. Spring Cloud之服务注册与发现机制

    一.复杂与简单并存 1.背景: 到底是复杂好还是简单好,这是一个没有答案的问题,也是一个哲学问题.见仁见智啦.事物整体肯定是向复杂化方向发展,但是向人们呈现时应尽量简单化.用一句话来说就是:功能复杂化 ...

  6. Spring Cloud 学习笔记(一) 之服务治理模块Spring Cloud Eureka 搭建注册中心

    2019独角兽企业重金招聘Python工程师标准>>> 关于springboot的学习请参考前面的文章 接下来我们会开启一系列关于springcloud的学习文章. 一.概念 首先我 ...

  7. 天荒地老修仙功-第六部:Spring Cloud Eureka——服务发现

    文章目录 前言 1.Eureka 1.1.Spring Cloud Netflix 1.2.样例项目结构 1.3.服务注册中心 1.4.客户端(服务提供者) 1.5.高可用 1.5.1.Region ...

  8. 用ZooKeeper做为注册中心搭建基于Spring Cloud实现服务注册与发现

    前提: 先安装好ZooKeeper的环境,搭建参考:http://www.cnblogs.com/EasonJim/p/7482961.html 说明: 可以再简单的理解为有两方协作,一个是服务提供这 ...

  9. 【Spring Cloud Alibaba】Spring Cloud Alibaba 服务注册与发现实践

    1. 简介 服务注册与发现是微服务架构体系中最关键的组件之一.如果尝试着用手动的方式来给每一个客户端来配置所有服务提供者的服务列表是一件非常困难的事,而且也不利于服务的动态扩缩容.Nacos Disc ...

最新文章

  1. java如何监控cpu耗时_超级干货:3个性能监控和优化命令讲解
  2. python的第三方库-Python第三方库安装和卸载
  3. JAVA-WBE——spring security 3.2 认证-学习笔记2
  4. win7系统电脑自动重启解决方法
  5. 200818C链表的查询插入删除
  6. Codeforces 338 D. GCD Table
  7. 结构体数组排列_学习RTOS(3)数据结构
  8. 计算机网络(七)-物理层设备
  9. 《大厂内部资料》Redis 性能优化的 13 条军规!全网首发
  10. mysql order by按照汉字拼音进行排序
  11. 什么是Adam/ReLU/YOLO?这里有一份深度学习(.ai)词典
  12. git rebase和 merge的区别
  13. hp打印机驱动android,惠普打印机驱动
  14. win10环境下python3如何使用PyV8
  15. 微信小程序实现授权登录及退出
  16. 屏蔽广告效果好的手机浏览器,这3款你一定不要错过
  17. 京东2023年Q1财报预测:短期增速承压,收入和净利润预测被下调
  18. 菩提本无树,明镜亦非台,本来无一物,何处惹尘埃。
  19. 网络基础(四) — QUIC协议
  20. 巧学五笔打字-----1小时可学会,不用死背字根 [图片]

热门文章

  1. C语言 指针声明和定义 - C语言零基础入门教程
  2. nodejs轻量服务器后端
  3. android怎么换小米系统更新,miui8怎么更新 miui8更新升级方法汇总
  4. mysql表里插不进去数据_Oracle数据中表值插不进去问题(转)
  5. 如何快速的学习html5,高效快速学习HTML5的技巧
  6. android xml 列表展示,Android中ListView实现展示列表数据
  7. php浏览服务器某一文件夹内容,php删除web服务器中指定目录下的指定格式的文件...
  8. Java输出小明算对多少题目_2014年Java方向C组第十题
  9. 计算机存储技术及应用,计算机数据安全存储技术及应用
  10. php 传递类名,php 对象和数组序列化 serialize()返回字符串方便存储和传递 unserialize()反序列化 不丢失类型和结构...