当前位置: 首页 > news >正文

java B2B2C Springcloud电子商城系统-搭建一个简单的Eureka程序

Eureka集群主要有三个部分Eureka服务器,服务提供者,服务调用者

简单的来说就是服务提供者将服务注册到Eureka服务器,服务调用者对其服务进行查找调用。

需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六

一.搭建服务器

1.引入maven依赖,使用官方文档中的依赖的结果还是启动不起来,缺少日志相关的依赖,另外自己添加了几个依赖后就OK了

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Dalston.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <artifactId>slf4j-api</artifactId>
      <groupId>org.slf4j</groupId>
      <version>1.7.10</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
      <exclusions>
        <exclusion>
          <artifactId>slf4j-api</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.2.3</version>
    </dependency>

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

  </dependencies>
复制代码

因为Spring cloud集成了很多项目。所以引入spring-cloud-starter-eureka-server就相当于引入了spring-boot-starter-web等,就具有了web容器的功能了。

2.配置yml文件:设置服务器端口以及相关信息

server:
  port: 8761 #更改端口为8761
eureka:
  client:
    register-with-eureka: false #服务器不用注册到其他服务器
    fetch-registry: false #服务器不用去服务器抓取注册信息
复制代码

3.编写服务启动类,也就是main方法启动spring boot,注意使用@EnableEurekaServer注解

package com.nijunyang;

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

@SpringBootApplication
@EnableEurekaServer
public class ServerApp {
  public static void main(String[] args){
      new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
  }
}
  
复制代码

启动之后访问http://localhost:8761/就可以看到Eureka服务器控制台。 二.服务提供者(警察局)

1.maven依赖将服务器的spring-cloud-starter-eureka-server依赖改为spring-cloud-starter-eureka即可

2.yml配置文件:需要将服务提供者注册到Eureka服务器上,服务器的端口设置的8761

server:
  port: 8080
spring:
  application:
    name: first-police  #服务提供者名字
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/  #注册到服务器
复制代码

3.编写一个实体类police和PoliceController,有人报警则派出一个警察

package com.nijunyang;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PoliceController {
    @RequestMapping(value = "/call/{id}",method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Police call(@PathVariable Integer id){
        Police police = new Police();
        police.setId(id);
        police.setName("zhangsan");
        return police;
    }
}
复制代码

4.该模块的启动类,main方法启动和服务器的一样 new SpringApplicationBuilder(XXX(启动类类名).class).web(true).run(args);

三.服务调用者(报警)

1.maveny依赖:在服务提供者的基础上再加入负载均衡(后续了解)相关的依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
复制代码

2.yml配置:同样需要注册到服务器,上面的服务提供者由于没有设置端口所以默认是8080,现在将调用者端口设置8081,否则启动会出错

server:
  port: 8081 #更改端口为8081
spring:
  application:
    name: first-person
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/  #注册到服务器
复制代码

3.编写PersonController,需要新加@Configuration注解,以及配置RestTemplate,RestTemplate本来是spring-web下面的类用来调用REST服务。本身不具备调用分布式服务的能力,但是被@LoadBalanced修饰后就具有访问分布式服务的能力了(具体涉及负载均衡,后续深入了解)。因为是注册到Eureka服务器的,所以我在内部请求的时候只需要转到相应的服务提供者就可以了:http://first-police/xxx

package com.nijunyang;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Configuration
public class PersonController {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    @GetMapping(value = "/call/{id}")
    @ResponseBody
    public String call(@PathVariable Integer id){
        RestTemplate restTemplate = getRestTemplate();
        return restTemplate.getForObject("http://first-police/call/" + id ,String.class);
    }
}
复制代码

4.不多说,启动类编写

依次启动服务,服务提提供者和服务调用者。访问Eureka控制台就可以看到注册进去的first-police和first-person。访问http://localhost:8081/call/id,页面就会返回某个police的josn信息。 java B2B2C Springcloud电子商城系统

转载于:https://juejin.im/post/5c4fb255e51d455d1c52f00f

相关文章:

  • 传闻 Android Q 将支持手机应用版本回滚
  • MD5加密原理解析及OC版原理实现
  • linux环境安装golang
  • 国际乒联2月世界排名:樊振东、丁宁持续领跑
  • 李嘉诚23岁长孙女登场接班!出任地产公司董事,照片曝光气质获赞
  • Nginx实现动静分离
  • Java基础教程,第四讲,字符串使用以及常用字符串处理函数
  • 树链剖分算法解析
  • 数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份...
  • 前端技术周刊 2019-02-11 Serverless
  • 杂篇:一代版本一代神[-Gradle-]
  • MySQL 大表迁移简单方案
  • cenots6.5安装 git version 2.0.5
  • Codeforces Global Round1 简要题解
  • Terraform入门 - 1. 安装Terraform
  • python3.6+scrapy+mysql 爬虫实战
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • ➹使用webpack配置多页面应用(MPA)
  • 10个确保微服务与容器安全的最佳实践
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • Android框架之Volley
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • FastReport在线报表设计器工作原理
  • Go 语言编译器的 //go: 详解
  • isset在php5.6-和php7.0+的一些差异
  • Laravel 中的一个后期静态绑定
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • mockjs让前端开发独立于后端
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 分享一份非常强势的Android面试题
  • 码农张的Bug人生 - 初来乍到
  • 前端js -- this指向总结。
  • 驱动程序原理
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 系统认识JavaScript正则表达式
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (1)(1.13) SiK无线电高级配置(五)
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (3)(3.5) 遥测无线电区域条例
  • (39)STM32——FLASH闪存
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (离散数学)逻辑连接词
  • (算法设计与分析)第一章算法概述-习题
  • (转)菜鸟学数据库(三)——存储过程
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .net 怎么循环得到数组里的值_关于js数组
  • .net专家(高海东的专栏)
  • [2017][note]基于空间交叉相位调制的两个连续波在few layer铋Bi中的全光switch——
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [Asp.net mvc]国际化