(1)、新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等。

项目结构:

公共Bean:

 package cn.coreqi.entities;

 import java.io.Serializable;

 public class User implements Serializable {
private Integer id;
private String userName;
private String passWord;
private Integer enabled; public User() {
} public User(Integer id, String userName, String passWord, Integer enabled) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
this.enabled = enabled;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public Integer getEnabled() {
return enabled;
} public void setEnabled(Integer enabled) {
this.enabled = enabled;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", enabled=" + enabled +
'}';
}
}

公共服务接口:

 package cn.coreqi.service;

 import cn.coreqi.entities.User;

 import java.util.List;

 public interface UserService {
public void addUser(User user);
public void delById(Integer id);
public void modifyUser(User user);
public User getById(Integer id);
public List<User> getList();
}

(2)、新建SpringBoot项目用作与Eureka注册中心

  1)、导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Server)

  

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

  2)、配置Eureka Server相关信息(二选一)

    ①properties

 server.port=8761
#eureka.instance.hostname Eureka服务端实例名称
eureka.instance.hostname=eureka-server
#eureka.client.register-with-eureka 是否向注册中心注册自己
eureka.client.register-with-eureka=false
#eureka.client.fetch-registry 是否从Eureka上获取服务的注册信息,自己就是注册中心,本身职责就是维护服务实例,并不需要去检索服务
eureka.client.fetch-registry=false
#eureka.client.service-url.defaultZone 设置与Eureka Server交互的地址(查询服务、注册服务等)
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/

    ②yml

 server:
port: 8761
eureka:
instance:
hostname: eureka-server #Eureka服务端实例名称
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka上获取服务的注册信息,自己就是注册中心,本身职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://localhost:${server.port}/eureka/ #设置与Eureka Server交互的地址(查询服务、注册服务等)

  3)、在主程序类上添加@EnableEurekaServer注解启用注册中心功能

 package cn.coreqi;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer //启用Eureka注册中心功能
public class SpringbootcloudregistryApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudregistryApplication.class, args);
} }

  4)、启动项目后访问http://ip:port查看服务启动情况

(3)、新建SpringBoot项目用作与服务提供者

  1)、导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Discovery,注意还要导入公共项目)

         <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.coreqi</groupId>
<artifactId>springbootcloudapi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

  2)、配置Eureka相关信息(二选一)

    ①properties

 server.port=8001

 spring.application.name=user-provider

 #eureka.instance.prefer-ip-address  注册服务的时候使用服务的ip地址
eureka.instance.prefer-ip-address=true eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

    ②yml 

 server:
port: 8001
spring:
application:
name: user-provider
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/

  3)、编写服务及控制器等

  

 package cn.coreqi.service.impl;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List; @Service
public class UserServiceImpl implements UserService {
private static List<User> users = new ArrayList<>();
static {
users.add(new User(1,"fanqi","123456",1));
users.add(new User(2,"zhangsan","123456",1));
users.add(new User(3,"lisi","123456",1));
users.add(new User(4,"wangwu","123456",1));
}
@Override
public void addUser(User user) {
users.add(user);
} @Override
public void delById(Integer id) {
for (User s:users){
if(s.getId() == id){
users.remove(s);
break;
}
}
} @Override
public void modifyUser(User user) {
delById(user.getId());
addUser(user);
} @Override
public User getById(Integer id) {
for (User s:users){
if(s.getId() == id){
return s;
}
}
return null;
} @Override
public List<User> getList() {
return users;
}
}
 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class UserController {
@Autowired
private UserService userService; @GetMapping("/users")
public List<User> getUsers(){
return userService.getList();
}
}

  4)、在主程序类上添加@EnableEurekaClient注解启动Eureka客户端功能,并启动项目

 package cn.coreqi;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient ////启用Eureka客户端功能
public class SpringbootcloudserviceproviderApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudserviceproviderApplication.class, args);
} }

(4)、新建SpringBoot项目用作与服务消费者

  1)导入依赖(或者在SpringBoot初始化向导中勾选cloud Discovery =》 Eureka Discovery,注意还要导入公共项目)=》同服务提供者一致,此处略

  2)配置Eureka相关信息(二选一)

    ①properties

 server.port=8200

 spring.application.name=user-consumer

 #eureka.instance.prefer-ip-address  注册服务的时候使用服务的ip地址
eureka.instance.prefer-ip-address=true eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

    ②yml

 server:
port: 8200
spring:
application:
name: user-consumer
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/

  3)在主程序上添加@EnableDiscoveryClient注解,开启发现服务功能,并在容器中添加RestTemplate。

 package cn.coreqi;

 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.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient //开启服务发现功能
public class SpringbootcloudserviceconsumerApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudserviceconsumerApplication.class, args);
} @Bean
@LoadBalanced //使用负载均衡机制
public RestOperations restTemplate(){
return new RestTemplate();
} }

  4)、在控制器中注入RestTemplate并调用远程服务

 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestOperations; import java.util.ArrayList;
import java.util.List; @RestController
public class UserController {
@Autowired
private RestOperations restTemplate;
@GetMapping("/userno1")
public User getUsersFirst(){
User[] users = restTemplate.getForObject("http://user-provider/users",User[].class);
return users[0];
}
}

SpringBoot集成SpringCloud的更多相关文章

  1. springboot集成springcloud,启动时报错java.lang.AbstractMethodError: null

    出现这个问题是springboot和springcloud的版本不匹配. 我此处使用了springboot 2.0.4.RELEASE,springcloud 使用了Finchley.SR2. 修改方 ...

  2. 微服务学习三:springboot与springcloud集成之Eurake的使用(server端,client端)

    这个多亏了网站上的一个大神的博客: http://blog.csdn.net/forezp/article/details/70148833 强烈推荐学习: 1.springcloud是什么,这个大家 ...

  3. 流式大数据计算实践(5)----HBase使用&SpringBoot集成

    一.前言 1.上文中我们搭建好了一套HBase集群环境,这一文我们学习一下HBase的基本操作和客户端API的使用 二.shell操作 先通过命令进入HBase的命令行操作 /work/soft/hb ...

  4. SpringBoot和SpringCloud面试题

    一. 什么是springboot 1.用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 2.创建独立的spring引用程序 main方法运行 ...

  5. Spring SpringBoot和SpringCloud的关系

    Spring SpringBoot和SpringCloud的关系 Spring Cloud 是完全基于 Spring Boot 而开发,Spring Cloud 利用 Spring Boot 特性整合 ...

  6. 几道关于springboot、springCloud的面试题。

    什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(propertites或yml文件) 创建独立的spring引用程序main方法运行 嵌入的tom ...

  7. Spring、SpringMVC、SpringBoot、SpringCloud的区别与联系

    前言 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架.Spring使你能够编写更干净.更可管理.并且更易于测试的代码. Spring MVC是Spring的一个模块,一个w ...

  8. [转帖]spring、springMvc、springBoot和springCloud的联系与区别

    spring.springMvc.springBoot和springCloud的联系与区别 -- :: 尘光掠影 阅读数 文章标签: springspringmvcspringbootspringCl ...

  9. spring、springMvc、springBoot和springCloud的联系与区别

    spring和springMvc: 1. spring是一个一站式的轻量级的java开发框架,核心是控制反转(IOC)和面向切面(AOP),针对于开发的WEB层(springMvc).业务层(Ioc) ...

随机推荐

  1. P3312 [SDOI2014]数表

    啊啊啊我昨天怎么没写题解wwww 补昨日题解... 题目链接 : https://www.luogu.org/problemnew/show/P3312 也是莫反 我要把fft留到今天写 [和zyn小 ...

  2. 洛谷P3959 宝藏(NOIP2017)(状压DP,子集DP)

    洛谷题目传送门 Dalao的题解多数是什么模拟退火.DFS剪枝.\(O(3^nn^2)\)的状压DP之类.蒟蒻尝试着把状压改进了一下使复杂度降到\(O(3^nn)\). 考虑到每条边的贡献跟它所在的层 ...

  3. Nginx多进程高并发、低时延、高可靠机制在缓存(redis、memcache)twemproxy代理中的应用

    1. 开发背景 现有开源缓存代理中间件有twemproxy.codis等,其中twemproxy为单进程单线程模型,只支持memcache单机版和redis单机版,都不支持集群版功能. 由于twemp ...

  4. BZOJ 4032: [HEOI2015]最短不公共子串

    4032: [HEOI2015]最短不公共子串 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 446  Solved: 224[Submit][Sta ...

  5. 洛谷 P1121 环状最大两段子段和 解题报告

    P1121 环状最大两段子段和 题目描述 给出一段环状序列,即认为\(A_1\)和\(A_N\)是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 第一行是一个正整数 ...

  6. 洛谷 P4408 逃学的小孩 解题报告

    P4408 [NOI2003]逃学的小孩 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?&q ...

  7. Angularjs中的$q详解

    先说说什么是Promise,什么是$q吧.Promise是一种异步处理模式,有很多的实现方式,比如著名的Kris Kwal's Q还有JQuery的Deffered. 什么是Promise 以前了解过 ...

  8. 洛谷P3195 玩具装箱TOY

    题目大意: 有n个数,要将他们分成若干段,每一段的cost定义为: cost=r-l+ΣCk (k∈[r,l]) 该段的最终花费是:(cost-L)^2; 给出L,n,C(1~n),总共的最小花费. ...

  9. 通过url传递参数如果汉字乱码采用的方法

    urlCodeDeal 方法把汉字编码, 在Jsp界面通过Escape.unescape方法,将编码反编译成汉字. 下面是urlCodeDeal方法: //UrlCode 处理代码 function ...

  10. python 正则括号的使用及踩坑

    直接先上结论: 若匹配规则里有1个括号------返回的是括号所匹配到的结果, 若匹配规则里有多个括号------返回多个括号分别匹配到的结果, 若匹配规则里没有括号------就返回整条语句所匹配到 ...