server:
port: 7001
#eureka,注册中心
eureka:
instance:
hostname: localhost #eureka服务端的实例名
client:
register-with-eureka: false #表示不向注册中心注册自己, 默认true
fetch-registry: false #表示自己不向注册中心拿服务, 默认true
service-url:
#这是一个map,设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
#通过http://localhost:7001来启动可视化界面管理
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#固定格式由 http://+hostname+server-port+/eureka/ 组成
这里末尾的/表示访问的是一个目录(默认访问default.html或是index.html),也可以不加,如果不加表示首先当作servlet处理,如果找不到就当作目录处理
主启动类
@EnableEurekaServer//表明该模块是eureka注册中心启动模块,接收其他微服务注册进来
@SpringBootApplication
public class MicrosoftEureka7001Application {
public static void main(String[] args) {
SpringApplication.run(MicrosoftEureka7001Application.class, args);
}
}
application.yml
#datasource
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/clouddb01?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 12345
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5 #初始连接数
max-active: 10 #最大活动连接
max-wait: 60000 #从池中取连接(没有闲置连接)的最大等待时间,-1表示无限等待
min-idle: 5 #最小闲置数,小于min-idle连接池会主动创建新的连接
time-between-eviction-runs-millis: 60000 #清理线程启动的间隔时间,当线程池中没有可用的连接启动清理线程
min-evictable-idle-time-millis: 300000 #清理线程保持闲置最小时间
validation-query: SELECT 1 #用于校验连接
test-on-borrow: false #请求连接时是否校验,比较消耗性能,一般设置false
test-on-return: false #归还连接时是否校验,比较消耗性能,一般设置false
test-while-idle: true #清理线程通过validation-query来校验连接是否正常,如果不正常将从连接池中移除
pool-prepared-statements: true #存储相同逻辑的sql到连接池的缓存中
filters: stat,wall #监控统计web的statement(sql),以及防sql注入的wall
web-stat-filter:
enabled: true #开启监控uri,默认false
url-pattern: /* #添加过滤规则
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid" #忽略过滤
stat-view-servlet:
enabled: true #开启视图管理界面,默认false
url-pattern: /druid/* #视图管理界面uri
login-username: druid #账号
login-password: 12345 #密码
#eureka
application:
name: microsoft-provider-8081 #服务名
eureka:
instance:
instance-id: microsoft-prodiver8081 #修改eureka中status的名字
prefer-ip-address: true #访问status路径可以显示ip
client:
service-url: #将客户端注册进eureka服务列表中
defaultZone: http://localhost:7001/eureka/ #注册中心url
#由于register-with-eureka和fetch-registry 默认是true可以不用显示的指明
#表示当前服务不向注册中心获取服务
fetch-registry: false
#server
server:
port: 8081
#mybatis
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#actuator指定信息, 直接配置即可
info:
app:
name: 我的第一个springboot项目
version: 0.0.1
data:
author: chz
controller
//@Controller
@RestController//前后分离返回json字符串
@RequestMapping("/provider")
public class DeptProviderController {
@Autowired
private DeptService deptService;
//表示对eureka开启读取服务
@Autowired
private DiscoveryClient discoveryClient;
//@RequestBody用于接收前端传过来的json传,且提交的方式必须是post
@PostMapping(path = {"/add"})
public boolean add(@RequestBody Dept dept) {
return deptService.add(dept);
}
@GetMapping(path = "get/{id}")
public Dept get(@PathVariable("id") Integer id) {
return deptService.get(id);
}
@GetMapping(path = "/get")
public List<Dept> list() {
return deptService.list();
}
@GetMapping(path = "/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
List<ServiceInstance> instances =
//对应eureka管理界面中Instances currently registered with Eureka的Application的信息
discoveryClient.getInstances("MICROSOFT-PROVIDER-8081");
for (ServiceInstance instance : instances) {
System.out.println("host===>" + instance.getHost());
System.out.println("port===>" + instance.getPort());
System.out.println("uri===>" + instance.getUri());
}
return discoveryClient;
}
}
主启动类
@EnableDiscoveryClient//服务发现,对于注册到服务中心的微服务,可以通过服务发现来获取服务信息
@EnableEurekaClient//表明,本服务启动后会自动注入eureka服务中
@SpringBootApplication
public class MicrosoftProvider8081Application {
public static void main(String[] args) {
SpringApplication.run(MicrosoftProvider8081Application.class, args);
}
}
只需要引入api的dependency
yml
server:
port: 8082 #可以设置为80端口, 这样就可以不用输出端口
spring:
application:
name: microsoft-consumer-8082
thymeleaf:
cache: false
restTemplate
/**
* 配置ResTemplate,用于分布式通信
*/
@Configuration
public class ConfigBean {
//执行http请求
//springboot并没有帮我们把RestTemplate注入到ioc中所以要手动注入
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@RequestMapping("/consumer")
@SuppressWarnings("all")
public class DeptConsumerController {
private static final String REST_URL_PREFIX = "http://localhost:8081/provider";
/**
* (url,requestMap,ResponseBean.class)三个参数分别代表
* REST请求地址,请求参数,http响应转换成的对象类型(provider方法返回的类型)
*/
@Autowired
private RestTemplate restTemplate;
@GetMapping("/add")
public Boolean add(Dept dept) {
//postForObject对应post insert
return restTemplate.
//中间的request可以是null
postForObject(REST_URL_PREFIX + "/add", dept, Boolean.class);
}
@GetMapping("/get")
public List<Dept> list() {
return restTemplate.
getForObject(REST_URL_PREFIX + "/get",List.class);
}
@GetMapping("/get/{id}")
public Dept get(@PathVariable("id") Integer id) {
//getForObject对应get
return restTemplate.
getForObject(REST_URL_PREFIX + "/get/" + id, Dept.class);
}
//发现对应提供者的相应服务
@GetMapping("/discovery")
public Object dicovery(){
return restTemplate.getForObject(REST_URL_PREFIX+"/discovery",Object.class);
}
}
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
server:
port: 7001
#eureka,注册中心
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名
client:
register-with-eureka: false #表示不向注册中心注册自己, 默认true
fetch-registry: false #表示自己不向注册中心拿服务, 默认true
service-url:
# eureka集群
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7002
#eureka,注册中心
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名
client:
register-with-eureka: false #表示不向注册中心注册自己, 默认true
fetch-registry: false #表示自己不向注册中心拿服务, 默认true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7002
#eureka,注册中心
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名
client:
register-with-eureka: false #表示不向注册中心注册自己, 默认true
fetch-registry: false #表示自己不向注册中心拿服务, 默认true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
可以通过eureka7001.com:7001
, eureka7002.com:7002
, eureka7003.com:7003
来访问
原文:https://www.cnblogs.com/kikochz/p/12868754.html