点击上方 "程序员小乐"关注, 星标或置顶一起成长
每天凌晨00点00分, 第一时间与你相约
每日英文
Themorewefind,themorewelose.Themoreweknow,themorewehurt.Themorewecare,themorewearesad.
得到越多,失去越多。懂得越多,伤得越多。在乎越多,难过越多。
每日掏心话
如果你越来越冷漠,你以为你成长了,但其实没有。长大应该是变温柔,对全世界都温柔。
来自:XuePeng77 | 责编:乐乐
链接:my.oschina.net/u/2450666/blog/1162531
程序员小乐(ID:study_tech)第 821 次推文 图片来自百度
往日回顾:微信技术总监:一亿用户背后的架构秘密
正文
手把手教你SpringBoot+SpringCloud系列共8篇:
1、手把手教你 SpringBoot + SpringCloud 开发环境搭建(一)
一、创建Eureka注册中心(Standalone Mode —— 独立模式)
1.1 创建Eureka Server工程
使用STS创建EurekaServer工程。
点击File —— New —— Spring Starter Project后,会弹出创建SpringBoot工程的窗口,输入工程信息后点击下一步。
工程信息可以选择构建工具,这里选择的Maven;
选择Packaging,是Jar包还是War包;
选择Java版本;
选择语言(支持Java、Kotlin和Groovy);
Group、Artifact、Version和Description为maven的相关属性;
最后一个Package是我们定义的工程主类的包,SpringBoot默认会识别该包和其下的子包;
选择SpringBoot的版本,并搜索eureka,勾选Eureka Server后,点击Finish创建工程。
创建好的工程目录结构如下:
工程会自动为我们创建maven结构的工程,并且创建了OwlBookstoreEurekaServerApplication类和对应的测试类,在resources文件夹下还有一个空白的application.properties属性文件。
1.2 pom文件
为了将工程作为Eureka注册中心,我们在创建工程时引入了Eureka Server包,在pom文件中可以看到:
<?xml version="1.0" encoding="UTF-8"?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0cn.net.bysoft
owl-bookstore-eureka-server1.0-SNAPSHOTjarowl-bookstore-eureka-serverowl-bookstore-eureka-serverorg.springframework.boot
spring-boot-starter-parent1.5.4.RELEASEUTF-8UTF-81.8Dalston.SR1org.springframework.cloud
spring-cloud-starter-eureka-serverorg.springframework.boot
spring-boot-starter-testtestorg.springframework.boot
spring-boot-devtoolstruetrueorg.springframework.cloud
spring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.boot
spring-boot-maven-pluginorg.springframework.boot
spring-boot-maven-plugintrue
1.3 application文件
接下来,我们修改配置信息,启动Eureka Server。
SpringBoot支持yml格式的配置文件,在resources中创建一个application.yml文件,并编写如下内容:
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
debug: true
logging:
file: wol-bookstore-eureka-server.log
level:
root: info
spring:application:name:给应用定义一个名称;
server:port:是应用的端口号;
eureka:instance:hostname:是应用所在的主机名;
eureka:client:registerWithEureka:用于应用是注册中心,所以不向注册中心注册自己;
eureka:client:fetchRegistry:用于应用是注册中心,所以不需要去检索服务;
eureka:client:serviceUrl:defaultZone:定义注册中心的地址;
debug:打开日志功能;
logging:file:日志文件指向当前应用的目录;
logging:level:root:输出级别为info;
1.4 修改主类
最后用@EnableEurekaServer修饰主类OwlBookstoreEurekaServerApplication。
package cn.net.bysoft.owl.bootstore;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
*- 使用@EnableEurekaServer来修饰主类;
*
*
* @author xuepeng
*/
@SpringBootApplication
@EnableEurekaServer
public class OwlBookstoreEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(OwlBookstoreEurekaServerApplication.class, args);
}
}
1.5 运行注册中心
使用SpringBootApp运行程序,程序运行后访问地址http://localhost:8761来进入注册中心。这里需要注意,虽然配置的是http://${eureka.instance.hostname}:${server.port}/eureka/,但是注册中心的地址不带有/eureka。
到此,Eureka注册中心已经运行成功了。
二、服务注册
2.1 创建service-user工程
创建一个服务,命名为owl-bookstore-service-user。
2.2 pom文件
在pom文件中,加入spring-cloud-starter-eureka依赖。
<?xml version="1.0" encoding="UTF-8"?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0cn.net.bysoft
owl-bookstore-service-user1.0-SNAPSHOTjarowl-bookstore-service-userowl-bookstore-service-userorg.springframework.boot
spring-boot-starter-parent1.5.4.RELEASEUTF-8UTF-81.8Dalston.SR1org.springframework.cloud
spring-cloud-starter-eurekaorg.springframework.boot
spring-boot-starter-testtestorg.springframework.cloud
spring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.boot
spring-boot-maven-plugin
2.3 application文件配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 7001
spring:
application:
name: SERVICE-USER
eureka:client:serviceUrl:defaultZone:设置注册中心的地址;
server:port:服务的端口号;
spring:application:name:服务的名称,该名称不要和其他的服务重名;
2.4 编写简单的业务demo
cn.net.bysoft.owl.bookstore.service.user包下存放着主类,使用@EnableDiscoveryClient修饰主类:
package cn.net.bysoft.owl.bookstore.service.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OwlBookstoreServiceUserApplication {
public static void main(String[] args) {
SpringApplication.run(OwlBookstoreServiceUserApplication.class, args);
}
}
cn.net.bysoft.owl.bookstore.service.user.entity包下存放user服务的实体类:
package cn.net.bysoft.owl.bookstore.service.user.entity;
public class User {
private final long id;
private final String name;
public User(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}
cn.net.bysoft.owl.bookstore.service.user.facade包下存放着具体的user服务:
package cn.net.bysoft.owl.bookstore.service.user.facade;
import java.util.List;
import cn.net.bysoft.owl.bookstore.service.user.entity.User;
public interface UserService {
List getAll();
}
cn.net.bysoft.owl.bookstore.service.user.facade.impl包下存放着服务的实现,用@RestController修饰类,用@RequestMapping修饰方式,@Value用来读取application.yml文件中的server.port的值:
package cn.net.bysoft.owl.bookstore.service.user.facade.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.net.bysoft.owl.bookstore.service.user.entity.User;
import cn.net.bysoft.owl.bookstore.service.user.facade.UserService;
@RestController
public class UserServiceImpl implements UserService {
@Value("${server.port}")
String port;
@RequestMapping(value = "users