为什么80%的码农都做不了架构师?>>>
一:注册中心 + 服务提供者(简单)
注册中心本身就可以是服务提供者,如果有需求可以分开。
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><groupId>com.cloudTest.com</groupId><artifactId>eureka_server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka_server</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Edgware.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</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>${spring-cloud.version}</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>
server:port: 8762eureka:instance: hostname: localhostclient: #默认true,设置为false时只作为注册中心使用,不提供服务registerWithEureka: truefetchRegistry: trueserviceUrl: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
@SpringBootApplication
public 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作为服务消费者
依赖:
<repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><!-- 下面两个是有可能报错时根据问题添加,注意版本 -->
<dependency><groupId>org.glassfish.jersey.core</groupId><artifactId>jersey-server</artifactId></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.10</version></dependency><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>
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{@Autowiredprivate 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