springcloud的配置流程

一.在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

 

转载于:https://www.cnblogs.com/handsome1013/p/10963559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值