您的位置:首页 > 编程语言 > Java开发

Spring Cloud1实现注册中心and服务的注册与消费

2017-07-21 12:32 363 查看
      Spirng Cloud可以很容易的实现注册中心,进行服务的注册,进行服务的消费。国内阿里开源的dubbo也实现了SOA,但是功能没有Spirng Cloud强大,而且dubbo已经不进行维护了,下面搭建一个简单的注册中心,然后进行服务的注册,服务的消费。

一:搭建注册中心

     1,使用IDEA创建一个maven项目,该项目是一个maven的parent,起到管理其他module的作用。在maven项目下创建一个module,该modul是一个spring boot项目,项目名为eureka-server注册中心。创建好了之后,修改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.jack</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.4.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<!--引入服务注册与发现服务的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


 2,给spring boot的启动类,添加注解,表示这是一个注册中心,代码如下:

package com.jack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer  //表示这是一个注册中心
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}


3,修改application.yml配置文件,代码如下:

server:
port: 9090 #端口好
spring:
application:
name: spring-cloud-eureka #应用程序名称
eureka:
client:
register-with-eureka: false #表示是否将自己注册到Eureka Server,默认为true
fetch-registry: false               #表示是否从Eureka Server获取注册信息,默认为true:
serviceUrl:
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
defaultZone: http://localhost:${server.port}/eureka/


     然后启动注册中心,这样一个简单的注册中心就开发完毕了。

    在浏览器输入:http://localhost:9090/地址,可以查看注册中心的信息,如下:



二:开发服务提供者

     在maven项目下在创建一个module的spring boot项目,项目名为eureka-service-provider

  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.jack</groupId>
<artifactId>eureka-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-service-provider</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


2,修改application.yml文件,如下:

spring:
application:
name: spring-cloud-producer  #应用程序名

server:
port: 9094   #端口号

eureka:
client:
serviceUrl:
defaultZone: http://localhost:9090/eureka/  #注册中心地址


3,创建一个Controller提供服务接口给其他应用进行调用,代码如下:

package com.jack.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by jack on 2017/7/20.
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello/provider")
public String helloProvider(){
return "I am is provider ,hello world";
}
}

4,修改该spring boot的启动程序,添加注解,表示可以向注册中心注册服务和发现服务,代码如下:

package com.jack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient  //表示发现服务和向注册中心进行服务注册
public class EurekaServiceProviderApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServiceProviderApplication.class, args);
}
}

    到这里一个服务提供者就开发完毕了,启动这个服务,端口好是9094,启动完毕在注册中心可以看到下面的日志:

2017-07-21 11:32:09.508  INFO 5356 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
2017-07-21 11:32:09.508  INFO 5356 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9090
2017-07-21 11:32:09.511  INFO 5356 --- [           main] com.jack.EurekaServerApplication         : Started EurekaServerApplication in 6.714 seconds (JVM running for 7.13)
2017-07-21 11:32:36.979  INFO 5356 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-21 11:32:36.979  INFO 5356 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-07-21 11:32:37.003  INFO 5356 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 24 ms
2017-07-21 11:33:09.469  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:34:09.470  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:35:09.471  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:36:09.471  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:37:09.471  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:38:09.472  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:39:09.472  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:40:09.472  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:41:09.473  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:42:09.473  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:43:09.474  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:44:09.474  INFO 5356 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2017-07-21 11:44:39.797  INFO 5356 --- [nio-9090-exec-2] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SPRING-CLOUD-PRODUCER/wj:spring-cloud-producer:9094 with status UP (replication=false)
2017-07-21 11:44:40.395  INFO 5356 --- [nio-9090-exec-4] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SPRING-CLOUD-PRODUCER/wj:spring-cloud-producer:9094 with status UP (replication=true)


Registered instance SPRING-CLOUD-PRODUCER/wj:spring-cloud-producer:9094 with status UP (replication=false)
2017-07-21 11:44:40.395  INFO 5356 --- [nio-9090-exec-4] c.n.e.registry.AbstractInstanceRegistry  : Registered instance SPRING-CLOUD-PRODUCER/wj:spring-cloud-producer:9094 with status UP (replication=true)
  表示注册服务成功。



三:开发消费者

      开发消费者,调用其他的应用接口,进行服务消费。在maven项目下再创建一个spring boot项目,项目名我命名为springbootstudy。
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.jack</groupId>
<artifactId>springbootstudy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springbootstudy</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>

<!--feign方式远程调用需要的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

</build>

</project>


2,修改application.yml,如下:

server:
port: 9092 #端口号

spring:
application:
name: spring-cloud-consumer #应用程序名

eureka:
client:
serviceUrl:
defaultZone: http://localhost:9090/eureka/ #注册中心地址



3,修改项目的启动程序代码:

package com.jack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient //启用服务注册与发现
@EnableFeignClients //启用feign进行远程调用
public class SpringbootstudyApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootstudyApplication.class, args);
}

//使用RestTemplate方式调用时需要,定义这个bean
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}


4,使用feign进行远程调用

    1)创建一个接口,调用远程接口:

    package com.jack.rest;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by jack on 2017/7/20.
*/
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
//这个接口要和远程调用的接口一只,参数,接口名,返回类型
@RequestMapping(value = "/hello/provider")
public String helloProvider();
}

   2)创建一个controller,调用上面定义的接口,代码如下:

package com.jack.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by jack on 2017/7/20.
*/
@RestController
public class HelloConroller {
@Autowired
HelloRemote helloRemote;

@RequestMapping(value = "/hello")
public String hello(){
return helloRemote.helloProvider();

}

}


      启动应用程序,通过http://localhost:9092/hello接口调用controller,然后通过调用远程接口获取返回信息,这就实现了一个通过feign方式调用远程接口了。

5,使用RestTemplate调用远程接口

    修改上面的controller,代码如下:

   package com.jack.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Created by jack on 2017/7/20.
*/
@RestController
public class HelloConroller {
@Autowired
HelloRemote helloRemote;
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/hello")
public String hello(){
//return helloRemote.helloProvider();
return "hello,world";
}
//RestTemplate方式调用
@RequestMapping(value = "/hello2")
public String hello2(){
return restTemplate.getForEntity("http://spring-cloud-producer/hello/provider", String.class).getBody();
}
}

     启动该spring boot服务,在浏览器输入http://localhost:9092/hello2,通过RestTemplate调用远程接口,获取返回的数据。

     总结:经过上面简单的例子,一个注册中心,一个服务提供者,一个服务调用的简单例子就实现了,可见Spring Cloud实现不同服务直接相互调用还是很简单的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐