SpringCloud Netflix相关代码

一、bean模块:二、eureka7001模块1.springBoot启动类2、yaml配置文件3、pom三、eureka7002模块yaml配置文件四、服务提供者(mySc-dept)1、pom依赖2、yaml配置3、config包下DeptConfig配置4、controller层DeptController5、mapper接口DeptMapper6、springBoot启动类五、服务提供者2和服务提供者3(mySc-dept-provider-8002)yaml配置六、服务消费者()1、pom依赖2、yaml配置3、config配置(DeptConfig)4、controller层5、springBoot启动类七、feign(负载均衡)1、pom2、yaml配置3、config层4、controller层5、springBoot启动类八、Hystrix(服务熔断)1、pom2、config层3、controller层4、mapper5、springBoot启动类6、配置文件yaml九、zuul(服务路由网关)1、pom2、yaml配置3、springBoot启动类

一、bean模块:

DeptService(接口)

 package com.gk.service;

import com.gk.bean.Dept;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@FeignClient(value = "MYSC-DEPT")
@Component
public interface DeptService {

@RequestMapping("byId/{id}")
public Dept salById(@PathVariable(value = "id") Long id);
@RequestMapping("salList")
public List salByList();

@RequestMapping("salByInfo")
public Object getDiscovery();
}

二、eureka7001模块

1.springBoot启动类

 @SpringBootApplication
@EnableEurekaServer
public class EurekaStart {
public static void main( String[] args ){
SpringApplication.run(EurekaStart.class,args);
}
}

2、yaml配置文件

 server:
port: 7001

eureka相关

eureka:
instance:
hostname: activate.navicat.com
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #fetch-registry为false,表示注册中心是自己
service-url:
defaultZone: http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/

3、pom

 


org.springframework.cloud
spring-cloud-starter-eureka-server
1.3.4.RELEASE


org.springframework.boot
spring-boot-starter-web

三、eureka7002模块

启动springBoot类、pom在eureka7001模块基础上 都不变

yaml配置文件

 server:
port: 7002

eureka相关

eureka:
instance:
hostname: activate.navicat2.com
client:
register-with-eureka: false #是否向eureka注册中心注册自己
fetch-registry: false #fetch-registry为false,表示注册中心是自己
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat3.com:7003/eureka/

四、服务提供者(mySc-dept)

1、pom依赖

 

com.gk
mySc-bean
1.0-SNAPSHOT


mysql
mysql-connector-java


com.alibaba
druid


com.baomidou
mybatis-plus-boot-starter


org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-devtools



org.springframework.cloud
spring-cloud-starter-eureka
1.3.4.RELEASE


org.springframework.boot
spring-boot-starter-actuator

2、yaml配置

 server:
port: 8000

spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root

eureka相关

eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-8000
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud
company.name: 这是注定,这是命运

3、config包下DeptConfig配置

 @Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {

}

4、controller层DeptController

 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gk.bean.Dept;
import com.gk.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {
@Autowired
private DeptMapper deptMapper;
@Autowired
private DiscoveryClient client;//可以用来获取服务信息

@RequestMapping("byId/{id}")
public Dept salById(@PathVariable Long id){
QueryWrapper dw = new QueryWrapper<>();
dw.eq("deptno",id);
Dept dept = deptMapper.selectOne(dw);
return dept;
}
@RequestMapping("salList")
public List salByList(){
List depts = deptMapper.selectList(null);
return depts;
}

@RequestMapping("salByInfo")
public Object getDiscovery(){
//获取微服务列表清单
List list = client.getServices();
System.out.println(list);
System.out.println("================");
//通过微服务的Application(注册在eureka中的实例) 获取某个具体的微服务信息
List instances = client.getInstances("MYSC-DEPT");//获取mySc-dept模块的服务信息
for (ServiceInstance s : instances) {
System.out.println(
s.getHost() + "\n"+
s.getInstanceId() + "\n"+
s.getScheme() + "\n"+
s.getServiceId() + "\n"+
s.getMetadata() + "\n"+
s.getPort() + "\n"+
s.getUri()+"\n"+
"==============="
);
}
return instances;
}
}

5、mapper接口DeptMapper

 @Repository
public interface DeptMapper extends BaseMapper {
}

6、springBoot启动类

 @SpringBootApplication
@EnableEurekaClient
public class MyScDeptApplication {
public static void main(String[] args) {
SpringApplication.run(MyScDeptApplication.class,args);
}
}

五、服务提供者2和服务提供者3(mySc-dept-provider-8002)

在第一个提供者之上没太对变化

yaml配置

 server:
port: 8002

spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root

eureka相关

eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-8002
ip-address: true # 显示服务器的ip地址
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud2
company.name: 这是注定,这是命运2

 server:
port: 8003

spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root

eureka相关

eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-dept3
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud3
company.name: 这是注定,这是命运3

六、服务消费者()

1、pom依赖

 


org.springframework.cloud
spring-cloud-starter-ribbon
1.4.7.RELEASE


org.springframework.cloud
spring-cloud-starter-eureka
1.3.4.RELEASE


com.gk
mySc-bean
1.0-SNAPSHOT


org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-devtools

2、yaml配置

 server:
port: 8001

eureka:
client:
register-with-eureka: false #不让在eureka注册中心注册自己
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/

3、config配置(DeptConfig)

 @Configuration
public class DeptConfig {
//配置负载均衡实现RestTemplate
@Bean
// IRule: 负载均衡策略(接口)以下为他的实现类
//RoundRobinRule:轮循(默认)
//RandomRule: 随机访问
//AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
//RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
@LoadBalanced //使用ribbon实现负载均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

4、controller层

 @RestController
public class DeptController {
//请求前缀地址
// private final String PREFIX_URL="http://localhost:8000/";
private final String PREFIX_URL="http://MYSC-DEPT/";

@Autowired
private RestTemplate restTemplate;
@RequestMapping("salById/{id}")
public Dept salById(@PathVariable Long id){

return restTemplate.getForObject(PREFIX_URL+"byId/"+id,Dept.class);
}
@RequestMapping("salByList")
public List salByList(){
return restTemplate.getForObject(PREFIX_URL + "salList",List.class);
}
}

5、springBoot启动类

 @SpringBootApplication
@EnableEurekaClient
public class MyScConsumer {
public static void main(String[] args) {
SpringApplication.run(MyScConsumer.class,args);
}
}

七、feign(负载均衡)

1、pom

 

com.gk
mySc-bean
1.0-SNAPSHOT


mysql
mysql-connector-java


com.alibaba
druid


com.baomidou
mybatis-plus-boot-starter


org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-devtools



org.springframework.cloud
spring-cloud-starter-eureka
1.3.4.RELEASE


org.springframework.boot
spring-boot-starter-actuator


org.springframework.cloud
spring-cloud-starter-feign
1.4.3.RELEASE

2、yaml配置

 server:
port: 8005

spring:
application:
name: mySc-dept-feign
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root

eureka相关

eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-feign-8005
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud2
company.name: 这是注定,这是命运2

3、config层

 @Configuration
@ComponentScan("com.gk")
public class DeptConfig {
//配置负载均衡实现RestTemplate
@Bean
// IRule: 负载均衡策略(接口)一下为他的实现类
//RoundRobinRule:轮循(默认)
//RandomRule: 随机访问
//AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
//RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
@LoadBalanced //使用ribbon实现
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

4、controller层

 @RestController
public class DeptController {

@Autowired
private DeptService deptService;
@RequestMapping("salById/{id}")
public Dept salById(@PathVariable(value="id") Long id){

return deptService.salById(id);
}
@RequestMapping("salByList")
public List salByList(){
return deptService.salByList();
}
}

5、springBoot启动类

 @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"com.gk"})
public class FeignScConsumer {
public static void main(String[] args) {
SpringApplication.run(FeignScConsumer.class,args);
}
}

八、Hystrix(服务熔断)

1、pom

 

com.gk
mySc-bean
1.0-SNAPSHOT


mysql
mysql-connector-java


com.alibaba
druid


com.baomidou
mybatis-plus-boot-starter


org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-devtools



org.springframework.cloud
spring-cloud-starter-eureka
1.3.4.RELEASE


org.springframework.boot
spring-boot-starter-actuator


org.springframework.cloud
spring-cloud-starter-hystrix
1.3.4.RELEASE

2、config层

 @Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {

}

3、controller层

 @RestController
public class DeptController {
@Autowired
private DeptMapper deptMapper;

@RequestMapping("byId/{id}")
@HystrixCommand(fallbackMethod="salById2")//方法异常后调用salById2方法
public Dept salById(@PathVariable Long id){
QueryWrapper dw = new QueryWrapper<>();
dw.eq("deptno",id);
Dept dept = deptMapper.selectOne(dw);
if (dept==null){
throw new RuntimeException("查询结果为空...");
}
return dept;
}

public Dept salById2(@PathVariable Long id){
return new Dept(id,"没有当前这个id=>"+id,"没有这个数据库");
}
}

4、mapper

 @Repository
public interface DeptMapper extends BaseMapper {

}

5、springBoot启动类

 @SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker //添加熔断支持
public class MyHystrixScDeptApplication {
public static void main(String[] args) {
SpringApplication.run(MyHystrixScDeptApplication.class,args);
}
}

6、配置文件yaml

 server:
port: 8004

spring:
application:
name: mySc-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
driver-class-name: com.mysql.jdbc.Driver
data-username: root
data-password: root

eureka相关

eureka:
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
instance:
instance-id: mySc-provider-hystrix-8004
info: # 点击 Status 下的内容,提示的内容信息
app.name: 张三的cloud3
company.name: 这是注定,这是命运3

九、zuul(服务路由网关)

1、pom

 


org.springframework.cloud
spring-cloud-starter-zuul
1.4.3.RELEASE


org.springframework.cloud
spring-cloud-starter-eureka
1.3.4.RELEASE


org.springframework.boot
spring-boot-starter-web

2、yaml配置

 server:
port: 9527

spring 配置服务名字

spring:
application:
name: mySc-zuul-9527

eureka

eureka:
instance:
instance-id: mySc-zuul-9527
prefer-ip-address: true # 显示id地址
client:
service-url:
defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
info:
app.name: zhangsan.com
app.address: www.疯子.com
zuul:
routes:
dept.serviceId: mysc-dept
dept.path: /dept/**
ignored-services: "*" # 不允许任何一个路径进行访问,只允许使用 dept(http://www.gk.com:9527/dept/) 进行访问
prefix: /gk #设置公共的访问前缀 :http://www.gk.com:9527/gk/dept/

3、springBoot启动类

 @SpringBootApplication
@EnableZuulProxy
public class MyZuulApplication {
public static void main( String[] args ){
SpringApplication.run(MyZuulApplication.class,args);
}
}

你可能感兴趣的:(SpringCloud Netflix相关代码)