笔记学习地址:http://blog.csdn.net/forezp/article/details/70148833
笔记内容皆摘抄自以上博客并亲自验证。
在此感谢原博主分享~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://blog.csdn.net/forezp/article/details/70148833
本文出自方志朋的博客
环境:
IDE: IDEA
jdk8
A、服务的注册与发现
1、Spring Eureka 服务端
实现SpringCloud的服务注册与发现功能。
/*1、Springboot项目搭建
使用start.spring.io或IDE工具创建项目或者直接创建*/
1、创建空项目,删除src作为父项目
2、创建module》Spring Initializr》输入项目artifictID等信息,项目名称我设置为eurekaServer》选择Cloud Discovery,勾选Eureka Server》Finish
3、完成后,找到SpringBoot的启动类,添加注解
/** * eureka服务端,服务注册中心 * * */ @EnableEurekaServer @SpringBootApplication public class EurademoApplication { public static void main(String[] args) { SpringApplication.run(EurademoApplication.class, args); } }
4、进入resource目录,对配置文件application.yml进行配置(可以配置properties或yml)如下:
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
5、启动SpringBoot,若启动未报错,进入 http://localhost:8761 查看服务端。
2、Spring Eureka client 服务注册端
1、创建和上面相同的module
2、boot启动类,为了方便controller也放在里面
/** * 服务注册方 */ @SpringBootApplication @EnableEurekaClient @RestController public class EuraclientApplication { public static void main(String[] args) { SpringApplication.run(EuraclientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; } }
3、yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: service-hi