微服务框架

 Dubbo:分布式服务治理框架,RPC(远程过程调用),容器化的技术。

 SpringCloud:基于http的rest调用。基于SpringBoot提供一套微服务解决方案,包括服务注册与发现,配置中心......

 

服务提供方

服务消费方

注册中心

 

项目规划:

父项目:pom包(parent)。

公共包:jar包(common),提供公共的util,实体类等。

提供者:提供真实的service。

消费者:不提供服务,通过rest调用提供者。

注册中心:Eureka(类似zookeeper,提供者把方法放到注册中心,消费者根据注册中心的方法地址去找提供者提供的方法)

 

具体实现:(先后顺序:个人习惯)

父项目:

  pom文件:

<parent>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath />
</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- spring-boot依赖 -->
<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>

注册中心:eureka

  pom文件:

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

</dependency>

</dependencies>

  application.yml文件(配置文件):

server:
port: 9001
#本身是霸道的,启动服务都要向它注册,包括自己
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
spring:
application:
name: eureka-server

  启动类App:

@SpringBootApplication
//开启eureka服务
@EnableEurekaServer
public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

提供者Provider:

  pom文件:

<dependencies>

<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- 依赖common -->
<dependency>

<groupId>com.springcloud</groupId>
<artifactId>springcloud-common</artifactId>
<version>0.0.1-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- 热部署 -->
<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> 

</dependency>

</dependencies>

  application.yml文件(配置文件):

server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
prefer-ip-address: true
spring:
application:
name: provider

  控制层HelloController:

@RestController
public class HelloController {

@GetMapping("hello")
public Map<String,Object> hello() {

Map<String,Object> map = new HashMap<>();

map.put("msg", "word");

return map;
}

}

  启动类App:

@SpringBootApplication
@EnableEurekaClient
public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

 

要先启动注册中心,再启动提供者。

 

posted on 2018-09-25 22:25  聂鑫磊  阅读(201)  评论(0编辑  收藏  举报