前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用springcloud搭建微服务集成中心

利用springcloud搭建微服务集成中心

作者头像
用户5166330
发布2019-04-16 17:08:12
3880
发布2019-04-16 17:08:12
举报
文章被收录于专栏:帅哥哥写代码帅哥哥写代码
大纲

  • 注册中心
  • 功能服务one
  • 功能服务two

整体目录结构如下

整体目录结构 这里整个功能是一个maven项目,注册中心与功能服务都是maven项目里面的模块。

注册中心

利用eureka搭建服务注册中心模块server-eureka 这是一个空的springboot项目,主要就是把他当做服务注册中心使用 1.springboot运行入口代码

代码语言:javascript
复制
package org.server.eureka;

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

@EnableEurekaServer
@SpringBootApplication
public class App {
  public static void main(String[] args) {
      SpringApplication.run(App.class, args);
  }
}

通过@EnableEurekaServer注解将应用标记为服务注册中心 2.pom配置文件

代码语言:javascript
复制
<?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>
  <packaging>jar</packaging>
  <name>server-eureka</name>
  <description>eureka-server project for Spring Boot</description>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>server-eureka</artifactId>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </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.SR1</spring-cloud.version>
  </properties>

  <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

作者:帅哥哥写代码
链接:https://www.jianshu.com/p/48d2ef887530
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

最主要的是要引入spring-cloud-starter-netflix-eureka-server这个jar包,用于支持以eureka作为服务注册中心 3.eureka服务的基本配置

代码语言:javascript
复制
server:
 port: 8761(指定应用端口)
spring:
 application:
   name: eureka-server(指定应用名称)
eureka:
 instance:
   hostname: localhost
 client:
   register-with-eureka: false (不注册自己)
   fetch-registry: false(不开启检索)
   service-url:
     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

做好这些配置,就完成了。

启动eureka-server,界面如下:

eureka-server管理界面.png 标红的应用中心显示目前没有服务注册到eureka中心

搭建第一个服务应用client-one-eureka

1.springboot入口代码

代码语言:javascript
复制
package org.client.one.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class App 
{
  public static void main(String[] args) {
      SpringApplication.run(App.class, args);
  }
}

两个重要注解@EnableEurekaClient、@EnableFeignClients。 @EnableEurekaClient标记为Eureka客服端(就是标记为Eureka一个服务) @EnableFeignClients开启Feign调用服务的方式。(调用服务有两种方式,一种是ribbon,一种是Feign),我这里想实现的是基于Feign实现client-one-eureka调用client-two-eureka服务方法。 2.pom依赖

代码语言:javascript
复制
<?xml version="1.0"?>
<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>client-one-eureka</artifactId>
  <name>client-one-eureka</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

作者:帅哥哥写代码
链接:https://www.jianshu.com/p/48d2ef887530
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

主要是spring-cloud-starter-netflix-eureka-server的jar和spring-cloud-starter-openfeign的jar。 3.应用的基本配置

代码语言:javascript
复制
server:
 port: 2001 
spring:
 application:
   name: client-two-eureka
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/(这个和server配置的是一致的)
   fetch-registry: true
   register-with-eureka: true

到此第一个应用配置完成。

搭建第二个服务应用client-two-eureka

1.springboot入口代码

代码语言:javascript
复制
package org.client.two.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class App {
  public static void main(String[] args) {
      SpringApplication.run(App.class, args);
  }

  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
      return new RestTemplate();
  }
}

2.pom配置文件

代码语言:javascript
复制
<?xml version="1.0"?>
<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven->4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>cn.ysh</groupId>
      <artifactId>cloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>cn.ysh.cloud</groupId>
  <artifactId>client-two-eureka</artifactId>
  <name>client-two-eureka</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

作者:帅哥哥写代码
链接:https://www.jianshu.com/p/48d2ef887530
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

spring-cloud-starter-netflix-ribbon的jar,这里主要是我想让client-two-eureka通过ribbon的方式调用client-one-eureka的服务接口。 3.配置文件

代码语言:javascript
复制
server:
 port: 2001
spring:
 application:
   name: client-two-eureka
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
   fetch-registry: true
   register-with-eureka: true

到此完成第二个服务应用的搭建。

启动client-one-eureka、client-two-eureka、server-eureka。当然server-eureka启动完成后,再启动client-one-eureka、client-two-eureka。原因很简单,服务中心起来之后,才能注册服务。 服务中心管理页面如下:

服务中心管理 可以看到应用中心里面已经有两个了。到这里服务注册功能已经完成。

服务之间相互调用

1.client-one-eureka 创建一个Client1Test的controller

代码语言:javascript
复制
package org.client.one.eureka.controller;

import org.client.one.eureka.service.FeignSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import colud.clent.model.User;

@RestController
@RequestMapping("/client1Test")
public class Client1Test {

  @Autowired
  FeignSevice feignSevice;
     /**通过client-one-eureka调用client-two-eureka里面的client2Test/clint1(Feign方式)
   * @param id
   * @return
   */
  @GetMapping("/clint1")
  public String name(@RequestParam String id) {
      System.out.println("进入client-one-eureka服务client1Test/clint1方法");
      return feignSevice.test(id, new User("1", "ysh"));
  }

    /**等待被client-two-eureka调用
   * 
   * @param id
   * @return
   */
  @GetMapping("/toCall")
  public String test1client(@RequestParam String id) {
      System.out.println("进入client-one-eureka服务client1Test/toCall方法");
      return "调用eureka-client1服务test1client方法,参数id=" + id;
  }
}

client-one-eureka调用client-two-eureka是通过Feign的方式。所以这边创建了一个FeignSevice。代码如下:

代码语言:javascript
复制
package org.client.one.eureka.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import colud.clent.model.User;

@Component
@FeignClient(name = "client-two-eureka")
public interface FeignSevice {

  @RequestMapping(value = "/client2Test/test2", method = RequestMethod.GET)
  public String test(@RequestParam("id") String id, @RequestBody User user);

}

代码说明:@FeignClient指定了我们所要调用服务的名称,@RequestMapping后面跟的就是我们所要调用服务的具体路径。 2.client-two-eureka 创建的controller代码

代码语言:javascript
复制
package org.client.two.eureka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import colud.clent.model.User;

@RestController
@RequestMapping("/client2Test")
public class Client2Test {

  @Autowired
  RestTemplate restTemplate;

  /**通过client-two-eureka调用client-one-eureka的client1Test/toCall方法(ribbon方式)
   * 
   * @param id
   * @return
   */
  @GetMapping("/hi")
  public String hi(@RequestParam String id) {
      System.out.println("进入client-two-eureka服务client2Test/hi方法");
      return restTemplate.getForObject("http://client-one-eureka/client1Test/toCall?id=" + id, >String.class);
  }

  /**等待被client-one-eureka调用
   * 
   * @param id
   * @param user
   * @return
   */
  @PostMapping("/test2")
  public String testOne2(@RequestParam(required = false) String id, @RequestBody User user) {
      System.out.println("进入client2Test/test2");
      return "调用client-two-eureka服务test2方法,参数id=" + id + "user=" + user;
  }
}
 

通过代码可以知道client-one-eureka的client1Test/clint1?id=''可以调用到client-two-eureka的client2Test/test2接口。这个接口采用了返回复杂对象予以说明。由于采用了复杂对象。为了服务间共享模型数据。所以我新建了clent-model模块。并在client-one-eureka服务、client-two-eureka服务中相继加入该模块依赖,完成数据结构共享。调用结果显示如下:

one call two

同样通过client-two-eureka的client2Test/hi?id=''可以调用到client-one-eureka的client1Test/toCall方法。结果演示如下:

two call one

文章到此就完成了服务注册与通过ribbon、Feign两种方式完成服务间相互调用。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 大纲
  • 注册中心
  • 启动eureka-server,界面如下:
  • 搭建第一个服务应用client-one-eureka
  • 搭建第二个服务应用client-two-eureka
  • 服务之间相互调用
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档