一:注册中心 + 服务提供者(简单)
注册中心本身就可以是服务提供者,如果有需求可以分开。
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
@EnableFeignClients3:配置
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