項目啟動順序:
1.啟動注冊中心
2.啟動 serverconfig
3.啟動 business
4.啟動 system
5.啟動 web
6.Finchley.RC1 這個版本的 Spring Cloud 已經無需添加@EnableDiscoveryClient注解了。
一.在common-server 里面主要部署了mybatis和druid的配置,也就是數據庫的鏈接配置和mysql的配置
druid是在DruidConfiguration.java配置的
mybatis是在common-server 工程下面 MyBatisConfig.java文件配置的,而且加載了druid是在DruidConfiguration文件
mybatis掃描別名的包在AliasesPackage("com.aoyuan.masterdata.moudle");
moudle單獨是一個工程存在,pojo和dto等都是放在這個工程下面
二.服務的注冊中心 eureka-server-register-center
通過@EnabledEurakeServer在啟動類上添加
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerRegisterCenterApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerRegisterCenterApplication.class, args);
}
}
在 application.yml配置:
eureka.client.register-with-eureka:表示是否將自己注冊到 Eureka Server,默認為 true。
eureka.client.fetch-registry:表示是否從 Eureka Server 獲取注冊信息,默認為 true。你自己都是注冊中心了,你就沒必要還要去注冊中心獲取注冊信息,設為false就可以了
eureka.client.service-url.defaultZone:設置與 Eureka Server 交互的地址,查詢服務和注冊服務都需要依賴這個地址。 默認是 http://localhost:1001/eureka ;多個地址可使用英文逗號(,)分隔。
如果是集群的配置,register-with-eureka和fetch-registry設置為true,需要讓自己也注冊到自己的注冊服務
server:
port: 1001
tomcat:
basedir: /master-data/eureka-server-register-center/tmp
eureka:
environment: dev
instance:
hostname: localhost
server:
enable-self-preservation: false #設為false,關閉自我保護
eviction-interval-timer-in-ms: 5000 #清理間隔(單位毫秒,默認是60*1000)
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
spring:
application:
name: eureka-server-register-center
profiles:
active: native
management:
endpoints:
web:
exposure:
include: '*'
三.config配置中心 為了方便服務配置文件統一管理,更易於部署、維護、所以就需要分布式配置中心組件
config分兩個角色 ,一個是config server 一個是config client
服務器配置:
除了springboot的jar引入之外,還要引入 spring-cloud-config-server
第二步:啟動類配置: @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ServerConfigApplication.class, args);
}
}
在application-dev.yml配置文件里面會把自己發布到git上面uri,並且還會在eureka上面注冊defaultZone:
server:
port: 1002
tomcat:
basedir: /master-data/server/server-config/tmp
spring:
cloud:
config:
server:
git:
uri: http://gongluzhen@rmis.ideasoft.net.cn:8090/scm/dom/aoyuan.git
default-label: idea-2019-05-13
search-paths: master-data/config
ignoreLocalSshSettings: true
strictHostKeyChecking: false
force-pull: true
cloneOnStart: true
skipSslValidation: true
username: gongluzhen
password: 123456
rabbitmq:
host: 192.168.209.173
port: 5672
username: masterdata
password: 123456
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:1001/eureka/
2.首先啟動服務注冊中心,然后啟動ServerConfigApplication啟動類
在瀏覽器打開localhost:1002/application-dev.yml查看配置文件
3.config client要引入server的配置
首先引入spring-cloud-starter-config的客戶端的jar包
在我們自己的server-generate-code的bootstrap.yml 里面會有
spring:
main:
allow-bean-definition-overriding: true
application:
name: server-generate-code
profiles:
active: '@profiles.active@'
cloud:
config:
fail-fast: true
profile: '@profiles.active@'
#面向服務,允許被發現
discovery:
enabled: true
#這個名字是Config Server端的服務名字,不能瞎寫。
service-id: server-config
bus:
enabled: true
trace:
enabled: true
eureka:
instance:
lease-renewal-interval-in-seconds: 5 #表示eureka client發送心跳給server端的頻率。如果在leaseExpirationDurationInSeconds后,server端沒有收到client的心跳,則將摘除該instance。
lease-expiration-duration-in-seconds: 10 #表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超時時間,在這個時間內若沒收到下一次心跳,則將移除該instance。
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
management:
endpoints:
web:
exposure:
include: '*'
四 服務的提供者和消費者
服務的提供者和消費者都是注冊中心的client,都是需要向注冊中心注冊的
服務提供者
我們假設服務提供者有一個 hello() 方法,可以根據傳入的參數,提供輸出 “hello xxx + 當前時間” 的服務。
POM 包配置
創建一個基本的 Spring Boot 應用,命名為eureka-producer,在 pom.xml 中添加如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件,制定服務注冊中心
1.application.yml配置:
spring:
application:
name: eureka-producer
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
server:
port: 8000
通過spring.application.name屬性,我們可以指定微服務的名稱, 后續在調用的時候只需要使用該名稱就可以進行服務的訪問。
eureka.client.serviceUrl.defaultZone屬性對應服務注冊中心的配置內容,指定服務注冊中心的位置。
為了在本機上測試區分服務提供方和服務注冊中心,使用server.port屬性設置不同的端口。
Finchley.RC1 這個版本的 Spring Cloud 已經無需添加@EnableDiscoveryClient注解了。
@SpringBootApplication
public class EurekaProducerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProducerApplication.class, args);
}
}
編寫controller
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/")
public String hello(@RequestParam String name) {
return "Hello, " + name + " " + new Date();
}
}
hello方法就被提供出去了,
啟動工程后,就可以在注冊中心 Eureka 的頁面看到 EUERKA-PRODUCER 服務。
我們模擬一個請求試一下 Producer 能否正常工作
http://localhost:8000/hello/?name=windmt
服務的消費者
創建服務消費者根據使用 API 的不同,大致分為三種方式。
雖然大家在實際使用中用的應該都是 Feign,但是這里還是把這三種都介紹一下吧,如果你只關心 Feign,可以直接跳到最后。
三種方式均使用同一配置文件,不再單獨說明了
spring:
application:
name: eureka-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/ # 指定 Eureka 注冊中心的地址
server:
port: 9000
POM 包配置
創建一個基本的 Spring Boot 應用,命名為eureka-producer-feign,在 pom.xml 中添加如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-client</artifactId>
</dependency>
啟動類
在啟動類上加上@EnableFeignClients
@EnableFeignClients
@SpringBootApplication
public class EurekaConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignApplication.class, args);
}
}
Feign 調用實現,其實就是消費方綁定提供方的接口
創建一個 Feign 的客戶端接口定義,注意HelloRemote是一個接口來的
使用@FeignClient注解來指定這個接口所要調用的服務名稱(就是服務的提供者),因為都會在服務注冊中心注冊所以就能拿到
接口中定義的各個函數使用 Spring MVC 的注解就可以來綁定服務提供方的 REST 接口,
比如下面就是綁定 eureka-producer 服務的/hello/接口的例子:
@FeignClient(name = "eureka-producer")
public interface HelloRemote {
@GetMapping("/hello/")
String hello(@RequestParam(value = "name") String name);
}
此類中的方法和遠程服務中 Contoller 中的方法名和參數需保持一致。
Controller
創建 Controller,將 HelloRemote 注入到 controller 層,像普通方法一樣去調用即可
@RequestMapping("/hello")
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@GetMapping("/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name + "!");
}
}
所以,要想使用 Feign,至少需要以下三個依賴
spring-boot-starter-web
spring-cloud-starter-openfeign
spring-cloud-starter-netflix-eureka-client