博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud快速搭建
阅读量:6982 次
发布时间:2019-06-27

本文共 4821 字,大约阅读时间需要 16 分钟。

  hot3.png

一:注册中心 + 服务提供者(简单)

注册中心本身就可以是服务提供者,如果有需求可以分开。

1:pom.xml

4.0.0
com.cloudTest.com
eureka_server
0.0.1-SNAPSHOT
jar
eureka_server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
Edgware.RELEASE
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
server:  port: 8762eureka:  instance:     hostname: localhost  client:     #默认true,设置为false时只作为注册中心使用,不提供服务    registerWithEureka: true    fetchRegistry: true    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:  application:    name: baseServer

 

package com.cloudtest.com.eureka_server;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;//启动一个服务注册中心@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}

 

服务:

一个正常的接口

@RestController@RequestMapping("/test")public class TestConstroller {    @RequestMapping(value = "/say", method = RequestMethod.GET)    public String say(@RequestParam(value = "name") String name){        return "hello" + name;    }

 

二:消费者

本例使用Feign作为服务消费者

 

依赖:

spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-feign
org.glassfish.jersey.core
jersey-server
com.thoughtworks.xstream
xstream
1.4.10
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import

 

2: main方法增加注解

@EnableEurekaClient

@EnableFeignClients

3:配置

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8762/eureka/spring:  application:    name: 名字

4:服务接口配置

package com.lcamtech.aiads.dts.mobileController;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value = "baseServer")public interface TestInterface {	 @RequestMapping(value = "/test/say",method = RequestMethod.GET)	 String rfc(@RequestParam(value = "name") String name);}

 

5:服务调用

直接请求本地配置好的接口方法

@RestController@RequestMapping("/test")public class TestController extends BaseController{	@Autowired	private TestInterface testInterface;		@RequestMapping(value = "/rpc", method = RequestMethod.GET)	public String rpc(@RequestParam(value = "name") String name){		String msg = testInterface.rfc(name);		return msg;	}	}

 

更多原理请自行百度。

1:一些坑,启动的时候可能报错,请检查Dalston.RC1版本,或是某些依赖包有缺失

2:启动注册中心以后,可以使用其他项目作为消费者,在注册中心进行服务注册,在分布式环境中可以通过相同的spring.appliaction.name来为一个服务注册多个实例。

 

本篇只有最简单的快读搭建,方便上手,更多内容请参考:

传送门:http://blog.csdn.net/forezp/article/details/70148833

 

转载于:https://my.oschina.net/dlam/blog/1586940

你可能感兴趣的文章
Java面试通关要点汇总集
查看>>
从源码角度理解Handler、Looper、MessageQueue之间关系
查看>>
Bitmap 比你想的更费内存 | 吊打 OOM
查看>>
为你揭秘小程序音视频背后的故事......
查看>>
自定义侧边快速索引栏
查看>>
一种自动化检测 Flash 中 XSS 方法的探讨
查看>>
基于环信sdk实现简单即时聊天
查看>>
Java基础-Synchronized原理
查看>>
大道至简,阿里巴巴敏捷教练的电子看板诞生记
查看>>
华山论剑之浅谈iOS的生产线 工厂模式
查看>>
浅谈javascript异步发展历程
查看>>
在vscode使用editorconfig的正确姿势
查看>>
你用过 PropTypes 的这些类型检查么?
查看>>
枚举的使用示例
查看>>
runC爆严重漏洞影响Kubernetes、Docker,阿里云修复runC漏洞的公告
查看>>
力扣(LeetCode)146
查看>>
Understanding HBase and BigTable 译文
查看>>
Java™ 教程(泛型、继承和子类型)
查看>>
如何优雅的构建排序公式
查看>>
React手稿之 React-Saga
查看>>